node/tools/eslint-rules/rules-utils.js
Anatoli Papirovski 3c0ebf5aca
tools: enable additional eslint rules
Enable additional rules that node either already adheres to
or it makes sense to do so going forward: for-direction,
accessor-pairs, no-lonely-if and symbol-description.

Fix all instances of no-lonely-if in lib & test and disable
accessor-pairs in test-util-inspect.

PR-URL: https://github.com/nodejs/node/pull/16243
Refs: https://eslint.org/docs/rules/for-direction
Refs: https://eslint.org/docs/rules/accessor-pairs
Refs: https://eslint.org/docs/rules/no-lonely-if
Refs: https://eslint.org/docs/rules/symbol-description
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
2017-10-19 13:54:14 -04:00

60 lines
1.5 KiB
JavaScript

/**
* Utility functions common to ESLint rules.
*/
'use strict';
/**
* Returns true if any of the passed in modules are used in
* require calls.
*/
module.exports.isRequired = function(node, modules) {
return node.callee.name === 'require' &&
modules.includes(node.arguments[0].value);
};
/**
* Returns true is the node accesses any property in the properties
* array on the 'common' object.
*/
module.exports.usesCommonProperty = function(node, properties) {
if (node.name) {
return properties.includes(node.name);
}
if (node.property) {
return properties.includes(node.property.name);
}
return false;
};
/**
* Returns true if the passed in node is inside an if statement block,
* and the block also has a call to skip.
*/
module.exports.inSkipBlock = function(node) {
var hasSkipBlock = false;
if (node.test &&
node.test.type === 'UnaryExpression' &&
node.test.operator === '!') {
const consequent = node.consequent;
if (consequent.body) {
consequent.body.some(function(expressionStatement) {
if (hasSkip(expressionStatement.expression)) {
return hasSkipBlock = true;
}
return false;
});
} else if (hasSkip(consequent.expression)) {
hasSkipBlock = true;
}
}
return hasSkipBlock;
};
function hasSkip(expression) {
return expression &&
expression.callee &&
(expression.callee.name === 'skip' ||
expression.callee.property &&
expression.callee.property.name === 'skip');
}