Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(eslint-plugin): [no-shadow] exclude external type declaration merging #3959

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
60 changes: 58 additions & 2 deletions packages/eslint-plugin/src/rules/no-shadow.ts
Expand Up @@ -95,10 +95,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 @@ -225,6 +225,58 @@ 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 isStringLiteral(
idan-at marked this conversation as resolved.
Show resolved Hide resolved
node: TSESTree.Node | null,
): node is TSESTree.StringLiteral {
return (
!!node &&
node.type === AST_NODE_TYPES.Literal &&
idan-at marked this conversation as resolved.
Show resolved Hide resolved
typeof node.value === 'string'
);
}

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 isExternalDeclerationMerging(
idan-at marked this conversation as resolved.
Show resolved Hide resolved
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) &&
isStringLiteral(firstDefinition.parent.source) &&
idan-at marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -404,6 +456,10 @@ export default util.createRule<Options, MessageIds>({
continue;
}

if (isExternalDeclerationMerging(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
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,
},
],
},
],
});