Skip to content

Commit

Permalink
refactor(minifier): remove duplicated helper move_out_expression (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
IWANABETHATGUY committed Aug 20, 2024
1 parent 8d15e65 commit 0f64d10
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions crates/oxc_minifier/src/ast_passes/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
use std::{cmp::Ordering, mem};
use std::cmp::Ordering;

use num_bigint::BigInt;

Expand Down Expand Up @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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
}
Expand All @@ -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();
Expand Down

0 comments on commit 0f64d10

Please sign in to comment.