Skip to content

Commit 8581a59

Browse files
authored
fix(43347): fix crash occurred when export type to existing commonJs imported name (#43369)
1 parent f526a38 commit 8581a59

6 files changed

+157
-2
lines changed

src/compiler/checker.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33805,11 +33805,14 @@ namespace ts {
3380533805
case SyntaxKind.SourceFile:
3380633806
return DeclarationSpaces.ExportType | DeclarationSpaces.ExportValue | DeclarationSpaces.ExportNamespace;
3380733807
case SyntaxKind.ExportAssignment:
33808+
case SyntaxKind.BinaryExpression:
33809+
const node = d as ExportAssignment | BinaryExpression;
33810+
const expression = isExportAssignment(node) ? node.expression : node.right;
3380833811
// Export assigned entity name expressions act as aliases and should fall through, otherwise they export values
33809-
if (!isEntityNameExpression((d as ExportAssignment).expression)) {
33812+
if (!isEntityNameExpression(expression)) {
3381033813
return DeclarationSpaces.ExportValue;
3381133814
}
33812-
d = (d as ExportAssignment).expression;
33815+
d = expression;
3381333816

3381433817
// The below options all declare an Alias, which is allowed to merge with other values within the importing module.
3381533818
// falls through
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
tests/cases/compiler/types1.ts(2,17): error TS1110: Type expected.
2+
tests/cases/compiler/types1.ts(3,1): error TS1005: '=' expected.
3+
tests/cases/compiler/types2.ts(2,19): error TS1110: Type expected.
4+
tests/cases/compiler/types3.ts(2,13): error TS2456: Type alias 'test' circularly references itself.
5+
6+
7+
==== tests/cases/compiler/test.js (0 errors) ====
8+
module.exports = {
9+
message: ""
10+
}
11+
12+
==== tests/cases/compiler/types1.ts (2 errors) ====
13+
import test from "./test";
14+
export type test
15+
16+
!!! error TS1110: Type expected.
17+
18+
19+
!!! error TS1005: '=' expected.
20+
==== tests/cases/compiler/types2.ts (1 errors) ====
21+
import test from "./test";
22+
export type test =
23+
24+
!!! error TS1110: Type expected.
25+
26+
==== tests/cases/compiler/types3.ts (1 errors) ====
27+
import test from "./test";
28+
export type test = test;
29+
~~~~
30+
!!! error TS2456: Type alias 'test' circularly references itself.
31+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/commonJsExportTypeDeclarationError.ts] ////
2+
3+
//// [test.js]
4+
module.exports = {
5+
message: ""
6+
}
7+
8+
//// [types1.ts]
9+
import test from "./test";
10+
export type test
11+
12+
//// [types2.ts]
13+
import test from "./test";
14+
export type test =
15+
16+
//// [types3.ts]
17+
import test from "./test";
18+
export type test = test;
19+
20+
21+
//// [test.js]
22+
module.exports = {
23+
message: ""
24+
};
25+
//// [types1.js]
26+
"use strict";
27+
Object.defineProperty(exports, "__esModule", { value: true });
28+
//// [types2.js]
29+
"use strict";
30+
Object.defineProperty(exports, "__esModule", { value: true });
31+
//// [types3.js]
32+
"use strict";
33+
Object.defineProperty(exports, "__esModule", { value: true });
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
=== tests/cases/compiler/test.js ===
2+
module.exports = {
3+
>module.exports : Symbol(module.exports, Decl(test.js, 0, 0))
4+
>module : Symbol(export=, Decl(test.js, 0, 0))
5+
>exports : Symbol(export=, Decl(test.js, 0, 0))
6+
7+
message: ""
8+
>message : Symbol(message, Decl(test.js, 0, 18))
9+
}
10+
11+
=== tests/cases/compiler/types1.ts ===
12+
import test from "./test";
13+
>test : Symbol(test, Decl(types1.ts, 0, 6), Decl(types1.ts, 0, 26))
14+
15+
export type test
16+
>test : Symbol(test, Decl(types1.ts, 0, 26))
17+
18+
=== tests/cases/compiler/types2.ts ===
19+
import test from "./test";
20+
>test : Symbol(test, Decl(types2.ts, 0, 6), Decl(types2.ts, 0, 26))
21+
22+
export type test =
23+
>test : Symbol(test, Decl(types2.ts, 0, 26))
24+
25+
=== tests/cases/compiler/types3.ts ===
26+
import test from "./test";
27+
>test : Symbol(test, Decl(types3.ts, 0, 6), Decl(types3.ts, 0, 26))
28+
29+
export type test = test;
30+
>test : Symbol(test, Decl(types3.ts, 0, 26))
31+
>test : Symbol(test, Decl(types3.ts, 0, 26))
32+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
=== tests/cases/compiler/test.js ===
2+
module.exports = {
3+
>module.exports = { message: ""} : { message: string; }
4+
>module.exports : { message: string; }
5+
>module : { exports: { message: string; }; }
6+
>exports : { message: string; }
7+
>{ message: ""} : { message: string; }
8+
9+
message: ""
10+
>message : string
11+
>"" : ""
12+
}
13+
14+
=== tests/cases/compiler/types1.ts ===
15+
import test from "./test";
16+
>test : { message: string; }
17+
18+
export type test
19+
>test : any
20+
21+
=== tests/cases/compiler/types2.ts ===
22+
import test from "./test";
23+
>test : { message: string; }
24+
25+
export type test =
26+
>test : any
27+
28+
=== tests/cases/compiler/types3.ts ===
29+
import test from "./test";
30+
>test : { message: string; }
31+
32+
export type test = test;
33+
>test : any
34+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// @esModuleInterop: true
2+
// @checkJs: true
3+
// @module: commonjs
4+
// @target: es5
5+
// @outDir: ./out
6+
7+
// @Filename: ./test.js
8+
module.exports = {
9+
message: ""
10+
}
11+
12+
// @Filename: ./types1.ts
13+
import test from "./test";
14+
export type test
15+
16+
// @Filename: ./types2.ts
17+
import test from "./test";
18+
export type test =
19+
20+
// @Filename: ./types3.ts
21+
import test from "./test";
22+
export type test = test;

0 commit comments

Comments
 (0)