2017-06-20 08:30:47 +02:00
|
|
|
/**
|
|
|
|
* Utility functions common to ESLint rules.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2019-05-13 14:50:55 -04:00
|
|
|
function isRequireCall(node) {
|
|
|
|
return node.callee.type === 'Identifier' && node.callee.name === 'require';
|
|
|
|
}
|
|
|
|
module.exports.isRequireCall = isRequireCall;
|
|
|
|
|
2019-05-15 15:29:56 -04:00
|
|
|
module.exports.isString = function(node) {
|
|
|
|
return node && node.type === 'Literal' && typeof node.value === 'string';
|
|
|
|
};
|
|
|
|
|
2018-02-18 20:19:20 +01:00
|
|
|
module.exports.isDefiningError = function(node) {
|
|
|
|
return node.expression &&
|
|
|
|
node.expression.type === 'CallExpression' &&
|
|
|
|
node.expression.callee &&
|
|
|
|
node.expression.callee.name === 'E' &&
|
|
|
|
node.expression.arguments.length !== 0;
|
|
|
|
};
|
|
|
|
|
2022-02-19 18:14:09 +01:00
|
|
|
module.exports.isDefiningDeprecation = function(node) {
|
|
|
|
return node.expression &&
|
|
|
|
node.expression.type === 'CallExpression' &&
|
|
|
|
node.expression.callee &&
|
|
|
|
node.expression.callee.name.endsWith('deprecate') &&
|
|
|
|
node.expression.arguments.length !== 0;
|
|
|
|
};
|
|
|
|
|
2017-06-20 08:30:47 +02:00
|
|
|
/**
|
|
|
|
* Returns true if any of the passed in modules are used in
|
|
|
|
* require calls.
|
|
|
|
*/
|
|
|
|
module.exports.isRequired = function(node, modules) {
|
2019-05-13 14:50:55 -04:00
|
|
|
return isRequireCall(node) && node.arguments.length !== 0 &&
|
2017-06-20 08:30:47 +02:00
|
|
|
modules.includes(node.arguments[0].value);
|
|
|
|
};
|
|
|
|
|
2017-11-01 01:08:45 +05:30
|
|
|
/**
|
2021-12-07 06:35:08 -08:00
|
|
|
* Return true if common module is required
|
|
|
|
* in AST Node under inspection
|
|
|
|
*/
|
2019-03-02 23:07:03 +09:00
|
|
|
const commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/);
|
2017-11-01 01:08:45 +05:30
|
|
|
module.exports.isCommonModule = function(node) {
|
2019-05-13 14:50:55 -04:00
|
|
|
return isRequireCall(node) &&
|
2017-11-01 01:08:45 +05:30
|
|
|
node.arguments.length !== 0 &&
|
|
|
|
commonModuleRegExp.test(node.arguments[0].value);
|
|
|
|
};
|
|
|
|
|
2017-12-26 09:48:38 +01:00
|
|
|
/**
|
|
|
|
* Returns true if any of the passed in modules are used in
|
2019-01-08 10:36:28 -05:00
|
|
|
* process.binding() or internalBinding() calls.
|
2017-12-26 09:48:38 +01:00
|
|
|
*/
|
|
|
|
module.exports.isBinding = function(node, modules) {
|
2019-01-08 10:36:28 -05:00
|
|
|
const isProcessBinding = node.callee.object &&
|
|
|
|
node.callee.object.name === 'process' &&
|
|
|
|
node.callee.property.name === 'binding';
|
|
|
|
|
|
|
|
return (isProcessBinding || node.callee.name === 'internalBinding') &&
|
|
|
|
modules.includes(node.arguments[0].value);
|
2017-12-26 09:48:38 +01:00
|
|
|
};
|
|
|
|
|
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) {
|
2019-03-02 23:07:03 +09:00
|
|
|
let hasSkipBlock = false;
|
2017-06-20 08:30:47 +02:00
|
|
|
if (node.test &&
|
|
|
|
node.test.type === 'UnaryExpression' &&
|
|
|
|
node.test.operator === '!') {
|
|
|
|
const consequent = node.consequent;
|
|
|
|
if (consequent.body) {
|
2019-03-01 22:47:47 -08:00
|
|
|
consequent.body.some((expressionStatement) => {
|
2017-06-20 08:30:47 +02:00
|
|
|
if (hasSkip(expressionStatement.expression)) {
|
|
|
|
return hasSkipBlock = true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
2017-10-16 18:37:14 -04:00
|
|
|
} else if (hasSkip(consequent.expression)) {
|
|
|
|
hasSkipBlock = true;
|
2017-06-20 08:30:47 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasSkipBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
function hasSkip(expression) {
|
2024-09-24 15:48:15 -04:00
|
|
|
return expression?.callee?.name === 'skip' || expression?.callee?.property?.name === 'skip';
|
2017-06-20 08:30:47 +02:00
|
|
|
}
|