From cb1634af1c730d6e729dba1d0d4a01f71ef168a0 Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Mon, 20 Sep 2021 21:34:41 -0300 Subject: [PATCH] fixup! fix(eslint-plugin): [no-shadow] ignore type-only imports properly --- packages/eslint-plugin/src/rules/no-shadow.ts | 21 ++++++++++++++++--- .../tests/rules/no-shadow.test.ts | 16 ++++++++++++++ 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index 4c15d9e0f78a..ea27f87e0831 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -3,7 +3,12 @@ import { TSESLint, TSESTree, } from '@typescript-eslint/experimental-utils'; -import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager'; +import { + Definition, + DefinitionType, + ImportBindingDefinition, + ScopeType, +} from '@typescript-eslint/scope-manager'; import * as util from '../util'; type MessageIds = 'noShadow'; @@ -85,6 +90,15 @@ export default util.createRule({ return variable.defs[0].type === 'Parameter' && variable.name === 'this'; } + function isTypeImport( + definition: Definition, + ): definition is ImportBindingDefinition { + return ( + definition.type === DefinitionType.ImportBinding && + definition.parent.importKind === 'type' + ); + } + function isTypeValueShadow( variable: TSESLint.Scope.Variable, shadowed: TSESLint.Scope.Variable, @@ -98,10 +112,11 @@ export default util.createRule({ return false; } + const [firstDefinition] = shadowed.defs; const isShadowedValue = !('isValueVariable' in shadowed) || - (shadowed.defs[0]?.type !== DefinitionType.ImportBinding && - shadowed.isValueVariable); + !firstDefinition || + (!isTypeImport(firstDefinition) && shadowed.isValueVariable); return variable.isValueVariable !== isShadowedValue; } diff --git a/packages/eslint-plugin/tests/rules/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow.test.ts index 16c06cf89b35..c3e5a09f3441 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow.test.ts @@ -1426,5 +1426,21 @@ function doThing(foo: number, bar: number) {} }, ], }, + { + code: ` +import { foo } from './foo'; +function doThing(foo: number, bar: number) {} + `, + options: [{ ignoreTypeValueShadow: true }], + errors: [ + { + messageId: 'noShadow', + data: { name: 'foo' }, + type: AST_NODE_TYPES.Identifier, + line: 3, + column: 18, + }, + ], + }, ], });