From 4afc45d792876f3f0cc62373cb6f720d14aedb6e Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Sun, 4 Feb 2018 17:38:41 +0530 Subject: [PATCH] tools: custom eslint autofix for inspector-check.js 1. Removes extra indentation 2. Removes array to track commomModule AST node. 3. Refactored test Refs: #16636 --- test/parallel/test-eslint-inspector-check.js | 8 ++++---- tools/eslint-rules/inspector-check.js | 16 +++++++++------- tools/eslint-rules/rules-utils.js | 12 ++++++------ 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/test/parallel/test-eslint-inspector-check.js b/test/parallel/test-eslint-inspector-check.js index c3d291f41f45c1..f8621e532ed7cc 100644 --- a/test/parallel/test-eslint-inspector-check.js +++ b/test/parallel/test-eslint-inspector-check.js @@ -13,13 +13,13 @@ new RuleTester().run('inspector-check', rule, { valid: [ 'foo;', 'common.skipIfInspectorDisabled(); require("inspector");', - `require("common"); - common.skipIfInspectorDisabled(); - require("inspector");` + 'require("common");\n' + + 'common.skipIfInspectorDisabled();\n' + + 'require("inspector");' ], invalid: [ { - code: 'require("inspector")', + code: 'require("inspector");', errors: [{ message }], output: 'require("common");\n' + 'common.skipIfInspectorDisabled();\n' + diff --git a/tools/eslint-rules/inspector-check.js b/tools/eslint-rules/inspector-check.js index d4f14335ad3025..00a2dd02963558 100644 --- a/tools/eslint-rules/inspector-check.js +++ b/tools/eslint-rules/inspector-check.js @@ -11,11 +11,11 @@ const utils = require('./rules-utils.js'); // Rule Definition //------------------------------------------------------------------------------ const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' + - 'test to be skippped when Node is built \'--without-inspector\'.'; + 'test to be skippped when Node is built \'--without-inspector\'.'; module.exports = function(context) { const missingCheckNodes = []; - const commonModuleNodes = []; + var commonModuleNode = null; var hasInspectorCheck = false; function testInspectorUsage(context, node) { @@ -24,7 +24,7 @@ module.exports = function(context) { } if (utils.isCommonModule(node)) { - commonModuleNodes.push(node); + commonModuleNode = node; } } @@ -41,10 +41,12 @@ module.exports = function(context) { node, message: msg, fix: (fixer) => { - return fixer.insertTextAfter( - commonModuleNodes[0], - '\ncommon.skipIfInspectorDisabled();' - ); + if (commonModuleNode) { + return fixer.insertTextAfter( + commonModuleNode, + '\ncommon.skipIfInspectorDisabled();' + ); + } } }); }); diff --git a/tools/eslint-rules/rules-utils.js b/tools/eslint-rules/rules-utils.js index da184a806d0afc..46d753f481eab2 100644 --- a/tools/eslint-rules/rules-utils.js +++ b/tools/eslint-rules/rules-utils.js @@ -55,8 +55,8 @@ module.exports.usesCommonProperty = function(node, properties) { module.exports.inSkipBlock = function(node) { var hasSkipBlock = false; if (node.test && - node.test.type === 'UnaryExpression' && - node.test.operator === '!') { + node.test.type === 'UnaryExpression' && + node.test.operator === '!') { const consequent = node.consequent; if (consequent.body) { consequent.body.some(function(expressionStatement) { @@ -74,8 +74,8 @@ module.exports.inSkipBlock = function(node) { function hasSkip(expression) { return expression && - expression.callee && - (expression.callee.name === 'skip' || - expression.callee.property && - expression.callee.property.name === 'skip'); + expression.callee && + (expression.callee.name === 'skip' || + expression.callee.property && + expression.callee.property.name === 'skip'); }