Skip to content

Commit fac24ae

Browse files
authored
Export declarations exporting aliases make the namespace instantiated (microsoft#54816)
1 parent 9707336 commit fac24ae

File tree

5 files changed

+160
-1
lines changed

5 files changed

+160
-1
lines changed

src/compiler/binder.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ import {
313313
unusedLabelIsError,
314314
VariableDeclaration,
315315
WhileStatement,
316-
WithStatement,
316+
WithStatement
317317
} from "./_namespaces/ts";
318318
import * as performance from "./_namespaces/ts.performance";
319319

@@ -445,6 +445,14 @@ function getModuleInstanceStateForAliasTarget(specifier: ExportSpecifier, visite
445445
if (found === ModuleInstanceState.Instantiated) {
446446
return found;
447447
}
448+
if (statement.kind === SyntaxKind.ImportEqualsDeclaration) {
449+
// Treat re-exports of import aliases as instantiated,
450+
// since they're ambiguous. This is consistent with
451+
// `export import x = mod.x` being treated as instantiated:
452+
// import x = mod.x;
453+
// export { x };
454+
found = ModuleInstanceState.Instantiated;
455+
}
448456
}
449457
}
450458
if (found !== undefined) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/conformance/internalModules/moduleDeclarations/reExportAliasMakesInstantiated.ts] ////
2+
3+
//// [reExportAliasMakesInstantiated.ts]
4+
declare module pack1 {
5+
const test1: string;
6+
export { test1 };
7+
}
8+
declare module pack2 {
9+
import test1 = pack1.test1;
10+
export { test1 };
11+
}
12+
export import test1 = pack2.test1;
13+
14+
declare module mod1 {
15+
type test1 = string;
16+
export { test1 };
17+
}
18+
declare module mod2 {
19+
import test1 = mod1.test1;
20+
export { test1 };
21+
}
22+
const test2 = mod2; // Possible false positive instantiation, but ok
23+
24+
25+
//// [reExportAliasMakesInstantiated.js]
26+
"use strict";
27+
Object.defineProperty(exports, "__esModule", { value: true });
28+
exports.test1 = void 0;
29+
exports.test1 = pack2.test1;
30+
var test2 = mod2; // Possible false positive instantiation, but ok
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//// [tests/cases/conformance/internalModules/moduleDeclarations/reExportAliasMakesInstantiated.ts] ////
2+
3+
=== reExportAliasMakesInstantiated.ts ===
4+
declare module pack1 {
5+
>pack1 : Symbol(pack1, Decl(reExportAliasMakesInstantiated.ts, 0, 0))
6+
7+
const test1: string;
8+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 1, 7))
9+
10+
export { test1 };
11+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 2, 10))
12+
}
13+
declare module pack2 {
14+
>pack2 : Symbol(pack2, Decl(reExportAliasMakesInstantiated.ts, 3, 1))
15+
16+
import test1 = pack1.test1;
17+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 4, 22))
18+
>pack1 : Symbol(pack1, Decl(reExportAliasMakesInstantiated.ts, 0, 0))
19+
>test1 : Symbol(pack1.test1, Decl(reExportAliasMakesInstantiated.ts, 2, 10))
20+
21+
export { test1 };
22+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 6, 10))
23+
}
24+
export import test1 = pack2.test1;
25+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 7, 1))
26+
>pack2 : Symbol(pack2, Decl(reExportAliasMakesInstantiated.ts, 3, 1))
27+
>test1 : Symbol(pack2.test1, Decl(reExportAliasMakesInstantiated.ts, 6, 10))
28+
29+
declare module mod1 {
30+
>mod1 : Symbol(mod1, Decl(reExportAliasMakesInstantiated.ts, 8, 34))
31+
32+
type test1 = string;
33+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 10, 21))
34+
35+
export { test1 };
36+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 12, 10))
37+
}
38+
declare module mod2 {
39+
>mod2 : Symbol(mod2, Decl(reExportAliasMakesInstantiated.ts, 13, 1))
40+
41+
import test1 = mod1.test1;
42+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 14, 21))
43+
>mod1 : Symbol(mod1, Decl(reExportAliasMakesInstantiated.ts, 8, 34))
44+
>test1 : Symbol(mod1.test1, Decl(reExportAliasMakesInstantiated.ts, 12, 10))
45+
46+
export { test1 };
47+
>test1 : Symbol(test1, Decl(reExportAliasMakesInstantiated.ts, 16, 10))
48+
}
49+
const test2 = mod2; // Possible false positive instantiation, but ok
50+
>test2 : Symbol(test2, Decl(reExportAliasMakesInstantiated.ts, 18, 5))
51+
>mod2 : Symbol(mod2, Decl(reExportAliasMakesInstantiated.ts, 13, 1))
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [tests/cases/conformance/internalModules/moduleDeclarations/reExportAliasMakesInstantiated.ts] ////
2+
3+
=== reExportAliasMakesInstantiated.ts ===
4+
declare module pack1 {
5+
>pack1 : typeof pack1
6+
7+
const test1: string;
8+
>test1 : string
9+
10+
export { test1 };
11+
>test1 : string
12+
}
13+
declare module pack2 {
14+
>pack2 : typeof pack2
15+
16+
import test1 = pack1.test1;
17+
>test1 : string
18+
>pack1 : typeof pack1
19+
>test1 : string
20+
21+
export { test1 };
22+
>test1 : string
23+
}
24+
export import test1 = pack2.test1;
25+
>test1 : string
26+
>pack2 : typeof pack2
27+
>test1 : string
28+
29+
declare module mod1 {
30+
type test1 = string;
31+
>test1 : string
32+
33+
export { test1 };
34+
>test1 : any
35+
}
36+
declare module mod2 {
37+
>mod2 : typeof mod2
38+
39+
import test1 = mod1.test1;
40+
>test1 : any
41+
>mod1 : any
42+
>test1 : string
43+
44+
export { test1 };
45+
>test1 : any
46+
}
47+
const test2 = mod2; // Possible false positive instantiation, but ok
48+
>test2 : typeof mod2
49+
>mod2 : typeof mod2
50+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
declare module pack1 {
2+
const test1: string;
3+
export { test1 };
4+
}
5+
declare module pack2 {
6+
import test1 = pack1.test1;
7+
export { test1 };
8+
}
9+
export import test1 = pack2.test1;
10+
11+
declare module mod1 {
12+
type test1 = string;
13+
export { test1 };
14+
}
15+
declare module mod2 {
16+
import test1 = mod1.test1;
17+
export { test1 };
18+
}
19+
const test2 = mod2; // Possible false positive instantiation, but ok

0 commit comments

Comments
 (0)