2017-06-20 08:30:47 +02:00
|
|
|
/**
|
|
|
|
* 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) {
|
2018-02-12 12:31:23 +01:00
|
|
|
return node.callee.name === 'require' && node.arguments.length !== 0 &&
|
2017-06-20 08:30:47 +02:00
|
|
|
modules.includes(node.arguments[0].value);
|
|
|
|
};
|
|
|
|
|
2017-12-26 09:48:38 +01:00
|
|
|
/**
|
|
|
|
* Returns true if any of the passed in modules are used in
|
|
|
|
* binding calls.
|
|
|
|
*/
|
|
|
|
module.exports.isBinding = function(node, modules) {
|
|
|
|
if (node.callee.object) {
|
|
|
|
return node.callee.object.name === 'process' &&
|
|
|
|
node.callee.property.name === 'binding' &&
|
|
|
|
modules.includes(node.arguments[0].value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-20 08:30:47 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
});
|
2017-10-17 00:37:14 +02:00
|
|
|
} else if (hasSkip(consequent.expression)) {
|
|
|
|
hasSkipBlock = true;
|
2017-06-20 08:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasSkipBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
function hasSkip(expression) {
|
|
|
|
return expression &&
|
|
|
|
expression.callee &&
|
|
|
|
(expression.callee.name === 'skip' ||
|
|
|
|
expression.callee.property &&
|
|
|
|
expression.callee.property.name === 'skip');
|
|
|
|
}
|