Skip to content

Commit

Permalink
tools: custom eslint autofix for inspector-check.js
Browse files Browse the repository at this point in the history
1. Adds fixer method
2. Extends test

PR-URL: #16646
Refs: #16636
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
shobhitchittora authored and MylesBorins committed Feb 21, 2018
1 parent f48062f commit 11dea28
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
12 changes: 9 additions & 3 deletions test/parallel/test-eslint-inspector-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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")'
}
]
});
18 changes: 17 additions & 1 deletion tools/eslint-rules/inspector-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();'
);
}
}
});
});
}
}
Expand Down

0 comments on commit 11dea28

Please sign in to comment.