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 <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
shobhitchittora authored and rvagg committed Aug 16, 2018
1 parent c32b087 commit 817f436
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 @@ -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")'
}
]
});
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 817f436

Please sign in to comment.