tools: DRY isRequireCall() in lint rules
This commit makes isRequireCall() a reusable utility function for core's custom ESLint rules. PR-URL: https://github.com/nodejs/node/pull/27680 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com>
This commit is contained in:
parent
53bef423f3
commit
03d43539f9
@ -4,6 +4,8 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { isRequireCall } = require('./rules-utils.js');
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Rule Definition
|
// Rule Definition
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@ -13,10 +15,6 @@ function isString(node) {
|
|||||||
return node && node.type === 'Literal' && typeof node.value === 'string';
|
return node && node.type === 'Literal' && typeof node.value === 'string';
|
||||||
}
|
}
|
||||||
|
|
||||||
function isRequireCall(node) {
|
|
||||||
return node.callee.type === 'Identifier' && node.callee.name === 'require';
|
|
||||||
}
|
|
||||||
|
|
||||||
function isTopLevel(node) {
|
function isTopLevel(node) {
|
||||||
do {
|
do {
|
||||||
if (node.type === 'FunctionDeclaration' ||
|
if (node.type === 'FunctionDeclaration' ||
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
const { isRequireCall } = require('./rules-utils.js');
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Rule Definition
|
// Rule Definition
|
||||||
@ -23,15 +24,6 @@ module.exports = function(context) {
|
|||||||
return node && node.type === 'Literal' && typeof node.value === 'string';
|
return node && node.type === 'Literal' && typeof node.value === 'string';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to check if a node is a require call.
|
|
||||||
* @param {ASTNode} node The node to check.
|
|
||||||
* @returns {boolean} If the node is a require call.
|
|
||||||
*/
|
|
||||||
function isRequireCall(node) {
|
|
||||||
return node.callee.type === 'Identifier' && node.callee.name === 'require';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to check if the path is a module and return its name.
|
* Function to check if the path is a module and return its name.
|
||||||
* @param {String} str The path to check
|
* @param {String} str The path to check
|
||||||
|
@ -4,6 +4,8 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
const { isRequireCall } = require('./rules-utils.js');
|
||||||
|
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
// Rule Definition
|
// Rule Definition
|
||||||
//------------------------------------------------------------------------------
|
//------------------------------------------------------------------------------
|
||||||
@ -32,15 +34,6 @@ module.exports = function(context) {
|
|||||||
return node && node.type === 'Literal' && typeof node.value === 'string';
|
return node && node.type === 'Literal' && typeof node.value === 'string';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Function to check if a node is a require call.
|
|
||||||
* @param {ASTNode} node The node to check.
|
|
||||||
* @returns {boolean} If the node is a require call.
|
|
||||||
*/
|
|
||||||
function isRequireCall(node) {
|
|
||||||
return node.callee.type === 'Identifier' && node.callee.name === 'require';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Function to check if the path is a required module and return its name.
|
* Function to check if the path is a required module and return its name.
|
||||||
* @param {String} str The path to check
|
* @param {String} str The path to check
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
*/
|
*/
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
|
function isRequireCall(node) {
|
||||||
|
return node.callee.type === 'Identifier' && node.callee.name === 'require';
|
||||||
|
}
|
||||||
|
module.exports.isRequireCall = isRequireCall;
|
||||||
|
|
||||||
module.exports.isDefiningError = function(node) {
|
module.exports.isDefiningError = function(node) {
|
||||||
return node.expression &&
|
return node.expression &&
|
||||||
node.expression.type === 'CallExpression' &&
|
node.expression.type === 'CallExpression' &&
|
||||||
@ -16,7 +21,7 @@ module.exports.isDefiningError = function(node) {
|
|||||||
* require calls.
|
* require calls.
|
||||||
*/
|
*/
|
||||||
module.exports.isRequired = function(node, modules) {
|
module.exports.isRequired = function(node, modules) {
|
||||||
return node.callee.name === 'require' && node.arguments.length !== 0 &&
|
return isRequireCall(node) && node.arguments.length !== 0 &&
|
||||||
modules.includes(node.arguments[0].value);
|
modules.includes(node.arguments[0].value);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -26,7 +31,7 @@ module.exports.isRequired = function(node, modules) {
|
|||||||
*/
|
*/
|
||||||
const commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/);
|
const commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/);
|
||||||
module.exports.isCommonModule = function(node) {
|
module.exports.isCommonModule = function(node) {
|
||||||
return node.callee.name === 'require' &&
|
return isRequireCall(node) &&
|
||||||
node.arguments.length !== 0 &&
|
node.arguments.length !== 0 &&
|
||||||
commonModuleRegExp.test(node.arguments[0].value);
|
commonModuleRegExp.test(node.arguments[0].value);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user