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

fix(eslint-plugin): [no-shadow] fix false-positive on enum declaration #2374

Merged
merged 1 commit into from Aug 8, 2020
Merged
Show file tree
Hide file tree
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
24 changes: 24 additions & 0 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Expand Up @@ -116,6 +116,25 @@ export default util.createRule<Options, MessageIds>({
);
}

/**
* Checks if a variable of the class name in the class scope of ClassDeclaration.
*
* ClassDeclaration creates two variables of its name into its outer scope and its class scope.
* So we should ignore the variable in the class scope.
* @param variable The variable to check.
* @returns Whether or not the variable of the class name in the class scope of ClassDeclaration.
*/
function isDuplicatedEnumNameVariable(
variable: TSESLint.Scope.Variable,
): boolean {
const block = variable.scope.block;

return (
block.type === AST_NODE_TYPES.TSEnumDeclaration &&
block.id === variable.identifiers[0]
);
}

/**
* Checks if a variable is inside the initializer of scopeVar.
*
Expand Down Expand Up @@ -233,6 +252,11 @@ export default util.createRule<Options, MessageIds>({
continue;
}

// ignore variables of a class name in the class scope of ClassDeclaration
if (isDuplicatedEnumNameVariable(variable)) {
continue;
}

// ignore configured allowed names
if (isAllowed(variable)) {
continue;
Expand Down
7 changes: 7 additions & 0 deletions packages/eslint-plugin/tests/rules/no-shadow.test.ts
Expand Up @@ -431,6 +431,13 @@ function foo(cb) {
`,
options: [{ allow: ['cb'] }],
},
// https://github.com/typescript-eslint/typescript-eslint/issues/2360
`
enum Direction {
left = 'left',
right = 'right',
}
`,
],
invalid: [
{
Expand Down