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

Prevent collision of imported type with exported declarations in current module #31231

Merged
merged 2 commits into from
Sep 27, 2019
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
3 changes: 2 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30078,7 +30078,7 @@ namespace ts {
}

function checkAliasSymbol(node: ImportEqualsDeclaration | ImportClause | NamespaceImport | ImportSpecifier | ExportSpecifier) {
const symbol = getSymbolOfNode(node);
let symbol = getSymbolOfNode(node);
const target = resolveAlias(symbol);

const shouldSkipWithJSExpandoTargets = symbol.flags & SymbolFlags.Assignment;
Expand All @@ -30089,6 +30089,7 @@ namespace ts {
// Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have,
// otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export*
// in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names).
symbol = getMergedSymbol(symbol.exportSymbol || symbol);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to fill in some historical context here: I was curious why this was needed when #7583 and #7591 only deal with the flags on the local symbol. Dug around a bit and found #16766. If you look at the changes in the binder there, information about the export used to be attached to the local symbol when it was exporting a type, namespace, or value, but #16766 removed that info for types and namespaces. So, the info we need for this function here used to exist on the local symbol, but now we have to go to the export symbol to get it. This implementation appears correct (although I wonder if #16766 would have been done differently if the flags had been used here).

const excludedMeanings =
(symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue) ? SymbolFlags.Value : 0) |
(symbol.flags & SymbolFlags.Type ? SymbolFlags.Type : 0) |
Expand Down
13 changes: 13 additions & 0 deletions tests/baselines/reference/mergeWithImportedNamespace.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
tests/cases/compiler/f2.ts(1,9): error TS2440: Import declaration conflicts with local declaration of 'N'.


==== tests/cases/compiler/f1.ts (0 errors) ====
export namespace N { export var x = 1; }

==== tests/cases/compiler/f2.ts (1 errors) ====
import {N} from "./f1";
~
!!! error TS2440: Import declaration conflicts with local declaration of 'N'.
export namespace N {
export interface I {x: any}
}
1 change: 0 additions & 1 deletion tests/baselines/reference/mergeWithImportedNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export namespace N { export var x = 1; }

//// [f2.ts]
import {N} from "./f1";
// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export namespace N {
export interface I {x: any}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/baselines/reference/mergeWithImportedNamespace.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ export namespace N { export var x = 1; }
import {N} from "./f1";
>N : Symbol(N, Decl(f2.ts, 0, 8), Decl(f2.ts, 0, 23))

// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export namespace N {
>N : Symbol(N, Decl(f2.ts, 0, 23))

export interface I {x: any}
>I : Symbol(I, Decl(f2.ts, 2, 20))
>x : Symbol(I.x, Decl(f2.ts, 3, 24))
>I : Symbol(I, Decl(f2.ts, 1, 20))
>x : Symbol(I.x, Decl(f2.ts, 2, 24))
}
1 change: 0 additions & 1 deletion tests/baselines/reference/mergeWithImportedNamespace.types
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export namespace N { export var x = 1; }
import {N} from "./f1";
>N : typeof N

// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export namespace N {
export interface I {x: any}
>x : any
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/mergeWithImportedType.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
tests/cases/compiler/f2.ts(1,9): error TS2440: Import declaration conflicts with local declaration of 'E'.


==== tests/cases/compiler/f1.ts (0 errors) ====
export enum E {X}

==== tests/cases/compiler/f2.ts (1 errors) ====
import {E} from "./f1";
~
!!! error TS2440: Import declaration conflicts with local declaration of 'E'.
export type E = E;
1 change: 0 additions & 1 deletion tests/baselines/reference/mergeWithImportedType.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export enum E {X}

//// [f2.ts]
import {E} from "./f1";
// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export type E = E;

//// [f1.js]
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/mergeWithImportedType.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export enum E {X}
import {E} from "./f1";
>E : Symbol(E, Decl(f2.ts, 0, 8), Decl(f2.ts, 0, 23))

// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export type E = E;
>E : Symbol(E, Decl(f2.ts, 0, 23))
>E : Symbol(E, Decl(f2.ts, 0, 8), Decl(f2.ts, 0, 23))
Expand Down
1 change: 0 additions & 1 deletion tests/baselines/reference/mergeWithImportedType.types
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export enum E {X}
import {E} from "./f1";
>E : typeof E

// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export type E = E;
>E : E

1 change: 0 additions & 1 deletion tests/cases/compiler/mergeWithImportedNamespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export namespace N { export var x = 1; }

// @filename: f2.ts
import {N} from "./f1";
// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export namespace N {
export interface I {x: any}
}
1 change: 0 additions & 1 deletion tests/cases/compiler/mergeWithImportedType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@ export enum E {X}

// @filename: f2.ts
import {E} from "./f1";
// partial revert of https://github.com/Microsoft/TypeScript/pull/7583 to prevent breaking changes
export type E = E;