From ed696b5725f23ebf4c4ec045421c4fdf142c73fa Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Thu, 17 Jul 2025 14:04:57 +0000 Subject: [PATCH] fix(minifier): remove more unused assignment expressions (#12364) --- .../src/peephole/remove_dead_code.rs | 4 ++++ .../remove_unused_variable_declaration.rs | 21 ++++++++++++------- tasks/minsize/minsize.snap | 8 +++---- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/crates/oxc_minifier/src/peephole/remove_dead_code.rs b/crates/oxc_minifier/src/peephole/remove_dead_code.rs index 9a4a809e66e89..80d75ad1ca98c 100644 --- a/crates/oxc_minifier/src/peephole/remove_dead_code.rs +++ b/crates/oxc_minifier/src/peephole/remove_dead_code.rs @@ -49,6 +49,10 @@ impl<'a> PeepholeOptimizations { self.try_fold_conditional_expression(e, state, ctx) } Expression::SequenceExpression(e) => self.try_fold_sequence_expression(e, state, ctx), + Expression::AssignmentExpression(_) => { + self.remove_unused_assignment_expression(expr, state, ctx); + None + } _ => None, } { *expr = folded_expr; diff --git a/crates/oxc_minifier/src/peephole/remove_unused_variable_declaration.rs b/crates/oxc_minifier/src/peephole/remove_unused_variable_declaration.rs index 1ead2df217621..26e671f14d499 100644 --- a/crates/oxc_minifier/src/peephole/remove_unused_variable_declaration.rs +++ b/crates/oxc_minifier/src/peephole/remove_unused_variable_declaration.rs @@ -1,6 +1,5 @@ use oxc_allocator::TakeIn; use oxc_ast::ast::*; -use oxc_ecmascript::side_effects::MayHaveSideEffects; use crate::{CompressOptionsUnused, ctx::Ctx}; @@ -79,13 +78,9 @@ impl<'a> PeepholeOptimizations { if !ctx.scoping().get_resolved_references(symbol_id).all(|r| !r.flags().is_read()) { return false; } + *e = assign_expr.right.take_in(ctx.ast); state.changed = true; - if assign_expr.right.may_have_side_effects(ctx) { - *e = assign_expr.right.take_in(ctx.ast); - false - } else { - true - } + false } /// Do remove top level vars in script mode. @@ -132,7 +127,17 @@ mod test { test_options("var x = 1; x = 2;", "", &options); test_options("var x = 1; x = foo();", "foo()", &options); test_same_options("var x = 1; x = 2, foo(x)", &options); - test_same_options("function foo() { var t; return t = x(); } foo();", &options); + test_same_options("function foo() { return t = x(); } foo();", &options); + test_options( + "function foo() { var t; return t = x(); } foo();", + "function foo() { return x(); } foo();", + &options, + ); + test_options( + "function foo(t) { return t = x(); } foo();", + "function foo(t) { return x(); } foo();", + &options, + ); } #[test] diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index cdffd933888cc..f835ace0c522e 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -11,17 +11,17 @@ Original | minified | minified | gzip | gzip | Fixture 544.10 kB | 71.38 kB | 72.48 kB | 25.85 kB | 26.20 kB | lodash.js -555.77 kB | 270.82 kB | 270.13 kB | 88.25 kB | 90.80 kB | d3.js +555.77 kB | 270.80 kB | 270.13 kB | 88.24 kB | 90.80 kB | d3.js 1.01 MB | 440.17 kB | 458.89 kB | 122.37 kB | 126.71 kB | bundle.min.js 1.25 MB | 647 kB | 646.76 kB | 160.28 kB | 163.73 kB | three.js -2.14 MB | 716.11 kB | 724.14 kB | 161.77 kB | 181.07 kB | victory.js +2.14 MB | 716.10 kB | 724.14 kB | 161.76 kB | 181.07 kB | victory.js 3.20 MB | 1.01 MB | 1.01 MB | 324.08 kB | 331.56 kB | echarts.js -6.69 MB | 2.25 MB | 2.31 MB | 463.06 kB | 488.28 kB | antd.js +6.69 MB | 2.24 MB | 2.31 MB | 462.45 kB | 488.28 kB | antd.js -10.95 MB | 3.35 MB | 3.49 MB | 860.92 kB | 915.50 kB | typescript.js +10.95 MB | 3.34 MB | 3.49 MB | 856.90 kB | 915.50 kB | typescript.js