Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 47 additions & 2 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ export default util.createRule<Options, MessageIds>({
}

function isTypeImport(
definition: Definition,
definition?: Definition,
): definition is ImportBindingDefinition {
return (
definition.type === DefinitionType.ImportBinding &&
definition?.type === DefinitionType.ImportBinding &&
definition.parent.importKind === 'type'
);
}
Expand Down Expand Up @@ -224,6 +224,47 @@ export default util.createRule<Options, MessageIds>({
);
}

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.
Expand Down Expand Up @@ -403,6 +444,10 @@ export default util.createRule<Options, MessageIds>({
continue;
}

if (isExternalDeclarationMerging(scope, variable, shadowed)) {
continue;
}

const isESLintGlobal = 'writeable' in shadowed;
if (
(shadowed.identifiers.length > 0 ||
Expand Down
87 changes: 87 additions & 0 deletions packages/eslint-plugin/tests/rules/no-shadow.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
},
],
},
],
});