mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
tools: remove redudant code from eslint require rule
PR-URL: https://github.com/nodejs/node/pull/54892 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
This commit is contained in:
parent
f20c42e964
commit
f43424ac2d
@ -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;
|
||||
},
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user