]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/global-require.js
2 * @fileoverview Rule for disallowing require() outside of the top-level module context
3 * @author Jamund Ferguson
4 * @deprecated in ESLint v7.0.0
9 const ACCEPTABLE_PARENTS
= [
10 "AssignmentExpression",
13 "ExpressionStatement",
15 "ConditionalExpression",
17 "VariableDeclaration",
22 * Finds the eslint-scope reference in the given scope.
23 * @param {Object} scope The scope to search.
24 * @param {ASTNode} node The identifier node.
25 * @returns {Reference|null} Returns the found reference or null if none were found.
27 function findReference(scope
, node
) {
28 const references
= scope
.references
.filter(reference
=> reference
.identifier
.range
[0] === node
.range
[0] &&
29 reference
.identifier
.range
[1] === node
.range
[1]);
31 /* istanbul ignore else: correctly returns null */
32 if (references
.length
=== 1) {
40 * Checks if the given identifier node is shadowed in the given scope.
41 * @param {Object} scope The current scope.
42 * @param {ASTNode} node The identifier node to check.
43 * @returns {boolean} Whether or not the name is shadowed.
45 function isShadowed(scope
, node
) {
46 const reference
= findReference(scope
, node
);
48 return reference
&& reference
.resolved
&& reference
.resolved
.defs
.length
> 0;
51 /** @type {import('../shared/types').Rule} */
61 description
: "require `require()` calls to be placed at top-level module scope",
63 url
: "https://eslint.org/docs/rules/global-require"
68 unexpected
: "Unexpected require()."
74 CallExpression(node
) {
75 const currentScope
= context
.getScope();
77 if (node
.callee
.name
=== "require" && !isShadowed(currentScope
, node
.callee
)) {
78 const isGoodRequire
= context
.getAncestors().every(parent
=> ACCEPTABLE_PARENTS
.indexOf(parent
.type
) > -1);
81 context
.report({ node
, messageId
: "unexpected" });