Skip to content

Commit 87fea4f

Browse files
committed
feat(3792): allow export default namespaces/modules/enums/types/const enums/declared classes
1 parent 4ce902c commit 87fea4f

File tree

75 files changed

+1691
-220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+1691
-220
lines changed

src/compiler/parser.ts

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2010,10 +2010,26 @@ namespace ts {
20102010

20112011
function nextTokenCanFollowDefaultKeyword(): boolean {
20122012
nextToken();
2013-
return token() === SyntaxKind.ClassKeyword || token() === SyntaxKind.FunctionKeyword ||
2014-
token() === SyntaxKind.InterfaceKeyword ||
2015-
(token() === SyntaxKind.AbstractKeyword && lookAhead(nextTokenIsClassKeywordOnSameLine)) ||
2016-
(token() === SyntaxKind.AsyncKeyword && lookAhead(nextTokenIsFunctionKeywordOnSameLine));
2013+
switch (token()) {
2014+
case SyntaxKind.ClassKeyword:
2015+
case SyntaxKind.InterfaceKeyword:
2016+
case SyntaxKind.FunctionKeyword:
2017+
case SyntaxKind.NamespaceKeyword:
2018+
case SyntaxKind.ModuleKeyword:
2019+
case SyntaxKind.EnumKeyword:
2020+
case SyntaxKind.ConstKeyword:
2021+
return true;
2022+
case SyntaxKind.DeclareKeyword:
2023+
return lookAhead(nextTokenCanFollowExportDefaultDeclareKeyword);
2024+
case SyntaxKind.TypeKeyword:
2025+
return lookAhead(nextTokenIsIdentifierOnSameLine);
2026+
case SyntaxKind.AbstractKeyword:
2027+
return lookAhead(nextTokenIsClassKeywordOnSameLine);
2028+
case SyntaxKind.AsyncKeyword:
2029+
return lookAhead(nextTokenIsFunctionKeywordOnSameLine);
2030+
default:
2031+
return false;
2032+
}
20172033
}
20182034

20192035
// True if positioned at the start of a list element
@@ -6137,6 +6153,18 @@ namespace ts {
61376153
return token() === SyntaxKind.ClassKeyword && !scanner.hasPrecedingLineBreak();
61386154
}
61396155

6156+
function nextTokenCanFollowExportDefaultDeclareKeyword() {
6157+
nextToken();
6158+
switch (token()) {
6159+
case SyntaxKind.NamespaceKeyword:
6160+
case SyntaxKind.ModuleKeyword:
6161+
case SyntaxKind.ClassKeyword:
6162+
return !scanner.hasPrecedingLineBreak();
6163+
default:
6164+
return false;
6165+
}
6166+
}
6167+
61406168
function nextTokenIsFunctionKeywordOnSameLine() {
61416169
nextToken();
61426170
return token() === SyntaxKind.FunctionKeyword && !scanner.hasPrecedingLineBreak();

src/compiler/transformers/ts.ts

Lines changed: 86 additions & 178 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/exportDefaultConstEnum.ts] ////
2+
3+
//// [a.ts]
4+
export default const enum A {
5+
A,
6+
B
7+
}
8+
9+
//// [b.ts]
10+
import A from "./a"
11+
12+
A.A;
13+
A.B;
14+
15+
16+
//// [a.js]
17+
"use strict";
18+
exports.__esModule = true;
19+
//// [b.js]
20+
"use strict";
21+
exports.__esModule = true;
22+
0 /* A */;
23+
1 /* B */;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default const enum A {
3+
>A : Symbol(A, Decl(a.ts, 0, 0))
4+
5+
A,
6+
>A : Symbol(A.A, Decl(a.ts, 0, 29))
7+
8+
B
9+
>B : Symbol(A.B, Decl(a.ts, 1, 6))
10+
}
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import A from "./a"
14+
>A : Symbol(A, Decl(b.ts, 0, 6))
15+
16+
A.A;
17+
>A.A : Symbol(A.A, Decl(a.ts, 0, 29))
18+
>A : Symbol(A, Decl(b.ts, 0, 6))
19+
>A : Symbol(A.A, Decl(a.ts, 0, 29))
20+
21+
A.B;
22+
>A.B : Symbol(A.B, Decl(a.ts, 1, 6))
23+
>A : Symbol(A, Decl(b.ts, 0, 6))
24+
>B : Symbol(A.B, Decl(a.ts, 1, 6))
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default const enum A {
3+
>A : A
4+
5+
A,
6+
>A : A.A
7+
8+
B
9+
>B : A.B
10+
}
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import A from "./a"
14+
>A : typeof A
15+
16+
A.A;
17+
>A.A : A.A
18+
>A : typeof A
19+
>A : A.A
20+
21+
A.B;
22+
>A.B : A.B
23+
>A : typeof A
24+
>B : A.B
25+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
//// [tests/cases/compiler/exportDefaultDeclareClass.ts] ////
2+
3+
//// [a.ts]
4+
export default declare class C {
5+
public foo: number;
6+
}
7+
8+
//// [b.ts]
9+
import A from "./a"
10+
let a: A;
11+
a.foo
12+
13+
14+
//// [a.js]
15+
"use strict";
16+
exports.__esModule = true;
17+
//// [b.js]
18+
"use strict";
19+
exports.__esModule = true;
20+
var a;
21+
a.foo;
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default declare class C {
3+
>C : Symbol(C, Decl(a.ts, 0, 0))
4+
5+
public foo: number;
6+
>foo : Symbol(C.foo, Decl(a.ts, 0, 32))
7+
}
8+
9+
=== tests/cases/compiler/b.ts ===
10+
import A from "./a"
11+
>A : Symbol(A, Decl(b.ts, 0, 6))
12+
13+
let a: A;
14+
>a : Symbol(a, Decl(b.ts, 1, 3))
15+
>A : Symbol(A, Decl(b.ts, 0, 6))
16+
17+
a.foo
18+
>a.foo : Symbol(A.foo, Decl(a.ts, 0, 32))
19+
>a : Symbol(a, Decl(b.ts, 1, 3))
20+
>foo : Symbol(A.foo, Decl(a.ts, 0, 32))
21+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default declare class C {
3+
>C : C
4+
5+
public foo: number;
6+
>foo : number
7+
}
8+
9+
=== tests/cases/compiler/b.ts ===
10+
import A from "./a"
11+
>A : typeof A
12+
13+
let a: A;
14+
>a : A
15+
16+
a.foo
17+
>a.foo : number
18+
>a : A
19+
>foo : number
20+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/exportDefaultEnum.ts] ////
2+
3+
//// [a.ts]
4+
export default enum A {
5+
A,
6+
B
7+
}
8+
9+
//// [b.ts]
10+
import A from "./a"
11+
12+
A.A;
13+
A.B;
14+
15+
16+
//// [a.js]
17+
define(["require", "exports"], function (require, exports) {
18+
"use strict";
19+
Object.defineProperty(exports, "__esModule", { value: true });
20+
var A = {};
21+
exports.default = A;
22+
(function (A) {
23+
A[A["A"] = 0] = "A";
24+
A[A["B"] = 1] = "B";
25+
})(A);
26+
});
27+
//// [b.js]
28+
define(["require", "exports", "./a"], function (require, exports, a_1) {
29+
"use strict";
30+
Object.defineProperty(exports, "__esModule", { value: true });
31+
a_1.default.A;
32+
a_1.default.B;
33+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default enum A {
3+
>A : Symbol(A, Decl(a.ts, 0, 0))
4+
5+
A,
6+
>A : Symbol(A.A, Decl(a.ts, 0, 23))
7+
8+
B
9+
>B : Symbol(A.B, Decl(a.ts, 1, 6))
10+
}
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import A from "./a"
14+
>A : Symbol(A, Decl(b.ts, 0, 6))
15+
16+
A.A;
17+
>A.A : Symbol(A.A, Decl(a.ts, 0, 23))
18+
>A : Symbol(A, Decl(b.ts, 0, 6))
19+
>A : Symbol(A.A, Decl(a.ts, 0, 23))
20+
21+
A.B;
22+
>A.B : Symbol(A.B, Decl(a.ts, 1, 6))
23+
>A : Symbol(A, Decl(b.ts, 0, 6))
24+
>B : Symbol(A.B, Decl(a.ts, 1, 6))
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default enum A {
3+
>A : A
4+
5+
A,
6+
>A : A.A
7+
8+
B
9+
>B : A.B
10+
}
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import A from "./a"
14+
>A : typeof A
15+
16+
A.A;
17+
>A.A : A.A
18+
>A : typeof A
19+
>A : A.A
20+
21+
A.B;
22+
>A.B : A.B
23+
>A : typeof A
24+
>B : A.B
25+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//// [tests/cases/compiler/exportDefaultEnum.ts] ////
2+
3+
//// [a.ts]
4+
export default enum A {
5+
A,
6+
B
7+
}
8+
9+
//// [b.ts]
10+
import A from "./a"
11+
12+
A.A;
13+
A.B;
14+
15+
16+
//// [a.js]
17+
define(["require", "exports"], function (require, exports) {
18+
"use strict";
19+
Object.defineProperty(exports, "__esModule", { value: true });
20+
var A = {};
21+
exports.default = A;
22+
(function (A) {
23+
A[A["A"] = 0] = "A";
24+
A[A["B"] = 1] = "B";
25+
})(A);
26+
});
27+
//// [b.js]
28+
define(["require", "exports", "./a"], function (require, exports, a_1) {
29+
"use strict";
30+
Object.defineProperty(exports, "__esModule", { value: true });
31+
a_1.default.A;
32+
a_1.default.B;
33+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default enum A {
3+
>A : Symbol(A, Decl(a.ts, 0, 0))
4+
5+
A,
6+
>A : Symbol(A.A, Decl(a.ts, 0, 23))
7+
8+
B
9+
>B : Symbol(A.B, Decl(a.ts, 1, 6))
10+
}
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import A from "./a"
14+
>A : Symbol(A, Decl(b.ts, 0, 6))
15+
16+
A.A;
17+
>A.A : Symbol(A.A, Decl(a.ts, 0, 23))
18+
>A : Symbol(A, Decl(b.ts, 0, 6))
19+
>A : Symbol(A.A, Decl(a.ts, 0, 23))
20+
21+
A.B;
22+
>A.B : Symbol(A.B, Decl(a.ts, 1, 6))
23+
>A : Symbol(A, Decl(b.ts, 0, 6))
24+
>B : Symbol(A.B, Decl(a.ts, 1, 6))
25+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
=== tests/cases/compiler/a.ts ===
2+
export default enum A {
3+
>A : A
4+
5+
A,
6+
>A : A.A
7+
8+
B
9+
>B : A.B
10+
}
11+
12+
=== tests/cases/compiler/b.ts ===
13+
import A from "./a"
14+
>A : typeof A
15+
16+
A.A;
17+
>A.A : A.A
18+
>A : typeof A
19+
>A : A.A
20+
21+
A.B;
22+
>A.B : A.B
23+
>A : typeof A
24+
>B : A.B
25+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/exportDefaultEnum.ts] ////
2+
3+
//// [a.ts]
4+
export default enum A {
5+
A,
6+
B
7+
}
8+
9+
//// [b.ts]
10+
import A from "./a"
11+
12+
A.A;
13+
A.B;
14+
15+
16+
//// [a.js]
17+
"use strict";
18+
Object.defineProperty(exports, "__esModule", { value: true });
19+
var A = {};
20+
exports.default = A;
21+
(function (A) {
22+
A[A["A"] = 0] = "A";
23+
A[A["B"] = 1] = "B";
24+
})(A);
25+
//// [b.js]
26+
"use strict";
27+
Object.defineProperty(exports, "__esModule", { value: true });
28+
var a_1 = require("./a");
29+
a_1.default.A;
30+
a_1.default.B;

0 commit comments

Comments
 (0)