Skip to content

Commit 441cca9

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

File tree

147 files changed

+173
-1900
lines changed

Some content is hidden

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

147 files changed

+173
-1900
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) {
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
343343
}
344344
return node
345345

346+
case ast.KindEnumDeclaration:
347+
return node
348+
346349
default:
347350
return tx.Visitor().VisitEachChild(node)
348351
}

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/blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.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 E.A;
33-
let E;
34-
(function (E) {
35-
E[E["A"] = 0] = "A";
36-
})(E || (E = {}));
3733
}
3834
const config = {
3935
a: AfterObject.A,
4036
};
41-
var AfterObject;
42-
(function (AfterObject) {
43-
AfterObject[AfterObject["A"] = 2] = "A";
44-
})(AfterObject || (AfterObject = {}));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--- old.blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.js
2+
+++ new.blockScopedEnumVariablesUseBeforeDef_verbatimModuleSyntax.js
3+
@@= skipped -29, +29 lines =@@
4+
}
5+
function foo2() {
6+
return E.A;
7+
- let E;
8+
- (function (E) {
9+
- E[E["A"] = 0] = "A";
10+
- })(E || (E = {}));
11+
}
12+
const config = {
13+
a: AfterObject.A,
14+
};
15+
-var AfterObject;
16+
-(function (AfterObject) {
17+
- AfterObject[AfterObject["A"] = 2] = "A";
18+
-})(AfterObject || (AfterObject = {}));

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>

0 commit comments

Comments
 (0)