From dfcb6bc13e40ce37359a8d0ce8bdfbf947683717 Mon Sep 17 00:00:00 2001 From: Shobhit Chittora Date: Wed, 1 Nov 2017 03:22:31 +0530 Subject: [PATCH] tools: custom eslint autofix for inspector-check.js 1. Adds fixer method 2. Extends test PR-URL: https://github.com/nodejs/node/pull/16646 Refs: https://github.com/nodejs/node/issues/16636 Reviewed-By: Ruben Bridgewater Reviewed-By: Anna Henningsen Reviewed-By: James M Snell --- test/parallel/test-eslint-inspector-check.js | 12 +++++++++--- tools/eslint-rules/inspector-check.js | 18 +++++++++++++++++- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-eslint-inspector-check.js b/test/parallel/test-eslint-inspector-check.js index ab8314b63818c7..bdec596f8d128e 100644 --- a/test/parallel/test-eslint-inspector-check.js +++ b/test/parallel/test-eslint-inspector-check.js @@ -12,12 +12,18 @@ const message = 'Please add a skipIfInspectorDisabled() call to allow this ' + new RuleTester().run('inspector-check', rule, { valid: [ 'foo;', - 'common.skipIfInspectorDisabled(); require("inspector");' + 'require("common")\n' + + 'common.skipIfInspectorDisabled();\n' + + 'require("inspector")' ], invalid: [ { - code: 'require("inspector")', - errors: [{ message }] + code: 'require("common")\n' + + 'require("inspector")', + errors: [{ message }], + output: 'require("common")\n' + + 'common.skipIfInspectorDisabled();\n' + + 'require("inspector")' } ] }); diff --git a/tools/eslint-rules/inspector-check.js b/tools/eslint-rules/inspector-check.js index bb40a98183250c..00a2dd02963558 100644 --- a/tools/eslint-rules/inspector-check.js +++ b/tools/eslint-rules/inspector-check.js @@ -15,12 +15,17 @@ const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' + module.exports = function(context) { const missingCheckNodes = []; + var commonModuleNode = null; var hasInspectorCheck = false; function testInspectorUsage(context, node) { if (utils.isRequired(node, ['inspector'])) { missingCheckNodes.push(node); } + + if (utils.isCommonModule(node)) { + commonModuleNode = node; + } } function checkMemberExpression(context, node) { @@ -32,7 +37,18 @@ module.exports = function(context) { function reportIfMissing(context) { if (!hasInspectorCheck) { missingCheckNodes.forEach((node) => { - context.report(node, msg); + context.report({ + node, + message: msg, + fix: (fixer) => { + if (commonModuleNode) { + return fixer.insertTextAfter( + commonModuleNode, + '\ncommon.skipIfInspectorDisabled();' + ); + } + } + }); }); } }