Skip to content

Commit ceaeffa

Browse files
authored
Fix declaration emit for imported export alias specifiers (microsoft#19852)
* Badness * Revert microsoft#3641, whose original bug has been fixed by other means * Add another repro
1 parent c6fddba commit ceaeffa

17 files changed

+254
-31
lines changed

src/compiler/checker.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,6 @@ namespace ts {
21612161
return forEachEntry(symbols, symbolFromSymbolTable => {
21622162
if (symbolFromSymbolTable.flags & SymbolFlags.Alias
21632163
&& symbolFromSymbolTable.escapedName !== "export="
2164-
&& !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier)
21652164
&& !(isUMDExportSymbol(symbolFromSymbolTable) && enclosingDeclaration && isExternalModule(getSourceFileOfNode(enclosingDeclaration)))
21662165
// If `!useOnlyExternalAliasing`, we can use any type of alias to get the name
21672166
&& (!useOnlyExternalAliasing || some(symbolFromSymbolTable.declarations, isExternalModuleImportEqualsDeclaration))) {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//// [tests/cases/compiler/declarationEmitOfTypeofAliasedExport.ts] ////
2+
3+
//// [a.ts]
4+
class C {}
5+
export { C as D }
6+
7+
//// [b.ts]
8+
import * as a from "./a";
9+
export default a.D;
10+
11+
12+
//// [a.js]
13+
"use strict";
14+
exports.__esModule = true;
15+
var C = /** @class */ (function () {
16+
function C() {
17+
}
18+
return C;
19+
}());
20+
exports.D = C;
21+
//// [b.js]
22+
"use strict";
23+
exports.__esModule = true;
24+
var a = require("./a");
25+
exports["default"] = a.D;
26+
27+
28+
//// [a.d.ts]
29+
declare class C {
30+
}
31+
export { C as D };
32+
//// [b.d.ts]
33+
import * as a from "./a";
34+
declare const _default: typeof a.D;
35+
export default _default;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== /a.ts ===
2+
class C {}
3+
>C : Symbol(C, Decl(a.ts, 0, 0))
4+
5+
export { C as D }
6+
>C : Symbol(D, Decl(a.ts, 1, 8))
7+
>D : Symbol(D, Decl(a.ts, 1, 8))
8+
9+
=== /b.ts ===
10+
import * as a from "./a";
11+
>a : Symbol(a, Decl(b.ts, 0, 6))
12+
13+
export default a.D;
14+
>a.D : Symbol(a.D, Decl(a.ts, 1, 8))
15+
>a : Symbol(a, Decl(b.ts, 0, 6))
16+
>D : Symbol(a.D, Decl(a.ts, 1, 8))
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== /a.ts ===
2+
class C {}
3+
>C : C
4+
5+
export { C as D }
6+
>C : typeof C
7+
>D : typeof C
8+
9+
=== /b.ts ===
10+
import * as a from "./a";
11+
>a : typeof a
12+
13+
export default a.D;
14+
>a.D : typeof a.D
15+
>a : typeof a
16+
>D : typeof a.D
17+

tests/baselines/reference/es6ExportClauseWithoutModuleSpecifier.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export { c as c2 } from "server";
3030

3131
export { i, m as instantiatedModule } from "server";
3232
>i : any
33-
>m : typeof m
34-
>instantiatedModule : typeof m
33+
>m : typeof instantiatedModule
34+
>instantiatedModule : typeof instantiatedModule
3535

3636
export { uninstantiated } from "server";
3737
>uninstantiated : any

tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ export { c as c2 } from "./server";
3030

3131
export { i, m as instantiatedModule } from "./server";
3232
>i : any
33-
>m : typeof m
34-
>instantiatedModule : typeof m
33+
>m : typeof instantiatedModule
34+
>instantiatedModule : typeof instantiatedModule
3535

3636
export { uninstantiated } from "./server";
3737
>uninstantiated : any

tests/baselines/reference/exportSpecifierForAGlobal.errors.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
tests/cases/compiler/b.ts(1,9): error TS2661: Cannot export 'X'. Only local declarations can be exported from a module.
2+
tests/cases/compiler/b.ts(2,17): error TS4060: Return type of exported function has or is using private name 'X'.
23

34

45
==== tests/cases/compiler/a.d.ts (0 errors) ====
56
declare class X { }
67

7-
==== tests/cases/compiler/b.ts (1 errors) ====
8+
==== tests/cases/compiler/b.ts (2 errors) ====
89
export {X};
910
~
1011
!!! error TS2661: Cannot export 'X'. Only local declarations can be exported from a module.
1112
export function f() {
13+
~
14+
!!! error TS4060: Return type of exported function has or is using private name 'X'.
1215
var x: X;
1316
return x;
1417
}

tests/baselines/reference/exportSpecifierForAGlobal.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,3 @@ function f() {
1919
return x;
2020
}
2121
exports.f = f;
22-
23-
24-
//// [b.d.ts]
25-
export { X };
26-
export declare function f(): X;

tests/baselines/reference/exportsAndImports3-amd.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ export enum E {
1515
>E : Symbol(E, Decl(t1.ts, 5, 1))
1616

1717
A, B, C
18-
>A : Symbol(E.A, Decl(t1.ts, 6, 15))
19-
>B : Symbol(E.B, Decl(t1.ts, 7, 6))
20-
>C : Symbol(E.C, Decl(t1.ts, 7, 9))
18+
>A : Symbol(E1.A, Decl(t1.ts, 6, 15))
19+
>B : Symbol(E1.B, Decl(t1.ts, 7, 6))
20+
>C : Symbol(E1.C, Decl(t1.ts, 7, 9))
2121
}
2222
export const enum D {
2323
>D : Symbol(D, Decl(t1.ts, 8, 1))
2424

2525
A, B, C
26-
>A : Symbol(D.A, Decl(t1.ts, 9, 21))
27-
>B : Symbol(D.B, Decl(t1.ts, 10, 6))
28-
>C : Symbol(D.C, Decl(t1.ts, 10, 9))
26+
>A : Symbol(D1.A, Decl(t1.ts, 9, 21))
27+
>B : Symbol(D1.B, Decl(t1.ts, 10, 6))
28+
>C : Symbol(D1.C, Decl(t1.ts, 10, 9))
2929
}
3030
export module M {
3131
>M : Symbol(M, Decl(t1.ts, 11, 1))

tests/baselines/reference/exportsAndImports3-es6.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ export enum E {
1515
>E : Symbol(E, Decl(t1.ts, 5, 1))
1616

1717
A, B, C
18-
>A : Symbol(E.A, Decl(t1.ts, 6, 15))
19-
>B : Symbol(E.B, Decl(t1.ts, 7, 6))
20-
>C : Symbol(E.C, Decl(t1.ts, 7, 9))
18+
>A : Symbol(E1.A, Decl(t1.ts, 6, 15))
19+
>B : Symbol(E1.B, Decl(t1.ts, 7, 6))
20+
>C : Symbol(E1.C, Decl(t1.ts, 7, 9))
2121
}
2222
export const enum D {
2323
>D : Symbol(D, Decl(t1.ts, 8, 1))
2424

2525
A, B, C
26-
>A : Symbol(D.A, Decl(t1.ts, 9, 21))
27-
>B : Symbol(D.B, Decl(t1.ts, 10, 6))
28-
>C : Symbol(D.C, Decl(t1.ts, 10, 9))
26+
>A : Symbol(D1.A, Decl(t1.ts, 9, 21))
27+
>B : Symbol(D1.B, Decl(t1.ts, 10, 6))
28+
>C : Symbol(D1.C, Decl(t1.ts, 10, 9))
2929
}
3030
export module M {
3131
>M : Symbol(M, Decl(t1.ts, 11, 1))

tests/baselines/reference/exportsAndImports3.symbols

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ export enum E {
1515
>E : Symbol(E, Decl(t1.ts, 5, 1))
1616

1717
A, B, C
18-
>A : Symbol(E.A, Decl(t1.ts, 6, 15))
19-
>B : Symbol(E.B, Decl(t1.ts, 7, 6))
20-
>C : Symbol(E.C, Decl(t1.ts, 7, 9))
18+
>A : Symbol(E1.A, Decl(t1.ts, 6, 15))
19+
>B : Symbol(E1.B, Decl(t1.ts, 7, 6))
20+
>C : Symbol(E1.C, Decl(t1.ts, 7, 9))
2121
}
2222
export const enum D {
2323
>D : Symbol(D, Decl(t1.ts, 8, 1))
2424

2525
A, B, C
26-
>A : Symbol(D.A, Decl(t1.ts, 9, 21))
27-
>B : Symbol(D.B, Decl(t1.ts, 10, 6))
28-
>C : Symbol(D.C, Decl(t1.ts, 10, 9))
26+
>A : Symbol(D1.A, Decl(t1.ts, 9, 21))
27+
>B : Symbol(D1.B, Decl(t1.ts, 10, 6))
28+
>C : Symbol(D1.C, Decl(t1.ts, 10, 9))
2929
}
3030
export module M {
3131
>M : Symbol(M, Decl(t1.ts, 11, 1))
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
//// [tests/cases/compiler/reexportWrittenCorrectlyInDeclaration.ts] ////
2+
3+
//// [ThingA.ts]
4+
// https://github.com/Microsoft/TypeScript/issues/8612
5+
export class ThingA { }
6+
7+
//// [ThingB.ts]
8+
export class ThingB { }
9+
10+
//// [Things.ts]
11+
export {ThingA} from "./ThingA";
12+
export {ThingB} from "./ThingB";
13+
14+
//// [Test.ts]
15+
import * as things from "./Things";
16+
17+
export class Test {
18+
public method = (input: things.ThingA) => { };
19+
}
20+
21+
//// [ThingA.js]
22+
"use strict";
23+
exports.__esModule = true;
24+
// https://github.com/Microsoft/TypeScript/issues/8612
25+
var ThingA = /** @class */ (function () {
26+
function ThingA() {
27+
}
28+
return ThingA;
29+
}());
30+
exports.ThingA = ThingA;
31+
//// [ThingB.js]
32+
"use strict";
33+
exports.__esModule = true;
34+
var ThingB = /** @class */ (function () {
35+
function ThingB() {
36+
}
37+
return ThingB;
38+
}());
39+
exports.ThingB = ThingB;
40+
//// [Things.js]
41+
"use strict";
42+
exports.__esModule = true;
43+
var ThingA_1 = require("./ThingA");
44+
exports.ThingA = ThingA_1.ThingA;
45+
var ThingB_1 = require("./ThingB");
46+
exports.ThingB = ThingB_1.ThingB;
47+
//// [Test.js]
48+
"use strict";
49+
exports.__esModule = true;
50+
var Test = /** @class */ (function () {
51+
function Test() {
52+
this.method = function (input) { };
53+
}
54+
return Test;
55+
}());
56+
exports.Test = Test;
57+
58+
59+
//// [ThingA.d.ts]
60+
export declare class ThingA {
61+
}
62+
//// [ThingB.d.ts]
63+
export declare class ThingB {
64+
}
65+
//// [Things.d.ts]
66+
export { ThingA } from "./ThingA";
67+
export { ThingB } from "./ThingB";
68+
//// [Test.d.ts]
69+
import * as things from "./Things";
70+
export declare class Test {
71+
method: (input: things.ThingA) => void;
72+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
=== tests/cases/compiler/ThingA.ts ===
2+
// https://github.com/Microsoft/TypeScript/issues/8612
3+
export class ThingA { }
4+
>ThingA : Symbol(ThingA, Decl(ThingA.ts, 0, 0))
5+
6+
=== tests/cases/compiler/ThingB.ts ===
7+
export class ThingB { }
8+
>ThingB : Symbol(ThingB, Decl(ThingB.ts, 0, 0))
9+
10+
=== tests/cases/compiler/Things.ts ===
11+
export {ThingA} from "./ThingA";
12+
>ThingA : Symbol(ThingA, Decl(Things.ts, 0, 8))
13+
14+
export {ThingB} from "./ThingB";
15+
>ThingB : Symbol(ThingB, Decl(Things.ts, 1, 8))
16+
17+
=== tests/cases/compiler/Test.ts ===
18+
import * as things from "./Things";
19+
>things : Symbol(things, Decl(Test.ts, 0, 6))
20+
21+
export class Test {
22+
>Test : Symbol(Test, Decl(Test.ts, 0, 35))
23+
24+
public method = (input: things.ThingA) => { };
25+
>method : Symbol(Test.method, Decl(Test.ts, 2, 19))
26+
>input : Symbol(input, Decl(Test.ts, 3, 21))
27+
>things : Symbol(things, Decl(Test.ts, 0, 6))
28+
>ThingA : Symbol(things.ThingA, Decl(Things.ts, 0, 8))
29+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/ThingA.ts ===
2+
// https://github.com/Microsoft/TypeScript/issues/8612
3+
export class ThingA { }
4+
>ThingA : ThingA
5+
6+
=== tests/cases/compiler/ThingB.ts ===
7+
export class ThingB { }
8+
>ThingB : ThingB
9+
10+
=== tests/cases/compiler/Things.ts ===
11+
export {ThingA} from "./ThingA";
12+
>ThingA : typeof ThingA
13+
14+
export {ThingB} from "./ThingB";
15+
>ThingB : typeof ThingB
16+
17+
=== tests/cases/compiler/Test.ts ===
18+
import * as things from "./Things";
19+
>things : typeof things
20+
21+
export class Test {
22+
>Test : Test
23+
24+
public method = (input: things.ThingA) => { };
25+
>method : (input: things.ThingA) => void
26+
>(input: things.ThingA) => { } : (input: things.ThingA) => void
27+
>input : things.ThingA
28+
>things : any
29+
>ThingA : things.ThingA
30+
}

tests/baselines/reference/systemModule15.types

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ use(moduleB.moduleC);
2323
use(moduleB.moduleCStar);
2424
>use(moduleB.moduleCStar) : void
2525
>use : (v: any) => void
26-
>moduleB.moduleCStar : typeof "tests/cases/compiler/file3"
26+
>moduleB.moduleCStar : typeof moduleB.moduleCStar
2727
>moduleB : typeof moduleB
28-
>moduleCStar : typeof "tests/cases/compiler/file3"
28+
>moduleCStar : typeof moduleB.moduleCStar
2929

3030
=== tests/cases/compiler/file2.ts ===
3131
import * as moduleCStar from "./file3"
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// @declaration: true
2+
// @filename: /a.ts
3+
class C {}
4+
export { C as D }
5+
6+
// @filename: /b.ts
7+
import * as a from "./a";
8+
export default a.D;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// https://github.com/Microsoft/TypeScript/issues/8612
2+
// @declaration: true
3+
// @filename: ThingA.ts
4+
export class ThingA { }
5+
6+
// @filename: ThingB.ts
7+
export class ThingB { }
8+
9+
// @filename: Things.ts
10+
export {ThingA} from "./ThingA";
11+
export {ThingB} from "./ThingB";
12+
13+
// @filename: Test.ts
14+
import * as things from "./Things";
15+
16+
export class Test {
17+
public method = (input: things.ThingA) => { };
18+
}

0 commit comments

Comments
 (0)