From 817f43637b972a86a210d5dd5c2ba50989abc4da 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 ff956cb64ed43f..656e13b67675a9 100644 --- a/test/parallel/test-eslint-inspector-check.js +++ b/test/parallel/test-eslint-inspector-check.js @@ -14,12 +14,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();' + ); + } + } + }); }); } }