Skip to content

Commit

Permalink
Chore: remove undocumented Linter#getScope method (#9253)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Sep 8, 2017
1 parent 5d7eb81 commit 3693e4e
Showing 1 changed file with 39 additions and 46 deletions.
85 changes: 39 additions & 46 deletions lib/linter.js
Expand Up @@ -594,6 +594,43 @@ function parse(text, providedParserOptions, parserName, filePath) {
}
}

/**
* Gets the scope for the current node
* @param {ScopeManager} scopeManager The scope manager for this AST
* @param {ASTNode} currentNode The node to get the scope of
* @param {number} ecmaVersion The `ecmaVersion` setting that this code was parsed with
* @returns {eslint-scope.Scope} The scope information for this node
*/
function getScope(scopeManager, currentNode, ecmaVersion) {
let initialNode;

// if current node introduces a scope, add it to the list
if (
["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"].indexOf(currentNode.type) >= 0 ||
ecmaVersion >= 6 && ["BlockStatement", "SwitchStatement", "CatchClause"].indexOf(currentNode.type) >= 0
) {
initialNode = currentNode;
} else {
initialNode = currentNode.parent;
}

// Ascend the current node's parents
for (let node = initialNode; node; node = node.parent) {

// Get the innermost scope
const scope = scopeManager.acquire(node, true);

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

return scopeManager.scopes[0];
}

// methods that exist on SourceCode object
const DEPRECATED_SOURCECODE_PASSTHROUGHS = {
getSource: "getText",
Expand Down Expand Up @@ -788,7 +825,7 @@ module.exports = class Linter {
getAncestors: () => this.traverser.parents(),
getDeclaredVariables: this.getDeclaredVariables.bind(this),
getFilename: () => filename,
getScope: this.getScope.bind(this),
getScope: () => getScope(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions.ecmaVersion),
getSourceCode: () => sourceCode,
markVariableAsUsed: this.markVariableAsUsed.bind(this),
parserOptions: config.parserOptions,
Expand Down Expand Up @@ -942,50 +979,6 @@ module.exports = class Linter {
return this.sourceCode;
}

/**
* Gets the scope for the current node.
* @returns {Object} An object representing the current node's scope.
*/
getScope() {
const parents = this.traverser.parents();

// Don't do this for Program nodes - they have no parents
if (parents.length) {

// if current node introduces a scope, add it to the list
const current = this.traverser.current();

if (this.currentConfig.parserOptions.ecmaVersion >= 6) {
if (["BlockStatement", "SwitchStatement", "CatchClause", "FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"].indexOf(current.type) >= 0) {
parents.push(current);
}
} else {
if (["FunctionDeclaration", "FunctionExpression", "ArrowFunctionExpression"].indexOf(current.type) >= 0) {
parents.push(current);
}
}

// Ascend the current node's parents
for (let i = parents.length - 1; i >= 0; --i) {

// Get the innermost scope
const scope = this.scopeManager.acquire(parents[i], true);

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

}

}

}

return this.scopeManager.scopes[0];
}

/**
* Record that a particular variable has been used in code
* @param {string} name The name of the variable to mark as used
Expand All @@ -995,7 +988,7 @@ module.exports = class Linter {
markVariableAsUsed(name) {
const hasGlobalReturn = this.currentConfig.parserOptions.ecmaFeatures && this.currentConfig.parserOptions.ecmaFeatures.globalReturn,
specialScope = hasGlobalReturn || this.currentConfig.parserOptions.sourceType === "module";
let scope = this.getScope(),
let scope = getScope(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions.ecmaVersion),
i,
len;

Expand Down

0 comments on commit 3693e4e

Please sign in to comment.