mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
af83b7963f
This commit makes isString() a reusable utility function for core's custom ESLint rules. PR-URL: https://github.com/nodejs/node/pull/27719 Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Masashi Hirano <shisama07@gmail.com> Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
65 lines
1.6 KiB
JavaScript
65 lines
1.6 KiB
JavaScript
/**
|
|
* @fileoverview Ensure modules are not required twice at top level of a module
|
|
* @author devsnek
|
|
*/
|
|
'use strict';
|
|
|
|
const { isRequireCall, isString } = require('./rules-utils.js');
|
|
|
|
//------------------------------------------------------------------------------
|
|
// Rule Definition
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
|
function isTopLevel(node) {
|
|
do {
|
|
if (node.type === 'FunctionDeclaration' ||
|
|
node.type === 'FunctionExpression' ||
|
|
node.type === 'ArrowFunctionExpression' ||
|
|
node.type === 'ClassBody' ||
|
|
node.type === 'MethodDefinition') {
|
|
return false;
|
|
}
|
|
} while (node = node.parent);
|
|
return true;
|
|
}
|
|
|
|
module.exports = (context) => {
|
|
if (context.parserOptions.sourceType === 'module') {
|
|
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();
|
|
}
|
|
|
|
return undefined;
|
|
}
|
|
|
|
const required = new Set();
|
|
|
|
const rules = {
|
|
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);
|
|
}
|
|
}
|
|
},
|
|
};
|
|
|
|
return rules;
|
|
};
|