Skip to content

Commit 32178ac

Browse files
committed
Merge pull request #7583 from Microsoft/colliding-local-import
check if import collides with exported local name
2 parents 279fec7 + 5ed389b commit 32178ac

File tree

4 files changed

+52
-3
lines changed

4 files changed

+52
-3
lines changed

src/compiler/checker.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15077,10 +15077,16 @@ namespace ts {
1507715077
const symbol = getSymbolOfNode(node);
1507815078
const target = resolveAlias(symbol);
1507915079
if (target !== unknownSymbol) {
15080+
// For external modules symbol represent local symbol for an alias.
15081+
// This local symbol will merge any other local declarations (excluding other aliases)
15082+
// and symbol.flags will contains combined representation for all merged declaration.
15083+
// Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have,
15084+
// otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export*
15085+
// in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names).
1508015086
const excludedMeanings =
15081-
(symbol.flags & SymbolFlags.Value ? SymbolFlags.Value : 0) |
15082-
(symbol.flags & SymbolFlags.Type ? SymbolFlags.Type : 0) |
15083-
(symbol.flags & SymbolFlags.Namespace ? SymbolFlags.Namespace : 0);
15087+
(symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue) ? SymbolFlags.Value : 0) |
15088+
(symbol.flags & (SymbolFlags.Type | SymbolFlags.ExportType) ? SymbolFlags.Type : 0) |
15089+
(symbol.flags & (SymbolFlags.Namespace | SymbolFlags.ExportNamespace) ? SymbolFlags.Namespace : 0);
1508415090
if (target.flags & excludedMeanings) {
1508515091
const message = node.kind === SyntaxKind.ExportSpecifier ?
1508615092
Diagnostics.Export_declaration_conflicts_with_exported_declaration_of_0 :
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
tests/cases/compiler/f2.ts(1,9): error TS2440: Import declaration conflicts with local declaration of 'f'
2+
3+
4+
==== tests/cases/compiler/f1.ts (0 errors) ====
5+
export function f() {
6+
}
7+
8+
==== tests/cases/compiler/f2.ts (1 errors) ====
9+
import {f} from './f1';
10+
~
11+
!!! error TS2440: Import declaration conflicts with local declaration of 'f'
12+
export function f() {
13+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/functionAndImportNameConflict.ts] ////
2+
3+
//// [f1.ts]
4+
export function f() {
5+
}
6+
7+
//// [f2.ts]
8+
import {f} from './f1';
9+
export function f() {
10+
}
11+
12+
//// [f1.js]
13+
"use strict";
14+
function f() {
15+
}
16+
exports.f = f;
17+
//// [f2.js]
18+
"use strict";
19+
function f() {
20+
}
21+
exports.f = f;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// @module: commonjs
2+
// @filename: f1.ts
3+
export function f() {
4+
}
5+
6+
// @filename: f2.ts
7+
import {f} from './f1';
8+
export function f() {
9+
}

0 commit comments

Comments
 (0)