Skip to content

Port es2021 transform #1372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
96 changes: 95 additions & 1 deletion internal/transformers/estransforms/logicalassignment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,101 @@ type logicalAssignmentTransformer struct {
}

func (ch *logicalAssignmentTransformer) visit(node *ast.Node) *ast.Node {
return node // !!!
if node.SubtreeFacts()&ast.SubtreeContainsLogicalAssignments == 0 {
return node
}
switch node.Kind {
case ast.KindBinaryExpression:
return ch.visitBinaryExpression(node.AsBinaryExpression())
default:
return ch.Visitor().VisitEachChild(node)
}
}

func (ch *logicalAssignmentTransformer) visitBinaryExpression(node *ast.BinaryExpression) *ast.Node {
var nonAssignmentOperator ast.Kind
switch node.OperatorToken.Kind {
case ast.KindBarBarEqualsToken:
nonAssignmentOperator = ast.KindBarBarToken
case ast.KindAmpersandAmpersandEqualsToken:
nonAssignmentOperator = ast.KindAmpersandAmpersandToken
case ast.KindQuestionQuestionEqualsToken:
nonAssignmentOperator = ast.KindQuestionQuestionToken
default:
return ch.Visitor().VisitEachChild(node.AsNode())
}

left := ast.SkipParentheses(ch.Visitor().VisitNode(node.Left))
assignmentTarget := left
right := ast.SkipParentheses(ch.Visitor().VisitNode(node.Right))

if ast.IsAccessExpression(left) {
propertyAccessTargetSimpleCopiable := transformers.IsSimpleCopiableExpression(left.Expression())
propertyAccessTarget := left.Expression()
propertyAccessTargetAssignment := left.Expression()
if !propertyAccessTargetSimpleCopiable {
propertyAccessTarget = ch.Factory().NewTempVariable()
ch.EmitContext().AddVariableDeclaration(propertyAccessTarget)
propertyAccessTargetAssignment = ch.Factory().NewAssignmentExpression(
propertyAccessTarget,
left.Expression(),
)
}

if ast.IsPropertyAccessExpression(left) {
assignmentTarget = ch.Factory().NewPropertyAccessExpression(
propertyAccessTarget,
nil,
left.Name(),
ast.NodeFlagsNone,
)
left = ch.Factory().NewPropertyAccessExpression(
propertyAccessTargetAssignment,
nil,
left.Name(),
ast.NodeFlagsNone,
)
} else {
elementAccessArgumentSimpleCopiable := transformers.IsSimpleCopiableExpression(left.AsElementAccessExpression().ArgumentExpression)
elementAccessArgument := left.AsElementAccessExpression().ArgumentExpression
argumentExpr := elementAccessArgument
if !elementAccessArgumentSimpleCopiable {
elementAccessArgument = ch.Factory().NewTempVariable()
ch.EmitContext().AddVariableDeclaration(elementAccessArgument)
argumentExpr = ch.Factory().NewAssignmentExpression(
elementAccessArgument,
left.AsElementAccessExpression().ArgumentExpression,
)
}

assignmentTarget = ch.Factory().NewElementAccessExpression(
propertyAccessTarget,
nil,
elementAccessArgument,
ast.NodeFlagsNone,
)
left = ch.Factory().NewElementAccessExpression(
propertyAccessTargetAssignment,
nil,
argumentExpr,
ast.NodeFlagsNone,
)
}

}

return ch.Factory().NewBinaryExpression(
nil,
left,
nil,
ch.Factory().NewToken(nonAssignmentOperator),
ch.Factory().NewParenthesizedExpression(
ch.Factory().NewAssignmentExpression(
assignmentTarget,
right,
),
),
)
}

func newLogicalAssignmentTransformer(emitContext *printer.EmitContext) *transformers.Transformer {
Expand Down
14 changes: 2 additions & 12 deletions internal/transformers/moduletransforms/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/microsoft/typescript-go/internal/core"
"github.com/microsoft/typescript-go/internal/outputpaths"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/transformers"
"github.com/microsoft/typescript-go/internal/tspath"
)

Expand Down Expand Up @@ -113,20 +114,9 @@ func isFileLevelReservedGeneratedIdentifier(emitContext *printer.EmitContext, na
info.Flags.IsReservedInNestedScopes()
}

// Used in the module transformer to check if an expression is reasonably without sideeffect,
//
// and thus better to copy into multiple places rather than to cache in a temporary variable
// - this is mostly subjective beyond the requirement that the expression not be sideeffecting
func isSimpleCopiableExpression(expression *ast.Expression) bool {
return ast.IsStringLiteralLike(expression) ||
ast.IsNumericLiteral(expression) ||
ast.IsKeywordKind(expression.Kind) ||
ast.IsIdentifier(expression)
}

// A simple inlinable expression is an expression which can be copied into multiple locations
// without risk of repeating any sideeffects and whose value could not possibly change between
// any such locations
func isSimpleInlineableExpression(expression *ast.Expression) bool {
return !ast.IsIdentifier(expression) && isSimpleCopiableExpression(expression)
return !ast.IsIdentifier(expression) && transformers.IsSimpleCopiableExpression(expression)
}
14 changes: 14 additions & 0 deletions internal/transformers/utilities.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,17 @@ func SingleOrMany(nodes []*ast.Node, factory *printer.NodeFactory) *ast.Node {
}
return factory.NewSyntaxList(nodes)
}

// Used in the module transformer to check if an expression is reasonably without sideeffect,
//
// and thus better to copy into multiple places rather than to cache in a temporary variable
// - this is mostly subjective beyond the requirement that the expression not be sideeffecting
//
// Also used by the logical assignment downleveling transform to skip temp variables when they're
// not needed.
func IsSimpleCopiableExpression(expression *ast.Expression) bool {
return ast.IsStringLiteralLike(expression) ||
ast.IsNumericLiteral(expression) ||
ast.IsKeywordKind(expression.Kind) ||
ast.IsIdentifier(expression)
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ function f2() {

//// [equalityWithtNullishCoalescingAssignment.js]
function f1(a) {
a ??= true;
a ?? (a = true);
if (a === false) {
console.log(a);
}
}
f1(false);
function f2() {
let x = 0;
x ??= 1;
x ?? (x = 1);
if (x === 0) {
console.log(x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//// [equalityWithtNullishCoalescingAssignment.js]
function f1(a) {
- a !== null && a !== void 0 ? a : (a = true);
+ a ??= true;
+ a ?? (a = true);
if (a === false) {
console.log(a);
}
Expand All @@ -14,7 +14,7 @@
function f2() {
let x = 0;
- x !== null && x !== void 0 ? x : (x = 1);
+ x ??= 1;
+ x ?? (x = 1);
if (x === 0) {
console.log(x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ function f2() {

//// [equalityWithtNullishCoalescingAssignment.js]
function f1(a) {
a ??= true;
a ?? (a = true);
if (a === false) {
console.log(a);
}
}
f1(false);
function f2() {
let x = 0;
x ??= 1;
x ?? (x = 1);
if (x === 0) {
console.log(x);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
-"use strict";
function f1(a) {
- a !== null && a !== void 0 ? a : (a = true);
+ a ??= true;
+ a ?? (a = true);
if (a === false) {
console.log(a);
}
Expand All @@ -16,7 +16,7 @@
function f2() {
let x = 0;
- x !== null && x !== void 0 ? x : (x = 1);
+ x ??= 1;
+ x ?? (x = 1);
if (x === 0) {
console.log(x);
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ i ??= 42


//// [logicalAssignment1.js]
a &&= "foo";
b ||= "foo";
c ??= "foo";
d &&= 42;
e ||= 42;
f ??= 42;
g &&= 42;
h ||= 42;
i ??= 42;
a && (a = "foo");
b || (b = "foo");
c ?? (c = "foo");
d && (d = 42);
e || (e = 42);
f ?? (f = 42);
g && (g = 42);
h || (h = 42);
i ?? (i = 42);
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@

//// [logicalAssignment1.js]
-"use strict";
-a && (a = "foo");
-b || (b = "foo");
a && (a = "foo");
b || (b = "foo");
-c !== null && c !== void 0 ? c : (c = "foo");
-d && (d = 42);
-e || (e = 42);
+c ?? (c = "foo");
d && (d = 42);
e || (e = 42);
-f !== null && f !== void 0 ? f : (f = 42);
-g && (g = 42);
-h || (h = 42);
+f ?? (f = 42);
g && (g = 42);
h || (h = 42);
-i !== null && i !== void 0 ? i : (i = 42);
+a &&= "foo";
+b ||= "foo";
+c ??= "foo";
+d &&= 42;
+e ||= 42;
+f ??= 42;
+g &&= 42;
+h ||= 42;
+i ??= 42;
+i ?? (i = 42);
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ i ??= 42


//// [logicalAssignment1.js]
a &&= "foo";
b ||= "foo";
c ??= "foo";
d &&= 42;
e ||= 42;
f ??= 42;
g &&= 42;
h ||= 42;
i ??= 42;
a && (a = "foo");
b || (b = "foo");
c ?? (c = "foo");
d && (d = 42);
e || (e = 42);
f ?? (f = 42);
g && (g = 42);
h || (h = 42);
i ?? (i = 42);
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@

//// [logicalAssignment1.js]
-"use strict";
-a && (a = "foo");
-b || (b = "foo");
-c ?? (c = "foo");
-d && (d = 42);
-e || (e = 42);
-f ?? (f = 42);
-g && (g = 42);
-h || (h = 42);
-i ?? (i = 42);
+a &&= "foo";
+b ||= "foo";
+c ??= "foo";
+d &&= 42;
+e ||= 42;
+f ??= 42;
+g &&= 42;
+h ||= 42;
+i ??= 42;
a && (a = "foo");
b || (b = "foo");
c ?? (c = "foo");
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ oobj["obj"][incr()] ??= incr();


//// [logicalAssignment10.js]
var _a, _b, _c;
var count = 0;
var obj = {};
function incr() {
Expand All @@ -24,5 +25,5 @@ function incr() {
const oobj = {
obj
};
obj[incr()] ??= incr();
oobj["obj"][incr()] ??= incr();
obj[_a = incr()] ?? (obj[_a] = incr());
(_b = oobj["obj"])[_c = incr()] ?? (_b[_c] = incr());
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
//// [logicalAssignment10.js]
-var _a, _b;
-var _c, _d, _e;
+var _a, _b, _c;
var count = 0;
var obj = {};
function incr() {
@@= skipped -10, +8 lines =@@
@@= skipped -10, +9 lines =@@
const oobj = {
obj
};
-(_a = obj[_c = incr()]) !== null && _a !== void 0 ? _a : (obj[_c] = incr());
-(_b = (_d = oobj["obj"])[_e = incr()]) !== null && _b !== void 0 ? _b : (_d[_e] = incr());
+obj[incr()] ??= incr();
+oobj["obj"][incr()] ??= incr();
+obj[_a = incr()] ?? (obj[_a] = incr());
+(_b = oobj["obj"])[_c = incr()] ?? (_b[_c] = incr());
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ oobj["obj"][incr()] ??= incr();


//// [logicalAssignment10.js]
var _a, _b, _c;
var count = 0;
var obj = {};
function incr() {
Expand All @@ -24,5 +25,5 @@ function incr() {
const oobj = {
obj
};
obj[incr()] ??= incr();
oobj["obj"][incr()] ??= incr();
obj[_a = incr()] ?? (obj[_a] = incr());
(_b = oobj["obj"])[_c = incr()] ?? (_b[_c] = incr());
Loading