Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Sep 20, 2024
1 parent 1e25dce commit a39dd1d
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions crates/swc_ecma_minifier/src/compress/optimize/sequences.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use swc_ecma_usage_analyzer::{
};
use swc_ecma_utils::{
contains_arguments, contains_this_expr, prepend_stmts, ExprExt, IdentUsageFinder, StmtLike,
Type, Value,
};
use swc_ecma_visit::{noop_visit_type, Visit, VisitWith};
#[cfg(feature = "debug")]
Expand Down Expand Up @@ -2379,6 +2380,8 @@ impl Optimizer<'_> {
Mergable::Drop => return Ok(false),
};

let a_type = a_right.as_deref().map(|a| a.get_type());

if let Some(a_right) = a_right {
if a_right.is_this() || a_right.is_ident_ref_to("arguments") {
return Ok(false);
Expand Down Expand Up @@ -2482,8 +2485,13 @@ impl Optimizer<'_> {
_ => None,
};

let Some(a_type) = a_type else {
return Ok(false);
};
let b_type = b.right.get_type();

if let Some(a_op) = a_op {
if can_drop_op_for(a_op, b.op) {
if can_drop_op_for(a_op, b.op, a_type, b_type) {
if b_left.to_id() == left_id.to_id() {
if let Some(bin_op) = b.op.to_update() {
report_change!(
Expand Down Expand Up @@ -2715,13 +2723,17 @@ pub(crate) fn is_trivial_lit(e: &Expr) -> bool {
}

/// This assumes `a.left.to_id() == b.left.to_id()`
fn can_drop_op_for(a: AssignOp, b: AssignOp) -> bool {
fn can_drop_op_for(a: AssignOp, b: AssignOp, a_type: Value<Type>, b_type: Value<Type>) -> bool {
if a == op!("=") {
return true;
}

if a == b {
return matches!(a, op!("+=") | op!("*="));
if a == op!("+=") && a_type.is_known() && a_type == b_type {
return true;
}

return matches!(a, op!("*="));
}

false
Expand Down

0 comments on commit a39dd1d

Please sign in to comment.