Skip to content

Commit

Permalink
feat(3792): allow export default namespaces/modules/enums/types/const…
Browse files Browse the repository at this point in the history
…/enums/declared classes
  • Loading branch information
a-tarasyuk committed Jun 14, 2022
1 parent 4c1e8f2 commit eb77b46
Show file tree
Hide file tree
Showing 75 changed files with 1,691 additions and 220 deletions.
36 changes: 32 additions & 4 deletions src/compiler/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2168,10 +2168,26 @@ namespace ts {

function nextTokenCanFollowDefaultKeyword(): boolean {
nextToken();
return token() === SyntaxKind.ClassKeyword || token() === SyntaxKind.FunctionKeyword ||
token() === SyntaxKind.InterfaceKeyword ||
(token() === SyntaxKind.AbstractKeyword && lookAhead(nextTokenIsClassKeywordOnSameLine)) ||
(token() === SyntaxKind.AsyncKeyword && lookAhead(nextTokenIsFunctionKeywordOnSameLine));
switch (token()) {
case SyntaxKind.ClassKeyword:
case SyntaxKind.InterfaceKeyword:
case SyntaxKind.FunctionKeyword:
case SyntaxKind.NamespaceKeyword:
case SyntaxKind.ModuleKeyword:
case SyntaxKind.EnumKeyword:
case SyntaxKind.ConstKeyword:
return true;
case SyntaxKind.DeclareKeyword:
return lookAhead(nextTokenCanFollowExportDefaultDeclareKeyword);
case SyntaxKind.TypeKeyword:
return lookAhead(nextTokenIsIdentifierOnSameLine);
case SyntaxKind.AbstractKeyword:
return lookAhead(nextTokenIsClassKeywordOnSameLine);
case SyntaxKind.AsyncKeyword:
return lookAhead(nextTokenIsFunctionKeywordOnSameLine);
default:
return false;
}
}

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

function nextTokenCanFollowExportDefaultDeclareKeyword() {
nextToken();
switch (token()) {
case SyntaxKind.NamespaceKeyword:
case SyntaxKind.ModuleKeyword:
case SyntaxKind.ClassKeyword:
return !scanner.hasPrecedingLineBreak();
default:
return false;
}
}

function nextTokenIsFunctionKeywordOnSameLine() {
nextToken();
return token() === SyntaxKind.FunctionKeyword && !scanner.hasPrecedingLineBreak();
Expand Down
264 changes: 86 additions & 178 deletions src/compiler/transformers/ts.ts

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions tests/baselines/reference/exportDefaultConstEnum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//// [tests/cases/compiler/exportDefaultConstEnum.ts] ////

//// [a.ts]
export default const enum A {
A,
B
}

//// [b.ts]
import A from "./a"

A.A;
A.B;


//// [a.js]
"use strict";
exports.__esModule = true;
//// [b.js]
"use strict";
exports.__esModule = true;
0 /* A.A */;
1 /* A.B */;
25 changes: 25 additions & 0 deletions tests/baselines/reference/exportDefaultConstEnum.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/a.ts ===
export default const enum A {
>A : Symbol(A, Decl(a.ts, 0, 0))

A,
>A : Symbol(A.A, Decl(a.ts, 0, 29))

B
>B : Symbol(A.B, Decl(a.ts, 1, 6))
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : Symbol(A, Decl(b.ts, 0, 6))

A.A;
>A.A : Symbol(A.A, Decl(a.ts, 0, 29))
>A : Symbol(A, Decl(b.ts, 0, 6))
>A : Symbol(A.A, Decl(a.ts, 0, 29))

A.B;
>A.B : Symbol(A.B, Decl(a.ts, 1, 6))
>A : Symbol(A, Decl(b.ts, 0, 6))
>B : Symbol(A.B, Decl(a.ts, 1, 6))

25 changes: 25 additions & 0 deletions tests/baselines/reference/exportDefaultConstEnum.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/a.ts ===
export default const enum A {
>A : A

A,
>A : A.A

B
>B : A.B
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : typeof A

A.A;
>A.A : A.A
>A : typeof A
>A : A.A

A.B;
>A.B : A.B
>A : typeof A
>B : A.B

21 changes: 21 additions & 0 deletions tests/baselines/reference/exportDefaultDeclareClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/exportDefaultDeclareClass.ts] ////

//// [a.ts]
export default declare class C {
public foo: number;
}

//// [b.ts]
import A from "./a"
let a: A;
a.foo


//// [a.js]
"use strict";
exports.__esModule = true;
//// [b.js]
"use strict";
exports.__esModule = true;
var a;
a.foo;
21 changes: 21 additions & 0 deletions tests/baselines/reference/exportDefaultDeclareClass.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
=== tests/cases/compiler/a.ts ===
export default declare class C {
>C : Symbol(C, Decl(a.ts, 0, 0))

public foo: number;
>foo : Symbol(C.foo, Decl(a.ts, 0, 32))
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : Symbol(A, Decl(b.ts, 0, 6))

let a: A;
>a : Symbol(a, Decl(b.ts, 1, 3))
>A : Symbol(A, Decl(b.ts, 0, 6))

a.foo
>a.foo : Symbol(A.foo, Decl(a.ts, 0, 32))
>a : Symbol(a, Decl(b.ts, 1, 3))
>foo : Symbol(A.foo, Decl(a.ts, 0, 32))

20 changes: 20 additions & 0 deletions tests/baselines/reference/exportDefaultDeclareClass.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
=== tests/cases/compiler/a.ts ===
export default declare class C {
>C : C

public foo: number;
>foo : number
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : typeof A

let a: A;
>a : A

a.foo
>a.foo : number
>a : A
>foo : number

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/compiler/exportDefaultEnum.ts] ////

//// [a.ts]
export default enum A {
A,
B
}

//// [b.ts]
import A from "./a"

A.A;
A.B;


//// [a.js]
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var A = {};
exports.default = A;
(function (A) {
A[A["A"] = 0] = "A";
A[A["B"] = 1] = "B";
})(A);
});
//// [b.js]
define(["require", "exports", "./a"], function (require, exports, a_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
a_1.default.A;
a_1.default.B;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/a.ts ===
export default enum A {
>A : Symbol(A, Decl(a.ts, 0, 0))

A,
>A : Symbol(A.A, Decl(a.ts, 0, 23))

B
>B : Symbol(A.B, Decl(a.ts, 1, 6))
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : Symbol(A, Decl(b.ts, 0, 6))

A.A;
>A.A : Symbol(A.A, Decl(a.ts, 0, 23))
>A : Symbol(A, Decl(b.ts, 0, 6))
>A : Symbol(A.A, Decl(a.ts, 0, 23))

A.B;
>A.B : Symbol(A.B, Decl(a.ts, 1, 6))
>A : Symbol(A, Decl(b.ts, 0, 6))
>B : Symbol(A.B, Decl(a.ts, 1, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/a.ts ===
export default enum A {
>A : A

A,
>A : A.A

B
>B : A.B
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : typeof A

A.A;
>A.A : A.A
>A : typeof A
>A : A.A

A.B;
>A.B : A.B
>A : typeof A
>B : A.B

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
//// [tests/cases/compiler/exportDefaultEnum.ts] ////

//// [a.ts]
export default enum A {
A,
B
}

//// [b.ts]
import A from "./a"

A.A;
A.B;


//// [a.js]
define(["require", "exports"], function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var A = {};
exports.default = A;
(function (A) {
A[A["A"] = 0] = "A";
A[A["B"] = 1] = "B";
})(A);
});
//// [b.js]
define(["require", "exports", "./a"], function (require, exports, a_1) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
a_1.default.A;
a_1.default.B;
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/a.ts ===
export default enum A {
>A : Symbol(A, Decl(a.ts, 0, 0))

A,
>A : Symbol(A.A, Decl(a.ts, 0, 23))

B
>B : Symbol(A.B, Decl(a.ts, 1, 6))
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : Symbol(A, Decl(b.ts, 0, 6))

A.A;
>A.A : Symbol(A.A, Decl(a.ts, 0, 23))
>A : Symbol(A, Decl(b.ts, 0, 6))
>A : Symbol(A.A, Decl(a.ts, 0, 23))

A.B;
>A.B : Symbol(A.B, Decl(a.ts, 1, 6))
>A : Symbol(A, Decl(b.ts, 0, 6))
>B : Symbol(A.B, Decl(a.ts, 1, 6))

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
=== tests/cases/compiler/a.ts ===
export default enum A {
>A : A

A,
>A : A.A

B
>B : A.B
}

=== tests/cases/compiler/b.ts ===
import A from "./a"
>A : typeof A

A.A;
>A.A : A.A
>A : typeof A
>A : A.A

A.B;
>A.B : A.B
>A : typeof A
>B : A.B

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//// [tests/cases/compiler/exportDefaultEnum.ts] ////

//// [a.ts]
export default enum A {
A,
B
}

//// [b.ts]
import A from "./a"

A.A;
A.B;


//// [a.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var A = {};
exports.default = A;
(function (A) {
A[A["A"] = 0] = "A";
A[A["B"] = 1] = "B";
})(A);
//// [b.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var a_1 = require("./a");
a_1.default.A;
a_1.default.B;
Loading

0 comments on commit eb77b46

Please sign in to comment.