Skip to content

Commit 592bacc

Browse files
shobhitchittoraMylesBorins
authored andcommitted
tools: custom eslint autofix for inspector-check.js
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>
1 parent 1248236 commit 592bacc

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

test/parallel/test-eslint-inspector-check.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ const message = 'Please add a skipIfInspectorDisabled() call to allow this ' +
1414
new RuleTester().run('inspector-check', rule, {
1515
valid: [
1616
'foo;',
17-
'common.skipIfInspectorDisabled(); require("inspector");'
17+
'require("common")\n' +
18+
'common.skipIfInspectorDisabled();\n' +
19+
'require("inspector")'
1820
],
1921
invalid: [
2022
{
21-
code: 'require("inspector")',
22-
errors: [{ message }]
23+
code: 'require("common")\n' +
24+
'require("inspector")',
25+
errors: [{ message }],
26+
output: 'require("common")\n' +
27+
'common.skipIfInspectorDisabled();\n' +
28+
'require("inspector")'
2329
}
2430
]
2531
});

tools/eslint-rules/inspector-check.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' +
1515

1616
module.exports = function(context) {
1717
const missingCheckNodes = [];
18+
var commonModuleNode = null;
1819
var hasInspectorCheck = false;
1920

2021
function testInspectorUsage(context, node) {
2122
if (utils.isRequired(node, ['inspector'])) {
2223
missingCheckNodes.push(node);
2324
}
25+
26+
if (utils.isCommonModule(node)) {
27+
commonModuleNode = node;
28+
}
2429
}
2530

2631
function checkMemberExpression(context, node) {
@@ -32,7 +37,18 @@ module.exports = function(context) {
3237
function reportIfMissing(context) {
3338
if (!hasInspectorCheck) {
3439
missingCheckNodes.forEach((node) => {
35-
context.report(node, msg);
40+
context.report({
41+
node,
42+
message: msg,
43+
fix: (fixer) => {
44+
if (commonModuleNode) {
45+
return fixer.insertTextAfter(
46+
commonModuleNode,
47+
'\ncommon.skipIfInspectorDisabled();'
48+
);
49+
}
50+
}
51+
});
3652
});
3753
}
3854
}

0 commit comments

Comments
 (0)