Skip to content
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

refactor(minifier): remove duplicated helper move_out_expression #5007

Merged
merged 1 commit into from
Aug 20, 2024
Merged
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
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)
}

Boshen marked this conversation as resolved.
Show resolved Hide resolved
/// 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