Skip to content

Commit eb77b46

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

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

+32-4
Original file line numberDiff line numberDiff line change
@@ -2168,10 +2168,26 @@ namespace ts {
21682168

21692169
function nextTokenCanFollowDefaultKeyword(): boolean {
21702170
nextToken();
2171-
return token() === SyntaxKind.ClassKeyword || token() === SyntaxKind.FunctionKeyword ||
2172-
token() === SyntaxKind.InterfaceKeyword ||
2173-
(token() === SyntaxKind.AbstractKeyword && lookAhead(nextTokenIsClassKeywordOnSameLine)) ||
2174-
(token() === SyntaxKind.AsyncKeyword && lookAhead(nextTokenIsFunctionKeywordOnSameLine));
2171+
switch (token()) {
2172+
case SyntaxKind.ClassKeyword:
2173+
case SyntaxKind.InterfaceKeyword:
2174+
case SyntaxKind.FunctionKeyword:
2175+
case SyntaxKind.NamespaceKeyword:
2176+
case SyntaxKind.ModuleKeyword:
2177+
case SyntaxKind.EnumKeyword:
2178+
case SyntaxKind.ConstKeyword:
2179+
return true;
2180+
case SyntaxKind.DeclareKeyword:
2181+
return lookAhead(nextTokenCanFollowExportDefaultDeclareKeyword);
2182+
case SyntaxKind.TypeKeyword:
2183+
return lookAhead(nextTokenIsIdentifierOnSameLine);
2184+
case SyntaxKind.AbstractKeyword:
2185+
return lookAhead(nextTokenIsClassKeywordOnSameLine);
2186+
case SyntaxKind.AsyncKeyword:
2187+
return lookAhead(nextTokenIsFunctionKeywordOnSameLine);
2188+
default:
2189+
return false;
2190+
}
21752191
}
21762192

21772193
// True if positioned at the start of a list element
@@ -6339,6 +6355,18 @@ namespace ts {
63396355
return token() === SyntaxKind.ClassKeyword && !scanner.hasPrecedingLineBreak();
63406356
}
63416357

6358+
function nextTokenCanFollowExportDefaultDeclareKeyword() {
6359+
nextToken();
6360+
switch (token()) {
6361+
case SyntaxKind.NamespaceKeyword:
6362+
case SyntaxKind.ModuleKeyword:
6363+
case SyntaxKind.ClassKeyword:
6364+
return !scanner.hasPrecedingLineBreak();
6365+
default:
6366+
return false;
6367+
}
6368+
}
6369+
63426370
function nextTokenIsFunctionKeywordOnSameLine() {
63436371
nextToken();
63446372
return token() === SyntaxKind.FunctionKeyword && !scanner.hasPrecedingLineBreak();

src/compiler/transformers/ts.ts

+86-178
Large diffs are not rendered by default.
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.A */;
23+
1 /* A.B */;
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+
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+
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;
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+
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+
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+
});
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+
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+
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+
});
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+
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+
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)