Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(eslint-plugin): [no-shadow] use findVariable from utils #3921

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
30 changes: 4 additions & 26 deletions packages/eslint-plugin/src/rules/no-shadow.ts
@@ -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