From ff248211b1677c5342be59cd80a20b2c91e43a46 Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Sat, 11 Sep 2021 21:03:39 -0300 Subject: [PATCH] fix(eslint-plugin): [no-shadow] ignore type-only imports properly --- packages/eslint-plugin/src/rules/no-shadow.ts | 10 ++++--- .../tests/rules/no-shadow.test.ts | 30 ++++++++++++++++++- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index 6eb2ef38f7c8..4c15d9e0f78a 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -1,9 +1,9 @@ import { - TSESTree, - TSESLint, AST_NODE_TYPES, + TSESLint, + TSESTree, } from '@typescript-eslint/experimental-utils'; -import { ScopeType } from '@typescript-eslint/scope-manager'; +import { DefinitionType, ScopeType } from '@typescript-eslint/scope-manager'; import * as util from '../util'; type MessageIds = 'noShadow'; @@ -99,7 +99,9 @@ export default util.createRule({ } const isShadowedValue = - 'isValueVariable' in shadowed ? shadowed.isValueVariable : true; + !('isValueVariable' in shadowed) || + (shadowed.defs[0]?.type !== DefinitionType.ImportBinding && + 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 f48adfc1b57c..16c06cf89b35 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow.test.ts @@ -1,6 +1,6 @@ +import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; import rule from '../../src/rules/no-shadow'; import { RuleTester } from '../RuleTester'; -import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; const ruleTester = new RuleTester({ parserOptions: { @@ -172,6 +172,18 @@ export class Wrapper { } } `, + { + // https://github.com/typescript-eslint/typescript-eslint/issues/3862 + code: ` +import type { foo } from './foo'; +type bar = number; + +// 'foo' is already declared in the upper scope +// 'bar' is fine +function doThing(foo: number, bar: number) {} + `, + options: [{ ignoreTypeValueShadow: true }], + }, ], invalid: [ { @@ -1398,5 +1410,21 @@ function foo(cb) { }, ], }, + { + code: ` +import type { foo } from './foo'; +function doThing(foo: number, bar: number) {} + `, + options: [{ ignoreTypeValueShadow: false }], + errors: [ + { + messageId: 'noShadow', + data: { name: 'foo' }, + type: AST_NODE_TYPES.Identifier, + line: 3, + column: 18, + }, + ], + }, ], });