Skip to content

Commit

Permalink
fix(eslint-plugin): [no-shadow] fix false-positive on enum declaration (
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Aug 29, 2020
1 parent a9cd6fb commit 9de669f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
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

0 comments on commit 9de669f

Please sign in to comment.