Skip to content

Commit 5ed389b

Browse files
committed
check if import collides with exported local name
1 parent 51e8f7d commit 5ed389b

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
@@ -15063,10 +15063,16 @@ namespace ts {
1506315063
const symbol = getSymbolOfNode(node);
1506415064
const target = resolveAlias(symbol);
1506515065
if (target !== unknownSymbol) {
15066+
// For external modules symbol represent local symbol for an alias.
15067+
// This local symbol will merge any other local declarations (excluding other aliases)
15068+
// and symbol.flags will contains combined representation for all merged declaration.
15069+
// Based on symbol.flags we can compute a set of excluded meanings (meaning that resolved alias should not have,
15070+
// otherwise it will conflict with some local declaration). Note that in addition to normal flags we include matching SymbolFlags.Export*
15071+
// in order to prevent collisions with declarations that were exported from the current module (they still contribute to local names).
1506615072
const excludedMeanings =
15067-
(symbol.flags & SymbolFlags.Value ? SymbolFlags.Value : 0) |
15068-
(symbol.flags & SymbolFlags.Type ? SymbolFlags.Type : 0) |
15069-
(symbol.flags & SymbolFlags.Namespace ? SymbolFlags.Namespace : 0);
15073+
(symbol.flags & (SymbolFlags.Value | SymbolFlags.ExportValue) ? SymbolFlags.Value : 0) |
15074+
(symbol.flags & (SymbolFlags.Type | SymbolFlags.ExportType) ? SymbolFlags.Type : 0) |
15075+
(symbol.flags & (SymbolFlags.Namespace | SymbolFlags.ExportNamespace) ? SymbolFlags.Namespace : 0);
1507015076
if (target.flags & excludedMeanings) {
1507115077
const message = node.kind === SyntaxKind.ExportSpecifier ?
1507215078
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)