diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index dfba42cf301..68214ddd338 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -98,7 +98,9 @@ export default util.createRule({ ): definition is ImportBindingDefinition { return ( definition?.type === DefinitionType.ImportBinding && - definition.parent.importKind === 'type' + (definition.parent.importKind === 'type' || + (definition.node.type === AST_NODE_TYPES.ImportSpecifier && + definition.node.importKind === 'type')) ); } diff --git a/packages/eslint-plugin/tests/rules/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow.test.ts index bedb825df70..929ddf9d389 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow.test.ts @@ -193,6 +193,15 @@ function doThing(foo: number, bar: number) {} `, options: [{ ignoreTypeValueShadow: true }], }, + { + code: ` +import { type foo } from './foo'; + +// 'foo' is already declared in the upper scope +function doThing(foo: number) {} + `, + options: [{ ignoreTypeValueShadow: true }], + }, ], invalid: [ { @@ -1422,7 +1431,23 @@ function foo(cb) { { code: ` import type { foo } from './foo'; -function doThing(foo: number, bar: number) {} +function doThing(foo: number) {} + `, + options: [{ ignoreTypeValueShadow: false }], + errors: [ + { + messageId: 'noShadow', + data: { name: 'foo' }, + type: AST_NODE_TYPES.Identifier, + line: 3, + column: 18, + }, + ], + }, + { + code: ` +import { type foo } from './foo'; +function doThing(foo: number) {} `, options: [{ ignoreTypeValueShadow: false }], errors: [ @@ -1513,6 +1538,64 @@ declare module 'bar' { code: ` import type { Foo } from 'bar'; +declare module 'bar' { + interface Foo { + x: string; + } +} + `, + errors: [ + { + messageId: 'noShadow', + data: { name: 'Foo' }, + type: AST_NODE_TYPES.Identifier, + line: 5, + column: 13, + }, + ], + }, + { + code: ` +import { type Foo } from 'bar'; + +declare module 'baz' { + export interface Foo { + x: string; + } +} + `, + errors: [ + { + messageId: 'noShadow', + data: { name: 'Foo' }, + type: AST_NODE_TYPES.Identifier, + line: 5, + column: 20, + }, + ], + }, + { + code: ` +import { type Foo } from 'bar'; + +declare module 'bar' { + export type Foo = string; +} + `, + errors: [ + { + messageId: 'noShadow', + data: { name: 'Foo' }, + type: AST_NODE_TYPES.Identifier, + line: 5, + column: 15, + }, + ], + }, + { + code: ` +import { type Foo } from 'bar'; + declare module 'bar' { interface Foo { x: string;