Skip to content

Commit 2ba8f10

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

File tree

138 files changed

+139
-1873
lines changed

Some content is hidden

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

138 files changed

+139
-1873
lines changed

internal/core/compileroptions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func (options *CompilerOptions) GetResolveJsonModule() bool {
277277
}
278278

279279
func (options *CompilerOptions) ShouldPreserveConstEnums() bool {
280-
return options.PreserveConstEnums == TSTrue || options.IsolatedModules == TSTrue
280+
return options.PreserveConstEnums == TSTrue || options.IsolatedModules == TSTrue || options.VerbatimModuleSyntax == TSTrue
281281
}
282282

283283
func (options *CompilerOptions) GetAllowJS() bool {

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) {
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) {

internal/transformers/tstransforms/typeeraser.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,12 @@ func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
343343
}
344344
return node
345345

346+
case ast.KindEnumDeclaration:
347+
if ast.IsEnumConst(node) {
348+
return node
349+
}
350+
return tx.Visitor().VisitEachChild(node)
351+
346352
default:
347353
return tx.Visitor().VisitEachChild(node)
348354
}

testdata/baselines/reference/submodule/compiler/assignmentNonObjectTypeConstraints.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,6 @@ bar(new B);
2222

2323

2424
//// [assignmentNonObjectTypeConstraints.js]
25-
var E;
26-
(function (E) {
27-
E[E["A"] = 0] = "A";
28-
E[E["B"] = 1] = "B";
29-
E[E["C"] = 2] = "C";
30-
})(E || (E = {}));
3125
function foo(x) {
3226
var y = x; // Ok
3327
}

testdata/baselines/reference/submodule/compiler/assignmentNonObjectTypeConstraints.js.diff

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,6 @@
11
--- old.assignmentNonObjectTypeConstraints.js
22
+++ new.assignmentNonObjectTypeConstraints.js
3-
@@= skipped -21, +21 lines =@@
4-
5-
6-
//// [assignmentNonObjectTypeConstraints.js]
7-
+var E;
8-
+(function (E) {
9-
+ E[E["A"] = 0] = "A";
10-
+ E[E["B"] = 1] = "B";
11-
+ E[E["C"] = 2] = "C";
12-
+})(E || (E = {}));
13-
function foo(x) {
14-
var y = x; // Ok
15-
}
3+
@@= skipped -27, +27 lines =@@
164
foo(5);
175
foo(0 /* E.A */);
186
class A {

testdata/baselines/reference/submodule/compiler/blockScopedEnumVariablesUseBeforeDef.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,7 @@ function foo1() {
3030
}
3131
function foo2() {
3232
return 0 /* E.A */;
33-
let E;
34-
(function (E) {
35-
E[E["A"] = 0] = "A";
36-
})(E || (E = {}));
3733
}
3834
const config = {
3935
a: 2 /* AfterObject.A */,
4036
};
41-
var AfterObject;
42-
(function (AfterObject) {
43-
AfterObject[AfterObject["A"] = 2] = "A";
44-
})(AfterObject || (AfterObject = {}));

testdata/baselines/reference/submodule/compiler/blockScopedEnumVariablesUseBeforeDef.js.diff

Lines changed: 0 additions & 18 deletions
This file was deleted.

testdata/baselines/reference/submodule/compiler/coAndContraVariantInferences2.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,5 @@ function foo(node) {
156156
function bar(node) {
157157
const a = tryCast(node, isExpression); // tryCast<Expression, Node>
158158
}
159-
// Repro from #49924
160-
var SyntaxKind1;
161-
(function (SyntaxKind1) {
162-
SyntaxKind1[SyntaxKind1["ClassExpression"] = 0] = "ClassExpression";
163-
SyntaxKind1[SyntaxKind1["ClassStatement"] = 1] = "ClassStatement";
164-
})(SyntaxKind1 || (SyntaxKind1 = {}));
165159
const maybeClassStatement = tryCast(statement, isClassLike); // ClassLike1
166160
const x = tryCast(types, isNodeArray); // NodeAray<TypeNode>

testdata/baselines/reference/submodule/compiler/coAndContraVariantInferences2.js.diff

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,4 @@
77
-"use strict";
88
function f1(a, b) {
99
const x1 = cast(a, isC); // cast<A, C>
10-
const x2 = cast(b, isC); // cast<A, C>
11-
@@= skipped -35, +34 lines =@@
12-
function bar(node) {
13-
const a = tryCast(node, isExpression); // tryCast<Expression, Node>
14-
}
15-
+// Repro from #49924
16-
+var SyntaxKind1;
17-
+(function (SyntaxKind1) {
18-
+ SyntaxKind1[SyntaxKind1["ClassExpression"] = 0] = "ClassExpression";
19-
+ SyntaxKind1[SyntaxKind1["ClassStatement"] = 1] = "ClassStatement";
20-
+})(SyntaxKind1 || (SyntaxKind1 = {}));
21-
const maybeClassStatement = tryCast(statement, isClassLike); // ClassLike1
22-
const x = tryCast(types, isNodeArray); // NodeAray<TypeNode>
10+
const x2 = cast(b, isC); // cast<A, C>

0 commit comments

Comments
 (0)