diff --git a/tools/eslint-rules/no-duplicate-requires.js b/tools/eslint-rules/no-duplicate-requires.js index c0d036fab92deb..05a379eba673c9 100644 --- a/tools/eslint-rules/no-duplicate-requires.js +++ b/tools/eslint-rules/no-duplicate-requires.js @@ -4,6 +4,8 @@ */ 'use strict'; +const { isRequireCall } = require('./rules-utils.js'); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -13,10 +15,6 @@ function isString(node) { return node && node.type === 'Literal' && typeof node.value === 'string'; } -function isRequireCall(node) { - return node.callee.type === 'Identifier' && node.callee.name === 'require'; -} - function isTopLevel(node) { do { if (node.type === 'FunctionDeclaration' || diff --git a/tools/eslint-rules/require-common-first.js b/tools/eslint-rules/require-common-first.js index 24e8dbff1a5d6b..2b04a98d9f4bc3 100644 --- a/tools/eslint-rules/require-common-first.js +++ b/tools/eslint-rules/require-common-first.js @@ -4,6 +4,7 @@ 'use strict'; const path = require('path'); +const { isRequireCall } = require('./rules-utils.js'); //------------------------------------------------------------------------------ // Rule Definition @@ -23,15 +24,6 @@ module.exports = function(context) { 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. * @param {String} str The path to check diff --git a/tools/eslint-rules/required-modules.js b/tools/eslint-rules/required-modules.js index 9b895c90033c60..40cb8e46e57f02 100644 --- a/tools/eslint-rules/required-modules.js +++ b/tools/eslint-rules/required-modules.js @@ -4,6 +4,8 @@ */ 'use strict'; +const { isRequireCall } = require('./rules-utils.js'); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -32,15 +34,6 @@ module.exports = function(context) { 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. * @param {String} str The path to check diff --git a/tools/eslint-rules/rules-utils.js b/tools/eslint-rules/rules-utils.js index b85425cb50d59c..713a49bf7aa183 100644 --- a/tools/eslint-rules/rules-utils.js +++ b/tools/eslint-rules/rules-utils.js @@ -3,6 +3,11 @@ */ 'use strict'; +function isRequireCall(node) { + return node.callee.type === 'Identifier' && node.callee.name === 'require'; +} +module.exports.isRequireCall = isRequireCall; + module.exports.isDefiningError = function(node) { return node.expression && node.expression.type === 'CallExpression' && @@ -16,7 +21,7 @@ module.exports.isDefiningError = function(node) { * require calls. */ 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); }; @@ -26,7 +31,7 @@ module.exports.isRequired = function(node, modules) { */ const commonModuleRegExp = new RegExp(/^(\.\.\/)*common(\.js)?$/); module.exports.isCommonModule = function(node) { - return node.callee.name === 'require' && + return isRequireCall(node) && node.arguments.length !== 0 && commonModuleRegExp.test(node.arguments[0].value); };