Skip to content

Commit

Permalink
Chore: remove undocumented Linter#markVariableAsUsed method (refs #9161
Browse files Browse the repository at this point in the history
…) (#9266)
  • Loading branch information
not-an-aardvark committed Sep 9, 2017
1 parent 09414cf commit 88d5d4d
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions lib/linter.js
Expand Up @@ -631,6 +631,34 @@ function getScope(scopeManager, currentNode, ecmaVersion) {
return scopeManager.scopes[0];
}

/**
* Marks a variable as used in the current scope
* @param {ScopeManager} scopeManager The scope manager for this AST. The scope may be mutated by this function.
* @param {ASTNode} currentNode The node currently being traversed
* @param {Object} parserOptions The options used to parse this text
* @param {string} name The name of the variable that should be marked as used.
* @returns {boolean} True if the variable was found and marked as used, false if not.
*/
function markVariableAsUsed(scopeManager, currentNode, parserOptions, name) {
const hasGlobalReturn = parserOptions.ecmaFeatures && parserOptions.ecmaFeatures.globalReturn;
const specialScope = hasGlobalReturn || parserOptions.sourceType === "module";
const currentScope = getScope(scopeManager, currentNode, parserOptions.ecmaVersion);

// Special Node.js scope means we need to start one level deeper
const initialScope = currentScope.type === "global" && specialScope ? currentScope.childScopes[0] : currentScope;

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;
}

// methods that exist on SourceCode object
const DEPRECATED_SOURCECODE_PASSTHROUGHS = {
getSource: "getText",
Expand Down Expand Up @@ -827,7 +855,7 @@ module.exports = class Linter {
getFilename: () => filename,
getScope: () => getScope(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions.ecmaVersion),
getSourceCode: () => sourceCode,
markVariableAsUsed: this.markVariableAsUsed.bind(this),
markVariableAsUsed: name => markVariableAsUsed(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions, name),
parserOptions: config.parserOptions,
parserPath: config.parser,
parserServices,
Expand Down Expand Up @@ -979,36 +1007,6 @@ module.exports = class Linter {
return this.sourceCode;
}

/**
* Record that a particular variable has been used in code
* @param {string} name The name of the variable to mark as used
* @returns {boolean} True if the variable was found and marked as used,
* false if not.
*/
markVariableAsUsed(name) {
const hasGlobalReturn = this.currentConfig.parserOptions.ecmaFeatures && this.currentConfig.parserOptions.ecmaFeatures.globalReturn,
specialScope = hasGlobalReturn || this.currentConfig.parserOptions.sourceType === "module";
let scope = getScope(this.scopeManager, this.traverser.current(), this.currentConfig.parserOptions.ecmaVersion);

// Special Node.js scope means we need to start one level deeper
if (scope.type === "global" && specialScope) {
scope = scope.childScopes[0];
}

do {
const variables = scope.variables;

for (let i = 0; i < variables.length; i++) {
if (variables[i].name === name) {
variables[i].eslintUsed = true;
return true;
}
}
} while ((scope = scope.upper));

return false;
}

/**
* Defines a new linting rule.
* @param {string} ruleId A unique rule identifier
Expand Down

0 comments on commit 88d5d4d

Please sign in to comment.