From 07fe45b416a94b77ea6a2043adca7ffa89dfc52e Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 27 Sep 2024 19:21:55 +0000 Subject: [PATCH] refactor(transformer): exponentiation operator: convert to match (#6123) In exponentiation operator transform, the first branch handling `**` was "falling through" to also run the 2nd branch which handles `**=`. The first branch replaces `**` with `Math.pow`, so it can never match on the 2nd branch. Convert it to a `match` instead, so it only executes one branch or the other. --- .../src/es2016/exponentiation_operator.rs | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/crates/oxc_transformer/src/es2016/exponentiation_operator.rs b/crates/oxc_transformer/src/es2016/exponentiation_operator.rs index 5cd69a407..03412ee9b 100644 --- a/crates/oxc_transformer/src/es2016/exponentiation_operator.rs +++ b/crates/oxc_transformer/src/es2016/exponentiation_operator.rs @@ -95,24 +95,28 @@ impl<'a> Traverse<'a> for ExponentiationOperator<'a> { // NOTE: Bail bigint arguments to `Math.pow`, which are runtime errors. fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { - // left ** right - if let Expression::BinaryExpression(binary_expr) = expr { - if binary_expr.operator == BinaryOperator::Exponential { - if binary_expr.left.is_big_int_literal() || binary_expr.right.is_big_int_literal() { + match expr { + // left ** right + Expression::BinaryExpression(binary_expr) => { + if binary_expr.operator != BinaryOperator::Exponential + || binary_expr.left.is_big_int_literal() + || binary_expr.right.is_big_int_literal() + { return; } + let left = ctx.ast.move_expression(&mut binary_expr.left); let right = ctx.ast.move_expression(&mut binary_expr.right); *expr = Self::math_pow(left, right, ctx); } - } - - // left **= right - if let Expression::AssignmentExpression(assign_expr) = expr { - if assign_expr.operator == AssignmentOperator::Exponential { - if assign_expr.right.is_big_int_literal() { + // left **= right + Expression::AssignmentExpression(assign_expr) => { + if assign_expr.operator != AssignmentOperator::Exponential + || assign_expr.right.is_big_int_literal() + { return; } + let mut nodes = ctx.ast.vec(); let Some(Exploded { reference, uid }) = self.explode(&mut assign_expr.left, &mut nodes, ctx) @@ -130,6 +134,7 @@ impl<'a> Traverse<'a> for ExponentiationOperator<'a> { nodes.push(assign_expr); *expr = ctx.ast.expression_sequence(SPAN, nodes); } + _ => {} } } }