2024-08-09 12:04:48 +00:00
|
|
|
/**
|
|
|
|
* @fileoverview Rule for disallowing require() outside of the top-level module context
|
|
|
|
* @author Jamund Ferguson
|
2024-08-21 06:34:30 +00:00
|
|
|
* @deprecated in ESLint v7.0.0
|
2024-08-09 12:04:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
"use strict";
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
const ACCEPTABLE_PARENTS = new Set([
|
2024-08-09 12:04:48 +00:00
|
|
|
"AssignmentExpression",
|
|
|
|
"VariableDeclarator",
|
|
|
|
"MemberExpression",
|
|
|
|
"ExpressionStatement",
|
|
|
|
"CallExpression",
|
|
|
|
"ConditionalExpression",
|
|
|
|
"Program",
|
|
|
|
"VariableDeclaration",
|
|
|
|
"ChainExpression"
|
2024-08-21 06:34:30 +00:00
|
|
|
]);
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Finds the eslint-scope reference in the given scope.
|
|
|
|
* @param {Object} scope The scope to search.
|
|
|
|
* @param {ASTNode} node The identifier node.
|
|
|
|
* @returns {Reference|null} Returns the found reference or null if none were found.
|
|
|
|
*/
|
|
|
|
function findReference(scope, node) {
|
|
|
|
const references = scope.references.filter(reference => reference.identifier.range[0] === node.range[0] &&
|
|
|
|
reference.identifier.range[1] === node.range[1]);
|
|
|
|
|
|
|
|
if (references.length === 1) {
|
|
|
|
return references[0];
|
|
|
|
}
|
2024-08-21 06:34:30 +00:00
|
|
|
|
|
|
|
/* c8 ignore next */
|
2024-08-09 12:04:48 +00:00
|
|
|
return null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given identifier node is shadowed in the given scope.
|
|
|
|
* @param {Object} scope The current scope.
|
|
|
|
* @param {ASTNode} node The identifier node to check.
|
|
|
|
* @returns {boolean} Whether or not the name is shadowed.
|
|
|
|
*/
|
|
|
|
function isShadowed(scope, node) {
|
|
|
|
const reference = findReference(scope, node);
|
|
|
|
|
|
|
|
return reference && reference.resolved && reference.resolved.defs.length > 0;
|
|
|
|
}
|
|
|
|
|
2024-08-21 06:34:30 +00:00
|
|
|
/** @type {import('../shared/types').Rule} */
|
2024-08-09 12:04:48 +00:00
|
|
|
module.exports = {
|
|
|
|
meta: {
|
|
|
|
deprecated: true,
|
|
|
|
|
|
|
|
replacedBy: [],
|
|
|
|
|
|
|
|
type: "suggestion",
|
|
|
|
|
|
|
|
docs: {
|
2024-08-21 06:34:30 +00:00
|
|
|
description: "Require `require()` calls to be placed at top-level module scope",
|
2024-08-09 12:04:48 +00:00
|
|
|
recommended: false,
|
2024-08-21 06:34:30 +00:00
|
|
|
url: "https://eslint.org/docs/latest/rules/global-require"
|
2024-08-09 12:04:48 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
schema: [],
|
|
|
|
messages: {
|
|
|
|
unexpected: "Unexpected require()."
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
create(context) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const sourceCode = context.sourceCode;
|
|
|
|
|
2024-08-09 12:04:48 +00:00
|
|
|
return {
|
|
|
|
CallExpression(node) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const currentScope = sourceCode.getScope(node);
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
if (node.callee.name === "require" && !isShadowed(currentScope, node.callee)) {
|
2024-08-21 06:34:30 +00:00
|
|
|
const isGoodRequire = sourceCode.getAncestors(node).every(parent => ACCEPTABLE_PARENTS.has(parent.type));
|
2024-08-09 12:04:48 +00:00
|
|
|
|
|
|
|
if (!isGoodRequire) {
|
|
|
|
context.report({ node, messageId: "unexpected" });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|