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. Removes extra indentation
2. Removes array to track commomModule AST node.
3. Refactored test

Refs: #16636
  • Loading branch information
shobhitchittora committed Feb 12, 2018
1 parent 0e860d0 commit 4afc45d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
8 changes: 4 additions & 4 deletions test/parallel/test-eslint-inspector-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ new RuleTester().run('inspector-check', rule, {
valid: [
'foo;',
'common.skipIfInspectorDisabled(); require("inspector");',
`require("common");
common.skipIfInspectorDisabled();
require("inspector");`
'require("common");\n' +
'common.skipIfInspectorDisabled();\n' +
'require("inspector");'
],
invalid: [
{
code: 'require("inspector")',
code: 'require("inspector");',
errors: [{ message }],
output: 'require("common");\n' +
'common.skipIfInspectorDisabled();\n' +
Expand Down
16 changes: 9 additions & 7 deletions tools/eslint-rules/inspector-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ const utils = require('./rules-utils.js');
// Rule Definition
//------------------------------------------------------------------------------
const msg = 'Please add a skipIfInspectorDisabled() call to allow this ' +
'test to be skippped when Node is built \'--without-inspector\'.';
'test to be skippped when Node is built \'--without-inspector\'.';

module.exports = function(context) {
const missingCheckNodes = [];
const commonModuleNodes = [];
var commonModuleNode = null;
var hasInspectorCheck = false;

function testInspectorUsage(context, node) {
Expand All @@ -24,7 +24,7 @@ module.exports = function(context) {
}

if (utils.isCommonModule(node)) {
commonModuleNodes.push(node);
commonModuleNode = node;
}
}

Expand All @@ -41,10 +41,12 @@ module.exports = function(context) {
node,
message: msg,
fix: (fixer) => {
return fixer.insertTextAfter(
commonModuleNodes[0],
'\ncommon.skipIfInspectorDisabled();'
);
if (commonModuleNode) {
return fixer.insertTextAfter(
commonModuleNode,
'\ncommon.skipIfInspectorDisabled();'
);
}
}
});
});
Expand Down
12 changes: 6 additions & 6 deletions tools/eslint-rules/rules-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ module.exports.usesCommonProperty = function(node, properties) {
module.exports.inSkipBlock = function(node) {
var hasSkipBlock = false;
if (node.test &&
node.test.type === 'UnaryExpression' &&
node.test.operator === '!') {
node.test.type === 'UnaryExpression' &&
node.test.operator === '!') {
const consequent = node.consequent;
if (consequent.body) {
consequent.body.some(function(expressionStatement) {
Expand All @@ -74,8 +74,8 @@ module.exports.inSkipBlock = function(node) {

function hasSkip(expression) {
return expression &&
expression.callee &&
(expression.callee.name === 'skip' ||
expression.callee.property &&
expression.callee.property.name === 'skip');
expression.callee &&
(expression.callee.name === 'skip' ||
expression.callee.property &&
expression.callee.property.name === 'skip');
}

0 comments on commit 4afc45d

Please sign in to comment.