diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index 27c689309310..dfba42cf301e 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -94,10 +94,10 @@ export default util.createRule({ } function isTypeImport( - definition: Definition, + definition?: Definition, ): definition is ImportBindingDefinition { return ( - definition.type === DefinitionType.ImportBinding && + definition?.type === DefinitionType.ImportBinding && definition.parent.importKind === 'type' ); } @@ -224,6 +224,47 @@ export default util.createRule({ ); } + function isImportDeclaration( + definition: + | TSESTree.ImportDeclaration + | TSESTree.TSImportEqualsDeclaration, + ): definition is TSESTree.ImportDeclaration { + return definition.type === AST_NODE_TYPES.ImportDeclaration; + } + + function isExternalModuleDeclarationWithName( + scope: TSESLint.Scope.Scope, + name: string, + ): boolean { + return ( + scope.type === ScopeType.tsModule && + scope.block.type === AST_NODE_TYPES.TSModuleDeclaration && + scope.block.id.type === AST_NODE_TYPES.Literal && + scope.block.id.value === name + ); + } + + function isExternalDeclarationMerging( + scope: TSESLint.Scope.Scope, + variable: TSESLint.Scope.Variable, + shadowed: TSESLint.Scope.Variable, + ): boolean { + const [firstDefinition] = shadowed.defs; + const [secondDefinition] = variable.defs; + + return ( + isTypeImport(firstDefinition) && + isImportDeclaration(firstDefinition.parent) && + isExternalModuleDeclarationWithName( + scope, + firstDefinition.parent.source.value, + ) && + secondDefinition.node.type === AST_NODE_TYPES.TSInterfaceDeclaration && + secondDefinition.node.parent?.type === + AST_NODE_TYPES.ExportNamedDeclaration + ); + } + /** * Check if variable name is allowed. * @param variable The variable to check. @@ -403,6 +444,10 @@ export default util.createRule({ continue; } + if (isExternalDeclarationMerging(scope, variable, shadowed)) { + continue; + } + const isESLintGlobal = 'writeable' in shadowed; if ( (shadowed.identifiers.length > 0 || diff --git a/packages/eslint-plugin/tests/rules/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow.test.ts index c3e5a09f3441..bedb825df704 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow.test.ts @@ -51,6 +51,15 @@ class Foo { } interface Foo { prop2: string; +} + `, + ` +import type { Foo } from 'bar'; + +declare module 'bar' { + export interface Foo { + x: string; + } } `, // type value shadowing @@ -1442,5 +1451,83 @@ function doThing(foo: number, bar: number) {} }, ], }, + { + code: ` +interface Foo {} + +declare module 'bar' { + 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 '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; + } +} + `, + errors: [ + { + messageId: 'noShadow', + data: { name: 'Foo' }, + type: AST_NODE_TYPES.Identifier, + line: 5, + column: 13, + }, + ], + }, ], }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy