diff options
author | Node.js GitHub Bot <github-bot@iojs.org> | 2023-04-30 00:27:12 +0000 |
---|---|---|
committer | Node.js GitHub Bot <github-bot@iojs.org> | 2023-05-02 00:48:22 +0000 |
commit | 32778b8d0e1dc5464878728ab55fef16c9f77fb5 (patch) | |
tree | f4a4f6303bdf4992f0e65e8c730b7a57bc5a4283 /tools/node_modules/eslint/lib/source-code/source-code.js | |
parent | 9658d84dddd3835a61e3f270928f3480abadb5ca (diff) | |
download | node-new-32778b8d0e1dc5464878728ab55fef16c9f77fb5.tar.gz |
tools: update eslint to 8.39.0
PR-URL: https://github.com/nodejs/node/pull/47789
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Mestery <mestery@protonmail.com>
Diffstat (limited to 'tools/node_modules/eslint/lib/source-code/source-code.js')
-rw-r--r-- | tools/node_modules/eslint/lib/source-code/source-code.js | 51 |
1 files changed, 47 insertions, 4 deletions
diff --git a/tools/node_modules/eslint/lib/source-code/source-code.js b/tools/node_modules/eslint/lib/source-code/source-code.js index abaefa8999..07c0d29482 100644 --- a/tools/node_modules/eslint/lib/source-code/source-code.js +++ b/tools/node_modules/eslint/lib/source-code/source-code.js @@ -646,12 +646,12 @@ class SourceCode extends TokenStore { } /** - * Gets all of the declared variables in the scope associated - * with `node`. This is a convenience method that passes through + * Get the variables that `node` defines. + * This is a convenience method that passes through * to the same method on the `scopeManager`. - * @param {ASTNode} node The node from which to retrieve the scope to check. + * @param {ASTNode} node The node for which the variables are obtained. * @returns {Array<Variable>} An array of variable nodes representing - * the declared variables in the scope associated with `node`. + * the variables that `node` defines. */ getDeclaredVariables(node) { return this.scopeManager.getDeclaredVariables(node); @@ -681,6 +681,49 @@ class SourceCode extends TokenStore { } /* eslint-enable class-methods-use-this -- node is owned by SourceCode */ + /** + * Marks a variable as used in the current scope + * @param {string} name The name of the variable to mark as used. + * @param {ASTNode} [refNode] The closest node to the variable reference. + * @returns {boolean} True if the variable was found and marked as used, false if not. + */ + markVariableAsUsed(name, refNode = this.ast) { + + const currentScope = this.getScope(refNode); + let initialScope = currentScope; + + /* + * When we are in an ESM or CommonJS module, we need to start searching + * from the top-level scope, not the global scope. For ESM the top-level + * scope is the module scope; for CommonJS the top-level scope is the + * outer function scope. + * + * Without this check, we might miss a variable declared with `var` at + * the top-level because it won't exist in the global scope. + */ + if ( + currentScope.type === "global" && + currentScope.childScopes.length > 0 && + + // top-level scopes refer to a `Program` node + currentScope.childScopes[0].block === this.ast + ) { + initialScope = currentScope.childScopes[0]; + } + + for (let scope = initialScope; scope; scope = scope.upper) { + const variable = scope.variables.find(scopeVar => scopeVar.name === name); + + if (variable) { + variable.eslintUsed = true; + return true; + } + } + + return false; + } + + } module.exports = SourceCode; |