From f43424ac2d47c83fd276987b44006ade78811c90 Mon Sep 17 00:00:00 2001 From: Aviv Keller Date: Mon, 23 Sep 2024 16:23:05 -0400 Subject: [PATCH] tools: remove redudant code from eslint require rule PR-URL: https://github.com/nodejs/node/pull/54892 Reviewed-By: James M Snell Reviewed-By: Antoine du Hamel --- tools/eslint-rules/no-duplicate-requires.js | 57 ++++++++------------- 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/tools/eslint-rules/no-duplicate-requires.js b/tools/eslint-rules/no-duplicate-requires.js index fb4f36eed8a..413c0294b9b 100644 --- a/tools/eslint-rules/no-duplicate-requires.js +++ b/tools/eslint-rules/no-duplicate-requires.js @@ -1,6 +1,7 @@ /** * @fileoverview Ensure modules are not required twice at top level of a module * @author devsnek + * @author RedYetiDev */ 'use strict'; @@ -10,20 +11,18 @@ const { isRequireCall, isString } = require('./rules-utils.js'); // Rule Definition //------------------------------------------------------------------------------ -const secondLevelTypes = [ +const topLevelTypes = new Set([ 'FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression', 'ClassBody', 'MethodDefinition', -]; +]); -function isTopLevel(node) { - while (!secondLevelTypes.includes(node.type)) { +const isTopLevel = (node) => { + while (node) { + if (topLevelTypes.has(node.type)) return false; node = node.parent; - if (!node) { - return true; - } } - return false; -} + return true; +}; module.exports = { create(context) { @@ -31,37 +30,25 @@ module.exports = { return {}; } - function getRequiredModuleNameFromCall(node) { - // Node has arguments and first argument is string - if (node.arguments.length && isString(node.arguments[0])) { - return node.arguments[0].value.trim(); - } + const requiredModules = new Set(); - return undefined; - } - - const required = new Set(); - - const rules = { - CallExpression: (node) => { + return { + CallExpression(node) { if (isRequireCall(node) && isTopLevel(node)) { - const moduleName = getRequiredModuleNameFromCall(node); - if (moduleName === undefined) { - return; - } - if (required.has(moduleName)) { - context.report( - node, - '\'{{moduleName}}\' require is duplicated.', - { moduleName }, - ); - } else { - required.add(moduleName); + const [firstArg] = node.arguments; + if (isString(firstArg)) { + const moduleName = firstArg.value.trim(); + if (requiredModules.has(moduleName)) { + context.report({ + node, + message: `'${moduleName}' require is duplicated.`, + }); + } else { + requiredModules.add(moduleName); + } } } }, }; - - return rules; }, };