diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index aab60bc8163..9c77ca321fd 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -116,6 +116,25 @@ export default util.createRule({ ); } + /** + * 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. * @@ -233,6 +252,11 @@ export default util.createRule({ 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; diff --git a/packages/eslint-plugin/tests/rules/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow.test.ts index 1bf16fed28c..54f9851be84 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow.test.ts @@ -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: [ {