From 03d43539f93be2c401f57d7eac87a5ae933ef905 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Mon, 13 May 2019 14:50:55 -0400 Subject: [PATCH] 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 Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott Reviewed-By: Yongsheng Zhang --- tools/eslint-rules/no-duplicate-requires.js | 6 ++---- tools/eslint-rules/require-common-first.js | 10 +--------- tools/eslint-rules/required-modules.js | 11 ++--------- tools/eslint-rules/rules-utils.js | 9 +++++++-- 4 files changed, 12 insertions(+), 24 deletions(-) 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); };