Skip to content

Commit

Permalink
refactor(eslint-plugin): [no-shadow] use findVariable from utils (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelss95 authored Oct 3, 2021
1 parent 3c773e4 commit 35a8acf
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
ASTUtils,
AST_NODE_TYPES,
TSESLint,
TSESTree,
Expand Down Expand Up @@ -340,31 +341,6 @@ export default util.createRule<Options, MessageIds>({
);
}

/**
* Finds the variable by a given name in a given scope and its upper scopes.
* @param initScope A scope to start find.
* @param name A variable name to find.
* @returns A found variable or `null`.
*/
function getVariableByName(
initScope: TSESLint.Scope.Scope | null,
name: string,
): TSESLint.Scope.Variable | null {
let scope = initScope;

while (scope) {
const variable = scope.set.get(name);

if (variable) {
return variable;
}

scope = scope.upper;
}

return null;
}

/**
* Checks the current context for shadowed variables.
* @param {Scope} scope Fixme
Expand Down Expand Up @@ -404,7 +380,9 @@ export default util.createRule<Options, MessageIds>({
}

// Gets shadowed variable.
const shadowed = getVariableByName(scope.upper, variable.name);
const shadowed = scope.upper
? ASTUtils.findVariable(scope.upper, variable.name)
: null;
if (!shadowed) {
continue;
}
Expand Down

0 comments on commit 35a8acf

Please sign in to comment.