Skip to content

Commit c5a62a9

Browse files
committed
fix(1632): erase const enums after inlining
1 parent d85436e commit c5a62a9

9 files changed

+114
-5
lines changed

internal/transformers/tstransforms/runtimesyntax.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ func (tx *RuntimeSyntaxTransformer) addVarForDeclaration(statements []*ast.State
309309
}
310310

311311
func (tx *RuntimeSyntaxTransformer) visitEnumDeclaration(node *ast.EnumDeclaration) *ast.Node {
312+
if !tx.shouldEmitEnumDeclaration(node.AsEnumDeclaration()) {
313+
return tx.Factory().NewNotEmittedStatement()
314+
}
315+
312316
statements := []*ast.Statement{}
313317

314318
// If needed, we should emit a variable declaration for the enum:
@@ -1130,6 +1134,10 @@ func (tx *RuntimeSyntaxTransformer) evaluateEntity(node *ast.Node, location *ast
11301134
return result
11311135
}
11321136

1137+
func (tx *RuntimeSyntaxTransformer) shouldEmitEnumDeclaration(node *ast.EnumDeclaration) bool {
1138+
return !ast.IsEnumConst(node.AsNode()) || tx.compilerOptions.ShouldPreserveConstEnums()
1139+
}
1140+
11331141
func getInnermostModuleDeclarationFromDottedModule(moduleDeclaration *ast.ModuleDeclaration) *ast.ModuleDeclaration {
11341142
for moduleDeclaration.Body != nil && moduleDeclaration.Body.Kind == ast.KindModuleDeclaration {
11351143
moduleDeclaration = moduleDeclaration.Body.AsModuleDeclaration()

internal/transformers/tstransforms/runtimesyntax_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -200,11 +200,7 @@ var E;
200200
E[E["B"] = 1] = "B";
201201
})(E || (E = {}));`},
202202

203-
{title: "const enum", input: "const enum E {A, B}", output: `var E;
204-
(function (E) {
205-
E[E["A"] = 0] = "A";
206-
E[E["B"] = 1] = "B";
207-
})(E || (E = {}));`},
203+
{title: "const enum", input: "const enum E {A, B}", output: ""},
208204

209205
{title: "merged enum", input: "enum E {A} enum E {B=A}", output: `var E;
210206
(function (E) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/preserveConstEnums.ts] ////
2+
3+
//// [preserveConstEnums.ts]
4+
const enum A {
5+
B = 1
6+
}
7+
8+
const a = A.B
9+
10+
11+
//// [preserveConstEnums.js]
12+
var A;
13+
(function (A) {
14+
A[A["B"] = 1] = "B";
15+
})(A || (A = {}));
16+
const a = 1 /* A.B */;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/preserveConstEnums.ts] ////
2+
3+
=== preserveConstEnums.ts ===
4+
const enum A {
5+
>A : Symbol(A, Decl(preserveConstEnums.ts, 0, 0))
6+
7+
B = 1
8+
>B : Symbol(A.B, Decl(preserveConstEnums.ts, 0, 14))
9+
}
10+
11+
const a = A.B
12+
>a : Symbol(a, Decl(preserveConstEnums.ts, 4, 5))
13+
>A.B : Symbol(A.B, Decl(preserveConstEnums.ts, 0, 14))
14+
>A : Symbol(A, Decl(preserveConstEnums.ts, 0, 0))
15+
>B : Symbol(A.B, Decl(preserveConstEnums.ts, 0, 14))
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/preserveConstEnums.ts] ////
2+
3+
=== preserveConstEnums.ts ===
4+
const enum A {
5+
>A : A
6+
7+
B = 1
8+
>B : A.B
9+
>1 : 1
10+
}
11+
12+
const a = A.B
13+
>a : A.B
14+
>A.B : A
15+
>A : typeof A
16+
>B : A
17+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/preserveConstEnums.ts] ////
2+
3+
//// [preserveConstEnums.ts]
4+
const enum A {
5+
B = 1
6+
}
7+
8+
const a = A.B
9+
10+
11+
//// [preserveConstEnums.js]
12+
var A;
13+
(function (A) {
14+
A[A["B"] = 1] = "B";
15+
})(A || (A = {}));
16+
const a = 1 /* A.B */;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/preserveConstEnums.ts] ////
2+
3+
=== preserveConstEnums.ts ===
4+
const enum A {
5+
>A : Symbol(A, Decl(preserveConstEnums.ts, 0, 0))
6+
7+
B = 1
8+
>B : Symbol(A.B, Decl(preserveConstEnums.ts, 0, 14))
9+
}
10+
11+
const a = A.B
12+
>a : Symbol(a, Decl(preserveConstEnums.ts, 4, 5))
13+
>A.B : Symbol(A.B, Decl(preserveConstEnums.ts, 0, 14))
14+
>A : Symbol(A, Decl(preserveConstEnums.ts, 0, 0))
15+
>B : Symbol(A.B, Decl(preserveConstEnums.ts, 0, 14))
16+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/preserveConstEnums.ts] ////
2+
3+
=== preserveConstEnums.ts ===
4+
const enum A {
5+
>A : A
6+
7+
B = 1
8+
>B : A.B
9+
>1 : 1
10+
}
11+
12+
const a = A.B
13+
>a : A.B
14+
>A.B : A
15+
>A : typeof A
16+
>B : A
17+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @preserveConstEnums: true,false
2+
3+
const enum A {
4+
B = 1
5+
}
6+
7+
const a = A.B

0 commit comments

Comments
 (0)