Skip to content

Commit ea6d53c

Browse files
authored
fix(1632): erase const enums after inlining (#1639)
1 parent 0522ac5 commit ea6d53c

File tree

160 files changed

+316
-2008
lines changed

Some content is hidden

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

160 files changed

+316
-2008
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.GetIsolatedModules()
281281
}
282282

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

internal/printer/emitcontext.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func (c *EmitContext) NewNodeVisitor(visit func(node *ast.Node) *ast.Node) *ast.
9292
VisitFunctionBody: c.VisitFunctionBody,
9393
VisitIterationBody: c.VisitIterationBody,
9494
VisitTopLevelStatements: c.VisitVariableEnvironment,
95+
VisitEmbeddedStatement: c.VisitEmbeddedStatement,
9596
})
9697
}
9798

@@ -908,7 +909,7 @@ func (c *EmitContext) VisitIterationBody(body *ast.Statement, visitor *ast.NodeV
908909
}
909910

910911
c.StartLexicalEnvironment()
911-
updated := visitor.VisitEmbeddedStatement(body)
912+
updated := c.VisitEmbeddedStatement(body, visitor)
912913
if updated == nil {
913914
panic("Expected visitor to return a statement.")
914915
}
@@ -928,6 +929,21 @@ func (c *EmitContext) VisitIterationBody(body *ast.Statement, visitor *ast.NodeV
928929
return updated
929930
}
930931

932+
func (c *EmitContext) VisitEmbeddedStatement(node *ast.Statement, visitor *ast.NodeVisitor) *ast.Statement {
933+
embeddedStatement := visitor.VisitEmbeddedStatement(node)
934+
if embeddedStatement == nil {
935+
return nil
936+
}
937+
if ast.IsNotEmittedStatement(embeddedStatement) {
938+
emptyStatement := visitor.Factory.NewEmptyStatement()
939+
emptyStatement.Loc = node.Loc
940+
c.SetOriginal(emptyStatement, node)
941+
c.AssignCommentRange(emptyStatement, node)
942+
return emptyStatement
943+
}
944+
return embeddedStatement
945+
}
946+
931947
func (c *EmitContext) SetSyntheticLeadingComments(node *ast.Node, comments []SynthesizedComment) *ast.Node {
932948
c.emitNodes.Get(node).leadingComments = comments
933949
return node
@@ -961,3 +977,11 @@ func (c *EmitContext) GetSyntheticTrailingComments(node *ast.Node) []Synthesized
961977
}
962978
return nil
963979
}
980+
981+
func (c *EmitContext) NewNotEmittedStatement(node *ast.Node) *ast.Statement {
982+
statement := c.Factory.NewNotEmittedStatement()
983+
statement.Loc = node.Loc
984+
c.SetOriginal(statement, node)
985+
c.AssignCommentRange(statement, node)
986+
return statement
987+
}

internal/transformers/tstransforms/runtimesyntax.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func (tx *RuntimeSyntaxTransformer) pushScope(node *ast.Node) (savedCurrentScope
6464
case ast.KindCaseBlock, ast.KindModuleBlock, ast.KindBlock:
6565
tx.currentScope = node
6666
tx.currentScopeFirstDeclarationsOfName = nil
67-
case ast.KindFunctionDeclaration, ast.KindClassDeclaration, ast.KindEnumDeclaration, ast.KindModuleDeclaration, ast.KindVariableStatement:
67+
case ast.KindFunctionDeclaration, ast.KindClassDeclaration, ast.KindVariableStatement:
6868
tx.recordDeclarationInScope(node)
6969
}
7070
return savedCurrentScope, savedCurrentScopeFirstDeclarationsOfName
@@ -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.EmitContext().NewNotEmittedStatement(node.AsNode())
314+
}
315+
312316
statements := []*ast.Statement{}
313317

314318
// If needed, we should emit a variable declaration for the enum:
@@ -553,6 +557,10 @@ func (tx *RuntimeSyntaxTransformer) transformEnumMember(
553557
}
554558

555559
func (tx *RuntimeSyntaxTransformer) visitModuleDeclaration(node *ast.ModuleDeclaration) *ast.Node {
560+
if !tx.shouldEmitModuleDeclaration(node) {
561+
return tx.EmitContext().NewNotEmittedStatement(node.AsNode())
562+
}
563+
556564
statements := []*ast.Statement{}
557565

558566
// If needed, we should emit a variable declaration for the module:
@@ -1130,6 +1138,19 @@ func (tx *RuntimeSyntaxTransformer) evaluateEntity(node *ast.Node, location *ast
11301138
return result
11311139
}
11321140

1141+
func (tx *RuntimeSyntaxTransformer) shouldEmitEnumDeclaration(node *ast.EnumDeclaration) bool {
1142+
return !ast.IsEnumConst(node.AsNode()) || tx.compilerOptions.ShouldPreserveConstEnums()
1143+
}
1144+
1145+
func (tx *RuntimeSyntaxTransformer) shouldEmitModuleDeclaration(node *ast.ModuleDeclaration) bool {
1146+
pn := tx.EmitContext().ParseNode(node.AsNode())
1147+
if pn == nil {
1148+
// If we can't find a parse tree node, assume the node is instantiated.
1149+
return true
1150+
}
1151+
return isInstantiatedModule(node.AsNode(), tx.compilerOptions.ShouldPreserveConstEnums())
1152+
}
1153+
11331154
func getInnermostModuleDeclarationFromDottedModule(moduleDeclaration *ast.ModuleDeclaration) *ast.ModuleDeclaration {
11341155
for moduleDeclaration.Body != nil && moduleDeclaration.Body.Kind == ast.KindModuleDeclaration {
11351156
moduleDeclaration = moduleDeclaration.Body.AsModuleDeclaration()

internal/transformers/tstransforms/runtimesyntax_test.go

Lines changed: 4 additions & 20 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) {
@@ -250,9 +246,7 @@ func TestNamespaceTransformer(t *testing.T) {
250246
input string
251247
output string
252248
}{
253-
{title: "empty namespace", input: "namespace N {}", output: `var N;
254-
(function (N) {
255-
})(N || (N = {}));`},
249+
{title: "empty namespace", input: "namespace N {}", output: ``},
256250

257251
{title: "export var", input: "namespace N { export var x = 1; }", output: `var N;
258252
(function (N) {
@@ -363,19 +357,9 @@ func TestNamespaceTransformer(t *testing.T) {
363357
})(E = N.E || (N.E = {}));
364358
})(N || (N = {}));`},
365359

366-
{title: "export namespace", input: "namespace N { export namespace N2 {} }", output: `var N;
367-
(function (N) {
368-
let N2;
369-
(function (N2) {
370-
})(N2 = N.N2 || (N.N2 = {}));
371-
})(N || (N = {}));`},
360+
{title: "export namespace", input: "namespace N { export namespace N2 {} }", output: ``},
372361

373-
{title: "nested namespace", input: "namespace N.N2 { }", output: `var N;
374-
(function (N) {
375-
let N2;
376-
(function (N2) {
377-
})(N2 = N.N2 || (N.N2 = {}));
378-
})(N || (N = {}));`},
362+
{title: "nested namespace", input: "namespace N.N2 { }", output: ``},
379363

380364
{title: "import=", input: "import X = Y.X;", output: `var X = Y.X;`},
381365

internal/transformers/tstransforms/typeeraser.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ func (tx *TypeEraserTransformer) popNode(grandparentNode *ast.Node) {
3535
}
3636

3737
func (tx *TypeEraserTransformer) elide(node *ast.Statement) *ast.Statement {
38-
statement := tx.Factory().NewNotEmittedStatement()
39-
tx.EmitContext().SetOriginal(statement, node)
40-
statement.Loc = node.Loc
41-
return statement
38+
return tx.EmitContext().NewNotEmittedStatement(node.AsNode())
4239
}
4340

4441
func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
@@ -343,6 +340,12 @@ func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
343340
}
344341
return node
345342

343+
case ast.KindEnumDeclaration:
344+
if ast.IsEnumConst(node) {
345+
return node
346+
}
347+
return tx.Visitor().VisitEachChild(node)
348+
346349
default:
347350
return tx.Visitor().VisitEachChild(node)
348351
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//// [tests/cases/compiler/constEnumInEmbeddedStatements.ts] ////
2+
3+
//// [constEnumInEmbeddedStatements.ts]
4+
function t(x: number) {
5+
if (x)
6+
/* before E */ const enum E { A = 1 } /* after E */
7+
}
8+
9+
10+
//// [constEnumInEmbeddedStatements.js]
11+
function t(x) {
12+
if (x)
13+
/* before E */ ; /* after E */
14+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//// [tests/cases/compiler/constEnumInEmbeddedStatements.ts] ////
2+
3+
=== constEnumInEmbeddedStatements.ts ===
4+
function t(x: number) {
5+
>t : Symbol(t, Decl(constEnumInEmbeddedStatements.ts, 0, 0))
6+
>x : Symbol(x, Decl(constEnumInEmbeddedStatements.ts, 0, 11))
7+
8+
if (x)
9+
>x : Symbol(x, Decl(constEnumInEmbeddedStatements.ts, 0, 11))
10+
11+
/* before E */ const enum E { A = 1 } /* after E */
12+
>E : Symbol(E, Decl(constEnumInEmbeddedStatements.ts, 1, 10))
13+
>A : Symbol(E.A, Decl(constEnumInEmbeddedStatements.ts, 2, 37))
14+
}
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [tests/cases/compiler/constEnumInEmbeddedStatements.ts] ////
2+
3+
=== constEnumInEmbeddedStatements.ts ===
4+
function t(x: number) {
5+
>t : (x: number) => void
6+
>x : number
7+
8+
if (x)
9+
>x : number
10+
11+
/* before E */ const enum E { A = 1 } /* after E */
12+
>E : E
13+
>A : E.A
14+
>1 : 1
15+
}
16+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//// [tests/cases/conformance/constEnums/constEnumInNamespace.ts] ////
2+
3+
//// [constEnumInNamespace.ts]
4+
namespace N {
5+
export const enum E { A = 0 }
6+
}
7+
8+
9+
//// [constEnumInNamespace.js]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//// [tests/cases/conformance/constEnums/constEnumInNamespace.ts] ////
2+
3+
=== constEnumInNamespace.ts ===
4+
namespace N {
5+
>N : Symbol(N, Decl(constEnumInNamespace.ts, 0, 0))
6+
7+
export const enum E { A = 0 }
8+
>E : Symbol(E, Decl(constEnumInNamespace.ts, 0, 13))
9+
>A : Symbol(E.A, Decl(constEnumInNamespace.ts, 1, 23))
10+
}
11+

0 commit comments

Comments
 (0)