Repository: eslint/eslint
File: lib/source-code/source-code.js (JavaScript)

Code before:
        }

        // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
        const inner = currentNode.type !== "Program";

        for (let node = currentNode; node; node = node.parent) {
            const scope = this.scopeManager.acquire(node, inner);

            if (scope) {
                if (scope.type === "function-expression-name") {
                    cache.set(currentNode, scope.childScopes[0]);
                    return scope.childScopes[0];
                }

                cache.set(currentNode, scope);
                return scope;
            }
        }

        cache.set(currentNode, this.scopeManager.scopes[0]);
        return this.scopeManager.scopes[0];
    }

    /**
     * Gets all of the declared variables in the scope represented
     * by `node`. This is a convenience method that passes through
     * to the same method on the `scopeManager`.
     * @param {ASTNode} node The node representing the scope to check.
     * @returns {Array<Variable>} An array of variable nodes representing
     *      the declared variables in the scope.
     */
    getDeclaredVariables(node) {
        return this.scopeManager.getDeclaredVariables(node);
    }

    /**
     * Gets all the ancestors of a given node
     * @param {ASTNode} node The node
     * @returns {Array<ASTNode>} All the ancestor nodes in the AST, not including the provided node, starting
     * from the root node at index 0 and going inwards to the parent node.
     */
    getAncestors(node) {

        const cache = this[caches].get("ancestors");
        const cachedAncestry = cache.get(node);

        /*
         * Important! The array returned from this method must be mutable
         * without side effects, meaning each rule must receive its own copy
         * of the array that it can mutate without affecting other rules.
         * Historically, this method calculated and returned a new array for

Review comment (suggestion):
```suggestion
     * Gets all of the variables declared by `node`.
     * This is a convenience method that passes through
```

I think "in the scope represented by node" would be confusing. For example, for function declarations, this returns variables for the function name and function parameters. The function name variable is not in the scope represented by node but in the parent scope. Also, variables declared at the top level of the function's body are directly in the scope represented by node, but this doesn't return them.

Code after:

        // On Program node, get the outermost scope to avoid return Node.js special function scope or ES modules scope.
        const inner = currentNode.type !== "Program";

        for (let node = currentNode; node; node = node.parent) {
            const scope = this.scopeManager.acquire(node, inner);

            if (scope) {
                if (scope.type === "function-expression-name") {
                    cache.set(currentNode, scope.childScopes[0]);
                    return scope.childScopes[0];
                }

                cache.set(currentNode, scope);
                return scope;
            }
        }

        cache.set(currentNode, this.scopeManager.scopes[0]);
        return this.scopeManager.scopes[0];
    }

    /**
     * Gets all of the declared variables in the scope associated
     * with `node`. 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.
     * @returns {Array<Variable>} An array of variable nodes representing
     *      the declared variables in the scope associated with `node`.
     */
    getDeclaredVariables(node) {
        return this.scopeManager.getDeclaredVariables(node);
    }

    /* eslint-disable class-methods-use-this -- node is owned by SourceCode */
    /**
     * Gets all the ancestors of a given node
     * @param {ASTNode} node The node
     * @returns {Array<ASTNode>} All the ancestor nodes in the AST, not including the provided node, starting
     * from the root node at index 0 and going inwards to the parent node.
     * @throws {TypeError} When `node` is missing.
     */
    getAncestors(node) {

        if (!node) {
            throw new TypeError("Missing required argument: node.");
        }

        const ancestorsStartingAtParent = [];

        for (let ancestor = node.parent; ancestor; ancestor = ancestor.parent) {