Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule for disallowing require() outside of the top-level module context |
| 3 | * @author Jamund Ferguson |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame^] | 4 | * @deprecated in ESLint v7.0.0 |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | "use strict"; |
| 8 | |
| 9 | const ACCEPTABLE_PARENTS = [ |
| 10 | "AssignmentExpression", |
| 11 | "VariableDeclarator", |
| 12 | "MemberExpression", |
| 13 | "ExpressionStatement", |
| 14 | "CallExpression", |
| 15 | "ConditionalExpression", |
| 16 | "Program", |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 17 | "VariableDeclaration", |
| 18 | "ChainExpression" |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 19 | ]; |
| 20 | |
| 21 | /** |
| 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. |
| 26 | */ |
| 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]); |
| 30 | |
| 31 | /* istanbul ignore else: correctly returns null */ |
| 32 | if (references.length === 1) { |
| 33 | return references[0]; |
| 34 | } |
| 35 | return null; |
| 36 | |
| 37 | } |
| 38 | |
| 39 | /** |
| 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. |
| 44 | */ |
| 45 | function isShadowed(scope, node) { |
| 46 | const reference = findReference(scope, node); |
| 47 | |
| 48 | return reference && reference.resolved && reference.resolved.defs.length > 0; |
| 49 | } |
| 50 | |
| 51 | module.exports = { |
| 52 | meta: { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 53 | deprecated: true, |
| 54 | |
| 55 | replacedBy: [], |
| 56 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 57 | type: "suggestion", |
| 58 | |
| 59 | docs: { |
| 60 | description: "require `require()` calls to be placed at top-level module scope", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 61 | recommended: false, |
| 62 | url: "https://eslint.org/docs/rules/global-require" |
| 63 | }, |
| 64 | |
| 65 | schema: [], |
| 66 | messages: { |
| 67 | unexpected: "Unexpected require()." |
| 68 | } |
| 69 | }, |
| 70 | |
| 71 | create(context) { |
| 72 | return { |
| 73 | CallExpression(node) { |
| 74 | const currentScope = context.getScope(); |
| 75 | |
| 76 | if (node.callee.name === "require" && !isShadowed(currentScope, node.callee)) { |
| 77 | const isGoodRequire = context.getAncestors().every(parent => ACCEPTABLE_PARENTS.indexOf(parent.type) > -1); |
| 78 | |
| 79 | if (!isGoodRequire) { |
| 80 | context.report({ node, messageId: "unexpected" }); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | }; |
| 85 | } |
| 86 | }; |