mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(transformer): exponentiation transform: break up into functions (#6301)
Pure refactor.
This commit is contained in:
parent
bd81c5134c
commit
15cc8afd2e
1 changed files with 38 additions and 21 deletions
|
|
@ -65,7 +65,7 @@ impl<'a, 'ctx> Traverse<'a> for ExponentiationOperator<'a, 'ctx> {
|
||||||
// NOTE: Bail bigint arguments to `Math.pow`, which are runtime errors.
|
// NOTE: Bail bigint arguments to `Math.pow`, which are runtime errors.
|
||||||
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
match expr {
|
match expr {
|
||||||
// left ** right
|
// `left ** right`
|
||||||
Expression::BinaryExpression(binary_expr) => {
|
Expression::BinaryExpression(binary_expr) => {
|
||||||
if binary_expr.operator != BinaryOperator::Exponential
|
if binary_expr.operator != BinaryOperator::Exponential
|
||||||
|| binary_expr.left.is_big_int_literal()
|
|| binary_expr.left.is_big_int_literal()
|
||||||
|
|
@ -74,11 +74,9 @@ impl<'a, 'ctx> Traverse<'a> for ExponentiationOperator<'a, 'ctx> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let left = ctx.ast.move_expression(&mut binary_expr.left);
|
Self::convert_binary_expression(expr, ctx);
|
||||||
let right = ctx.ast.move_expression(&mut binary_expr.right);
|
|
||||||
*expr = Self::math_pow(left, right, ctx);
|
|
||||||
}
|
}
|
||||||
// left **= right
|
// `left **= right`
|
||||||
Expression::AssignmentExpression(assign_expr) => {
|
Expression::AssignmentExpression(assign_expr) => {
|
||||||
if assign_expr.operator != AssignmentOperator::Exponential
|
if assign_expr.operator != AssignmentOperator::Exponential
|
||||||
|| assign_expr.right.is_big_int_literal()
|
|| assign_expr.right.is_big_int_literal()
|
||||||
|
|
@ -86,6 +84,33 @@ impl<'a, 'ctx> Traverse<'a> for ExponentiationOperator<'a, 'ctx> {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.convert_assignment_expression(expr, ctx);
|
||||||
|
}
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
|
||||||
|
/// Convert `BinaryExpression`.
|
||||||
|
/// `left ** right` -> `Math.pow(left, right)`
|
||||||
|
fn convert_binary_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
|
let binary_expr = match ctx.ast.move_expression(expr) {
|
||||||
|
Expression::BinaryExpression(binary_expr) => binary_expr.unbox(),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
*expr = Self::math_pow(binary_expr.left, binary_expr.right, ctx);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Convert `AssignmentExpression`.
|
||||||
|
// `left **= right` -> `left = Math.pow(left, right)`
|
||||||
|
fn convert_assignment_expression(
|
||||||
|
&mut self,
|
||||||
|
expr: &mut Expression<'a>,
|
||||||
|
ctx: &mut TraverseCtx<'a>,
|
||||||
|
) {
|
||||||
|
let Expression::AssignmentExpression(assign_expr) = expr else { unreachable!() };
|
||||||
|
|
||||||
let mut nodes = ctx.ast.vec();
|
let mut nodes = ctx.ast.vec();
|
||||||
let Some(Exploded { reference, uid }) =
|
let Some(Exploded { reference, uid }) =
|
||||||
self.explode(&mut assign_expr.left, &mut nodes, ctx)
|
self.explode(&mut assign_expr.left, &mut nodes, ctx)
|
||||||
|
|
@ -94,21 +119,13 @@ impl<'a, 'ctx> Traverse<'a> for ExponentiationOperator<'a, 'ctx> {
|
||||||
};
|
};
|
||||||
let right = ctx.ast.move_expression(&mut assign_expr.right);
|
let right = ctx.ast.move_expression(&mut assign_expr.right);
|
||||||
let right = Self::math_pow(uid, right, ctx);
|
let right = Self::math_pow(uid, right, ctx);
|
||||||
let assign_expr = ctx.ast.expression_assignment(
|
let assign_expr =
|
||||||
SPAN,
|
ctx.ast.expression_assignment(SPAN, AssignmentOperator::Assign, reference, right);
|
||||||
AssignmentOperator::Assign,
|
|
||||||
reference,
|
|
||||||
right,
|
|
||||||
);
|
|
||||||
nodes.push(assign_expr);
|
nodes.push(assign_expr);
|
||||||
|
|
||||||
*expr = ctx.ast.expression_sequence(SPAN, nodes);
|
*expr = ctx.ast.expression_sequence(SPAN, nodes);
|
||||||
}
|
}
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
|
|
||||||
fn clone_expression(expr: &Expression<'a>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
|
fn clone_expression(expr: &Expression<'a>, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
|
||||||
match expr {
|
match expr {
|
||||||
Expression::Identifier(ident) => ctx.ast.expression_from_identifier_reference(
|
Expression::Identifier(ident) => ctx.ast.expression_from_identifier_reference(
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue