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 <ruben@bridgewater.de>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Shobhit Chittora 2017-11-01 03:22:31 +05:30 committed by Ruben Bridgewater
parent f242d4b250
commit 5156342105
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762
2 changed files with 26 additions and 4 deletions

View File

@ -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")'
}
]
});

View File

@ -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();'
);
}
}
});
});
}
}