Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
2ba8f10
fix(1632): erase const enums after inlining
a-tarasyuk Aug 27, 2025
1050065
use GetIsolatedModules
a-tarasyuk Aug 27, 2025
d6d6d58
Merge branch 'main' into fix/1632
a-tarasyuk Aug 27, 2025
3e3384c
restore variable reference emission for module declarations
a-tarasyuk Aug 28, 2025
ac5a131
Merge branch 'fix/1632' of https://github.com/a-tarasyuk/typescript-g…
a-tarasyuk Aug 28, 2025
37d672b
treat NotEmittedStatement as EmptyStatement in EmbeddedStatement tran…
a-tarasyuk Aug 28, 2025
3eb7e8e
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Aug 28, 2025
046bd93
fix VisitIterationBody
a-tarasyuk Aug 28, 2025
946df75
Merge branch 'main' into fix/1632
a-tarasyuk Aug 28, 2025
5c2206c
Merge branch 'main' into fix/1632
a-tarasyuk Aug 29, 2025
f729313
Merge branch 'main' into fix/1632
a-tarasyuk Aug 29, 2025
3b65035
Merge branch 'main' into fix/1632
a-tarasyuk Aug 31, 2025
f6aa1df
Merge branch 'main' into fix/1632
a-tarasyuk Sep 2, 2025
d991f93
Merge branch 'main' into fix/1632
a-tarasyuk Sep 2, 2025
e281903
Merge branch 'main' into fix/1632
a-tarasyuk Sep 3, 2025
5b9e5a3
elide const enums in modules based on preserveConstEnums
a-tarasyuk Sep 3, 2025
bc345c6
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Sep 3, 2025
8e3cb00
Merge branch 'main' into fix/1632
a-tarasyuk Sep 4, 2025
60a9d1f
add NewNotEmittedStatement to EmitContext
a-tarasyuk Sep 4, 2025
c283cdb
Merge branch 'fix/1632' of https://github.com/a-tarasyuk/typescript-g…
a-tarasyuk Sep 4, 2025
c02f91e
preserve text/comment ranges on empty statements
a-tarasyuk Sep 10, 2025
82bf47e
Merge branch 'main' of https://github.com/microsoft/typescript-go int…
a-tarasyuk Sep 10, 2025
8e83902
Merge branch 'main' into fix/1632
a-tarasyuk Sep 15, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/core/compileroptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func (options *CompilerOptions) GetResolveJsonModule() bool {
}

func (options *CompilerOptions) ShouldPreserveConstEnums() bool {
return options.PreserveConstEnums == TSTrue || options.IsolatedModules == TSTrue
return options.PreserveConstEnums == TSTrue || options.GetIsolatedModules()
}

func (options *CompilerOptions) GetAllowJS() bool {
Expand Down
10 changes: 9 additions & 1 deletion internal/transformers/tstransforms/runtimesyntax.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func (tx *RuntimeSyntaxTransformer) pushScope(node *ast.Node) (savedCurrentScope
case ast.KindCaseBlock, ast.KindModuleBlock, ast.KindBlock:
tx.currentScope = node
tx.currentScopeFirstDeclarationsOfName = nil
case ast.KindFunctionDeclaration, ast.KindClassDeclaration, ast.KindEnumDeclaration, ast.KindModuleDeclaration, ast.KindVariableStatement:
case ast.KindFunctionDeclaration, ast.KindClassDeclaration, ast.KindVariableStatement:
tx.recordDeclarationInScope(node)
}
return savedCurrentScope, savedCurrentScopeFirstDeclarationsOfName
Expand Down Expand Up @@ -309,6 +309,10 @@ func (tx *RuntimeSyntaxTransformer) addVarForDeclaration(statements []*ast.State
}

func (tx *RuntimeSyntaxTransformer) visitEnumDeclaration(node *ast.EnumDeclaration) *ast.Node {
if !tx.shouldEmitEnumDeclaration(node) {
return tx.Factory().NewNotEmittedStatement()
}

statements := []*ast.Statement{}

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

func (tx *RuntimeSyntaxTransformer) shouldEmitEnumDeclaration(node *ast.EnumDeclaration) bool {
return !ast.IsEnumConst(node.AsNode()) || tx.compilerOptions.ShouldPreserveConstEnums()
}

func getInnermostModuleDeclarationFromDottedModule(moduleDeclaration *ast.ModuleDeclaration) *ast.ModuleDeclaration {
for moduleDeclaration.Body != nil && moduleDeclaration.Body.Kind == ast.KindModuleDeclaration {
moduleDeclaration = moduleDeclaration.Body.AsModuleDeclaration()
Expand Down
6 changes: 1 addition & 5 deletions internal/transformers/tstransforms/runtimesyntax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,7 @@ var E;
E[E["B"] = 1] = "B";
})(E || (E = {}));`},

{title: "const enum", input: "const enum E {A, B}", output: `var E;
(function (E) {
E[E["A"] = 0] = "A";
E[E["B"] = 1] = "B";
})(E || (E = {}));`},
{title: "const enum", input: "const enum E {A, B}", output: ""},

{title: "merged enum", input: "enum E {A} enum E {B=A}", output: `var E;
(function (E) {
Expand Down
6 changes: 6 additions & 0 deletions internal/transformers/tstransforms/typeeraser.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ func (tx *TypeEraserTransformer) visit(node *ast.Node) *ast.Node {
}
return node

case ast.KindEnumDeclaration:
if ast.IsEnumConst(node) {
return node
}
return tx.Visitor().VisitEachChild(node)

default:
return tx.Visitor().VisitEachChild(node)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ bar(new B);


//// [assignmentNonObjectTypeConstraints.js]
var E;
(function (E) {
E[E["A"] = 0] = "A";
E[E["B"] = 1] = "B";
E[E["C"] = 2] = "C";
})(E || (E = {}));
function foo(x) {
var y = x; // Ok
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
--- old.assignmentNonObjectTypeConstraints.js
+++ new.assignmentNonObjectTypeConstraints.js
@@= skipped -21, +21 lines =@@


//// [assignmentNonObjectTypeConstraints.js]
+var E;
+(function (E) {
+ E[E["A"] = 0] = "A";
+ E[E["B"] = 1] = "B";
+ E[E["C"] = 2] = "C";
+})(E || (E = {}));
function foo(x) {
var y = x; // Ok
}
@@= skipped -27, +27 lines =@@
foo(5);
foo(0 /* E.A */);
class A {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,7 @@ function foo1() {
}
function foo2() {
return 0 /* E.A */;
let E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
}
const config = {
a: 2 /* AfterObject.A */,
};
var AfterObject;
(function (AfterObject) {
AfterObject[AfterObject["A"] = 2] = "A";
})(AfterObject || (AfterObject = {}));

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,5 @@ function foo(node) {
function bar(node) {
const a = tryCast(node, isExpression); // tryCast<Expression, Node>
}
// Repro from #49924
var SyntaxKind1;
(function (SyntaxKind1) {
SyntaxKind1[SyntaxKind1["ClassExpression"] = 0] = "ClassExpression";
SyntaxKind1[SyntaxKind1["ClassStatement"] = 1] = "ClassStatement";
})(SyntaxKind1 || (SyntaxKind1 = {}));
const maybeClassStatement = tryCast(statement, isClassLike); // ClassLike1
const x = tryCast(types, isNodeArray); // NodeAray<TypeNode>
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,4 @@
-"use strict";
function f1(a, b) {
const x1 = cast(a, isC); // cast<A, C>
const x2 = cast(b, isC); // cast<A, C>
@@= skipped -35, +34 lines =@@
function bar(node) {
const a = tryCast(node, isExpression); // tryCast<Expression, Node>
}
+// Repro from #49924
+var SyntaxKind1;
+(function (SyntaxKind1) {
+ SyntaxKind1[SyntaxKind1["ClassExpression"] = 0] = "ClassExpression";
+ SyntaxKind1[SyntaxKind1["ClassStatement"] = 1] = "ClassStatement";
+})(SyntaxKind1 || (SyntaxKind1 = {}));
const maybeClassStatement = tryCast(statement, isClassLike); // ClassLike1
const x = tryCast(types, isNodeArray); // NodeAray<TypeNode>
const x2 = cast(b, isC); // cast<A, C>
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,6 @@ function foo() {


//// [coAndContraVariantInferences3.js]
var SyntaxKind;
(function (SyntaxKind) {
SyntaxKind[SyntaxKind["ImportDeclaration"] = 0] = "ImportDeclaration";
SyntaxKind[SyntaxKind["Modifier"] = 1] = "Modifier";
SyntaxKind[SyntaxKind["ImportClause"] = 2] = "ImportClause";
SyntaxKind[SyntaxKind["AssertClause"] = 3] = "AssertClause";
SyntaxKind[SyntaxKind["Decorator"] = 4] = "Decorator";
})(SyntaxKind || (SyntaxKind = {}));
;
buildOverload("updateImportDeclaration")
.overload({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@

//// [coAndContraVariantInferences3.js]
-"use strict";
+var SyntaxKind;
+(function (SyntaxKind) {
+ SyntaxKind[SyntaxKind["ImportDeclaration"] = 0] = "ImportDeclaration";
+ SyntaxKind[SyntaxKind["Modifier"] = 1] = "Modifier";
+ SyntaxKind[SyntaxKind["ImportClause"] = 2] = "ImportClause";
+ SyntaxKind[SyntaxKind["AssertClause"] = 3] = "AssertClause";
+ SyntaxKind[SyntaxKind["Decorator"] = 4] = "Decorator";
+})(SyntaxKind || (SyntaxKind = {}));
;
buildOverload("updateImportDeclaration")
.overload({
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ function foo() {


//// [coAndContraVariantInferences4.js]
var SyntaxKind;
(function (SyntaxKind) {
SyntaxKind[SyntaxKind["Modifier"] = 0] = "Modifier";
SyntaxKind[SyntaxKind["Decorator"] = 1] = "Decorator";
})(SyntaxKind || (SyntaxKind = {}));
function foo() {
every(modifiers, isModifier);
every(modifiers, isDecorator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@

//// [coAndContraVariantInferences4.js]
-"use strict";
+var SyntaxKind;
+(function (SyntaxKind) {
+ SyntaxKind[SyntaxKind["Modifier"] = 0] = "Modifier";
+ SyntaxKind[SyntaxKind["Decorator"] = 1] = "Decorator";
+})(SyntaxKind || (SyntaxKind = {}));
function foo() {
every(modifiers, isModifier);
every(modifiers, isDecorator);
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,6 @@ var m1;
}
m1.b = b;
})(m1 || (m1 = {}));
var color;
(function (color) {
color[color["red"] = 0] = "red";
color[color["green"] = 1] = "green";
color[color["blue"] = 2] = "blue";
})(color || (color = {}));
var shade = 1;


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,7 @@
constructor(x) {
this.x = x;
}
}
m1.b = b;
})(m1 || (m1 = {}));
+var color;
+(function (color) {
+ color[color["red"] = 0] = "red";
+ color[color["green"] = 1] = "green";
+ color[color["blue"] = 2] = "blue";
+})(color || (color = {}));
var shade = 1;

@@= skipped -11, +12 lines =@@

//// [commentsdoNotEmitComments.d.ts]
declare var myVariable: number;
Expand All @@ -45,7 +35,7 @@
declare var fooVar: () => void;
declare class c {
constructor();
@@= skipped -27, +34 lines =@@
@@= skipped -16, +16 lines =@@
(a: number): number;
new (b: string): any;
[a: number]: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ foo[k] = ['foo'];

//// [a.js]
const k = Symbol();
var Props;
(function (Props) {
Props["k"] = "k";
})(Props || (Props = {}));
foo.k = ['foo'];
foo['k'] = ['foo'];
foo["k" /* Props.k */] = ['foo'];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,5 @@
//// [a.js]
-"use strict";
const k = Symbol();
+var Props;
+(function (Props) {
+ Props["k"] = "k";
+})(Props || (Props = {}));
foo.k = ['foo'];
foo['k'] = ['foo'];
foo["k" /* Props.k */] = ['foo'];
foo['k'] = ['foo'];
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ const enum E { A }
var x = E["B"]

//// [constEnumBadPropertyNames.js]
var E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
var x = E["B"];

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@ const enum E2 {
}

//// [constEnumDeclarations.js]
var E;
(function (E) {
E[E["A"] = 1] = "A";
E[E["B"] = 2] = "B";
E[E["C"] = 3] = "C";
})(E || (E = {}));
var E2;
(function (E2) {
E2[E2["A"] = 1] = "A";
E2[E2["B"] = 2] = "B";
E2[E2["C"] = 3] = "C";
})(E2 || (E2 = {}));


//// [constEnumDeclarations.d.ts]
Expand Down

This file was deleted.

30 changes: 0 additions & 30 deletions testdata/baselines/reference/submodule/compiler/constEnumErrors.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,9 @@ const enum NaNOrInfinity {

//// [constEnumErrors.js]
var E;
(function (E) {
E[E["A"] = 0] = "A";
})(E || (E = {}));
(function (E) {
var x = 1;
})(E || (E = {}));
var E1;
(function (E1) {
// illegal case
// forward reference to the element of the same enum
E1["X"] = E1.Y;
if (typeof E1.X !== "string") E1[E1.X] = "X";
// forward reference to the element of the same enum
E1["Y"] = E1.Z;
if (typeof E1.Y !== "string") E1[E1.Y] = "Y";
E1["Y1"] = E1["Z"];
if (typeof E1.Y1 !== "string") E1[E1.Y1] = "Y1";
})(E1 || (E1 = {}));
var E2;
(function (E2) {
E2[E2["A"] = 0] = "A";
})(E2 || (E2 = {}));
var y0 = E2[1];
var name = "A";
var y1 = E2[name];
Expand All @@ -79,14 +60,3 @@ var y = [E2];
function foo(t) {
}
foo(E2);
var NaNOrInfinity;
(function (NaNOrInfinity) {
NaNOrInfinity[NaNOrInfinity["A"] = 9007199254740992] = "A";
NaNOrInfinity[NaNOrInfinity["B"] = 8.112963841460668e+31] = "B";
NaNOrInfinity[NaNOrInfinity["C"] = 6.582018229284824e+63] = "C";
NaNOrInfinity[NaNOrInfinity["D"] = 4.332296397063773e+127] = "D";
NaNOrInfinity[NaNOrInfinity["E"] = 1.876879207201175e+255] = "E";
NaNOrInfinity[NaNOrInfinity["F"] = NaNOrInfinity.E * NaNOrInfinity.E] = "F";
NaNOrInfinity[NaNOrInfinity["G"] = 1 / 0] = "G";
NaNOrInfinity[NaNOrInfinity["H"] = 0 / 0] = "H"; // NaN
})(NaNOrInfinity || (NaNOrInfinity = {}));
Loading
Loading