Skip to content

Commit 5111336

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 1c5a430 commit 5111336

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
@@ -12,12 +12,18 @@ const message = 'Please add a skipIfInspectorDisabled() call to allow this ' +
1212
new RuleTester().run('inspector-check', rule, {
1313
valid: [
1414
'foo;',
15-
'common.skipIfInspectorDisabled(); require("inspector");'
15+
'require("common")\n' +
16+
'common.skipIfInspectorDisabled();\n' +
17+
'require("inspector")'
1618
],
1719
invalid: [
1820
{
19-
code: 'require("inspector")',
20-
errors: [{ message }]
21+
code: 'require("common")\n' +
22+
'require("inspector")',
23+
errors: [{ message }],
24+
output: 'require("common")\n' +
25+
'common.skipIfInspectorDisabled();\n' +
26+
'require("inspector")'
2127
}
2228
]
2329
});

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)