From 2ecf517e565adb3a6a7480cae7e47dd1a7259ebd Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Sun, 4 Feb 2018 17:50:18 +0530 Subject: [PATCH] tools: auto fix custom eslint rule for crypto-check.js 1. Refactors test 2. Removes commonModule AST node as array. Refs : #16636 --- test/parallel/test-eslint-crypto-check.js | 11 +++++------ tools/eslint-rules/crypto-check.js | 12 +++++++----- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/test/parallel/test-eslint-crypto-check.js b/test/parallel/test-eslint-crypto-check.js index 9f6b5798a21721..a58b18eec713c0 100644 --- a/test/parallel/test-eslint-crypto-check.js +++ b/test/parallel/test-eslint-crypto-check.js @@ -12,8 +12,7 @@ new RuleTester().run('crypto-check', rule, { valid: [ 'foo', 'crypto', - ` - if (!common.hasCrypto) { + `if (!common.hasCrypto) { common.skip(); } require("crypto"); @@ -23,8 +22,8 @@ new RuleTester().run('crypto-check', rule, { { code: 'require("crypto")', errors: [{ message }], - output: ` - if (!common.hasCrypto) { + output: + `if (!common.hasCrypto) { common.skip("missing crypto"); } require("crypto"); @@ -33,8 +32,8 @@ new RuleTester().run('crypto-check', rule, { { code: 'if (common.foo) {} require("crypto")', errors: [{ message }], - output: ` - if (!common.hasCrypto) { + output: + `if (!common.hasCrypto) { common.skip("missing crypto"); } require("crypto"); diff --git a/tools/eslint-rules/crypto-check.js b/tools/eslint-rules/crypto-check.js index 3133129dbbe963..a6ba48b5464263 100644 --- a/tools/eslint-rules/crypto-check.js +++ b/tools/eslint-rules/crypto-check.js @@ -23,7 +23,7 @@ const bindingModules = cryptoModules.concat(['tls_wrap']); module.exports = function(context) { const missingCheckNodes = []; const requireNodes = []; - const commonModuleNodes = []; + var commonModuleNode = null; var hasSkipCall = false; function testCryptoUsage(node) { @@ -33,7 +33,7 @@ module.exports = function(context) { } if (utils.isCommonModule(node)) { - commonModuleNodes.push(node); + commonModuleNode = node; } } @@ -84,10 +84,12 @@ module.exports = function(context) { node, message: msg, fix: (fixer) => { - if (commonModuleNodes.length) { + if (commonModuleNode) { return fixer.insertTextAfter( - commonModuleNodes[0], - '\nif (!common.hasCrypto)\n common.skip("missing crypto");' + commonModuleNode, + `\nif (!common.hasCrypto) { + common.skip("missing crypto"); + }` ); } }