Skip to content

Port ES2016 transform #1371

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 2 commits 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
24 changes: 24 additions & 0 deletions internal/printer/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,30 @@ func (f *NodeFactory) InlineExpressions(expressions []*ast.Expression) *ast.Expr
// Utilities
//

func (f *NodeFactory) NewMethodCall(object *ast.Node, methodName *ast.Node, argumentsList []*ast.Node) *ast.Node {
// Preserve the optionality of `object`.
if ast.IsCallExpression(object) && (object.Flags&ast.NodeFlagsOptionalChain != 0) {
return f.NewCallExpression(
f.NewPropertyAccessExpression(object, nil, methodName, ast.NodeFlagsNone),
nil,
nil,
f.NewNodeList(argumentsList),
ast.NodeFlagsOptionalChain,
)
}
return f.NewCallExpression(
f.NewPropertyAccessExpression(object, nil, methodName, ast.NodeFlagsNone),
nil,
nil,
f.NewNodeList(argumentsList),
ast.NodeFlagsNone,
)
}

func (f *NodeFactory) NewGlobalMethodCall(globalObjectName string, methodName string, argumentsList []*ast.Node) *ast.Node {
return f.NewMethodCall(f.NewIdentifier(globalObjectName), f.NewIdentifier(methodName), argumentsList)
}

// Determines whether a node is a parenthesized expression that can be ignored when recreating outer expressions.
//
// A parenthesized expression can be ignored when all of the following are true:
Expand Down
4 changes: 2 additions & 2 deletions internal/printer/namegenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ func (g *NameGenerator) makeTempVariableName(flags tempFlags, reservedInNestedSc
for {
count := tempFlags & tempFlagsCountMask
tempFlags++
// Skip over 'i'
if count != 8 {
// Skip over 'i' and 'n'
if count != 8 && count != 13 {
var name string
if count < 26 {
name = fmt.Sprintf("_%c", 'a'+byte(count))
Expand Down
73 changes: 72 additions & 1 deletion internal/transformers/estransforms/exponentiation.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,78 @@ type exponentiationTransformer struct {
}

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

func (ch *exponentiationTransformer) visitBinaryExpression(node *ast.BinaryExpression) *ast.Node {
switch node.OperatorToken.Kind {
case ast.KindAsteriskAsteriskEqualsToken:
return ch.visitExponentiationAssignmentExpression(node)
case ast.KindAsteriskAsteriskToken:
return ch.visitExponentiationExpression(node)
}
return ch.Visitor().VisitEachChild(node.AsNode())
}

func (ch *exponentiationTransformer) visitExponentiationAssignmentExpression(node *ast.BinaryExpression) *ast.Node {
var target *ast.Node
var value *ast.Node
left := ch.Visitor().VisitNode(node.Left)
right := ch.Visitor().VisitNode(node.Right)
if ast.IsElementAccessExpression(left) {
// Transforms `a[x] **= b` into `(_a = a)[_x = x] = Math.pow(_a[_x], b)`
expressionTemp := ch.Factory().NewTempVariable()
ch.EmitContext().AddVariableDeclaration(expressionTemp)
argumentExpressionTemp := ch.Factory().NewTempVariable()
ch.EmitContext().AddVariableDeclaration(argumentExpressionTemp)

objExpr := ch.Factory().NewAssignmentExpression(expressionTemp, left.AsElementAccessExpression().Expression)
objExpr.Loc = left.AsElementAccessExpression().Expression.Loc
accessExpr := ch.Factory().NewAssignmentExpression(argumentExpressionTemp, left.AsElementAccessExpression().ArgumentExpression)
accessExpr.Loc = left.AsElementAccessExpression().ArgumentExpression.Loc

target = ch.Factory().NewElementAccessExpression(objExpr, nil, accessExpr, ast.NodeFlagsNone)

value = ch.Factory().NewElementAccessExpression(expressionTemp, nil, argumentExpressionTemp, ast.NodeFlagsNone)
value.Loc = left.Loc
} else if ast.IsPropertyAccessExpression(left) {
// Transforms `a.x **= b` into `(_a = a).x = Math.pow(_a.x, b)`
expressionTemp := ch.Factory().NewTempVariable()
ch.EmitContext().AddVariableDeclaration(expressionTemp)
assignment := ch.Factory().NewAssignmentExpression(expressionTemp, left.Expression())
assignment.Loc = left.Expression().Loc
target = ch.Factory().NewPropertyAccessExpression(assignment, nil, left.Name(), ast.NodeFlagsNone)
target.Loc = left.Loc

value = ch.Factory().NewPropertyAccessExpression(expressionTemp, nil, left.Name(), ast.NodeFlagsNone)
value.Loc = left.Loc
} else {
// Transforms `a **= b` into `a = Math.pow(a, b)`
target = left
value = left
}

rhs := ch.Factory().NewGlobalMethodCall("Math", "pow", []*ast.Node{value, right})
rhs.Loc = node.Loc
result := ch.Factory().NewAssignmentExpression(target, rhs)
result.Loc = node.Loc
return result
}

func (ch *exponentiationTransformer) visitExponentiationExpression(node *ast.BinaryExpression) *ast.Node {
left := ch.Visitor().VisitNode(node.Left)
right := ch.Visitor().VisitNode(node.Right)
result := ch.Factory().NewGlobalMethodCall("Math", "pow", []*ast.Node{left, right})
result.Loc = node.Loc
return result
}

func newExponentiationTransformer(emitContext *printer.EmitContext) *transformers.Transformer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ foo **= BigInt(2); // should error


//// [bigIntWithTargetLessThanES2016.js]
BigInt(1) ** BigInt(1); // should error
Math.pow(BigInt(1), BigInt(1)); // should error
let foo = BigInt(2);
foo **= BigInt(2); // should error
foo = Math.pow(foo, BigInt(2)); // should error

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export const power = <Num extends number, PowerOf extends number>(
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.power = void 0;
const power = (num, powerOf) => (num ** powerOf);
const power = (num, powerOf) => (Math.pow(num, powerOf));
exports.power = power;


Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ async function binaryCompoundAssignment8() {
x[await a] += await y;
}
async function binaryExponentiation() {
(await x) ** y;
x ** await y;
Math.pow((await x), y);
Math.pow(x, await y);
}
async function binaryComma0() {
return (await x), y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,8 @@
+ x[await a] += await y;
+}
+async function binaryExponentiation() {
+ (await x) ** y;
+ x ** await y;
+ Math.pow((await x), y);
+ Math.pow(x, await y);
+}
+async function binaryComma0() {
+ return (await x), y;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ const at8: [1] = at7;
// #Repro from #30839
let point = [3, 4];
function distanceFromOrigin([x, y]) {
return Math.sqrt(x ** 2 + y ** 2);
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
}
distanceFromOrigin(point);
arryFn(point);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,20 @@ var a;
var b;
var c;
var x1;
x1 **= a;
x1 **= b;
x1 **= c;
x1 **= null;
x1 **= undefined;
x1 = Math.pow(x1, a);
x1 = Math.pow(x1, b);
x1 = Math.pow(x1, c);
x1 = Math.pow(x1, null);
x1 = Math.pow(x1, undefined);
var x2;
x2 **= a;
x2 **= b;
x2 **= c;
x2 **= null;
x2 **= undefined;
x2 = Math.pow(x2, a);
x2 = Math.pow(x2, b);
x2 = Math.pow(x2, c);
x2 = Math.pow(x2, null);
x2 = Math.pow(x2, undefined);
var x3;
x3 **= a;
x3 **= b;
x3 **= c;
x3 **= null;
x3 **= undefined;
x3 = Math.pow(x3, a);
x3 = Math.pow(x3, b);
x3 = Math.pow(x3, c);
x3 = Math.pow(x3, null);
x3 = Math.pow(x3, undefined);

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -71,52 +71,52 @@ var E;
var a;
var b;
var x1;
x1 **= a;
x1 **= b;
x1 **= true;
x1 **= 0;
x1 **= '';
x1 **= E.a;
x1 **= {};
x1 **= null;
x1 **= undefined;
x1 = Math.pow(x1, a);
x1 = Math.pow(x1, b);
x1 = Math.pow(x1, true);
x1 = Math.pow(x1, 0);
x1 = Math.pow(x1, '');
x1 = Math.pow(x1, E.a);
x1 = Math.pow(x1, {});
x1 = Math.pow(x1, null);
x1 = Math.pow(x1, undefined);
var x2;
x2 **= a;
x2 **= b;
x2 **= true;
x2 **= 0;
x2 **= '';
x2 **= E.a;
x2 **= {};
x2 **= null;
x2 **= undefined;
x2 = Math.pow(x2, a);
x2 = Math.pow(x2, b);
x2 = Math.pow(x2, true);
x2 = Math.pow(x2, 0);
x2 = Math.pow(x2, '');
x2 = Math.pow(x2, E.a);
x2 = Math.pow(x2, {});
x2 = Math.pow(x2, null);
x2 = Math.pow(x2, undefined);
var x3;
x3 **= a;
x3 **= b;
x3 **= true;
x3 **= 0;
x3 **= '';
x3 **= E.a;
x3 **= {};
x3 **= null;
x3 **= undefined;
x3 = Math.pow(x3, a);
x3 = Math.pow(x3, b);
x3 = Math.pow(x3, true);
x3 = Math.pow(x3, 0);
x3 = Math.pow(x3, '');
x3 = Math.pow(x3, E.a);
x3 = Math.pow(x3, {});
x3 = Math.pow(x3, null);
x3 = Math.pow(x3, undefined);
var x4;
x4 **= a;
x4 **= b;
x4 **= true;
x4 **= 0;
x4 **= '';
x4 **= E.a;
x4 **= {};
x4 **= null;
x4 **= undefined;
x4 = Math.pow(x4, a);
x4 = Math.pow(x4, b);
x4 = Math.pow(x4, true);
x4 = Math.pow(x4, 0);
x4 = Math.pow(x4, '');
x4 = Math.pow(x4, E.a);
x4 = Math.pow(x4, {});
x4 = Math.pow(x4, null);
x4 = Math.pow(x4, undefined);
var x5;
x5 **= b;
x5 **= true;
x5 **= '';
x5 **= {};
x5 = Math.pow(x5, b);
x5 = Math.pow(x5, true);
x5 = Math.pow(x5, '');
x5 = Math.pow(x5, {});
var x6;
x6 **= b;
x6 **= true;
x6 **= '';
x6 **= {};
x6 = Math.pow(x6, b);
x6 = Math.pow(x6, true);
x6 = Math.pow(x6, '');
x6 = Math.pow(x6, {});
Loading