diff --git a/crates/oxc_minifier/src/ast_passes/fold_constants.rs b/crates/oxc_minifier/src/ast_passes/fold_constants.rs index 9e915a76ae00f..74f925555a93e 100644 --- a/crates/oxc_minifier/src/ast_passes/fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/fold_constants.rs @@ -2,7 +2,7 @@ //! //! -use std::{cmp::Ordering, mem}; +use std::cmp::Ordering; use num_bigint::BigInt; @@ -610,17 +610,17 @@ impl<'a> FoldConstants<'a> { if (boolean_value && op == LogicalOperator::Or) || (!boolean_value && op == LogicalOperator::And) { - return Some(self.move_out_expression(&mut logical_expr.left)); + return Some(self.ast.move_expression(&mut logical_expr.left)); } else if !logical_expr.left.may_have_side_effects() { // (FALSE || x) => x // (TRUE && x) => x - return Some(self.move_out_expression(&mut logical_expr.right)); + return Some(self.ast.move_expression(&mut logical_expr.right)); } // Left side may have side effects, but we know its boolean value. // e.g. true_with_sideeffects || foo() => true_with_sideeffects, foo() // or: false_with_sideeffects && foo() => false_with_sideeffects, foo() - let left = self.move_out_expression(&mut logical_expr.left); - let right = self.move_out_expression(&mut logical_expr.right); + let left = self.ast.move_expression(&mut logical_expr.left); + let right = self.ast.move_expression(&mut logical_expr.right); let mut vec = self.ast.vec_with_capacity(2); vec.push(left); vec.push(right); @@ -637,8 +637,8 @@ impl<'a> FoldConstants<'a> { if !right_boolean && left_child_op == LogicalOperator::Or || right_boolean && left_child_op == LogicalOperator::And { - let left = self.move_out_expression(&mut left_child.left); - let right = self.move_out_expression(&mut logical_expr.right); + let left = self.ast.move_expression(&mut left_child.left); + let right = self.ast.move_expression(&mut logical_expr.right); let logic_expr = self.ast.expression_logical( logical_expr.span, left, @@ -695,7 +695,7 @@ impl<'a> FoldConstants<'a> { let should_fold = Self::try_minimize_not(&mut unary_expr.argument); if should_fold { - Some(self.move_out_expression(&mut unary_expr.argument)) + Some(self.ast.move_expression(&mut unary_expr.argument)) } else { None } @@ -708,11 +708,6 @@ impl<'a> FoldConstants<'a> { folded_expr } - fn move_out_expression(&mut self, expr: &mut Expression<'a>) -> Expression<'a> { - let null_expr = self.ast.expression_null_literal(expr.span()); - mem::replace(expr, null_expr) - } - /// ported from [closure compiler](https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java#L401-L435) fn try_minimize_not(expr: &mut Expression<'a>) -> bool { let span = &mut expr.span();