diff options
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; |