Skip to content

Commit d4fdba9

Browse files
committed
fix(minifier): keep argument spread side effects against empty functions
1 parent df8e639 commit d4fdba9

File tree

2 files changed

+6
-16
lines changed

2 files changed

+6
-16
lines changed

crates/oxc_minifier/src/peephole/remove_dead_code.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -416,22 +416,13 @@ impl<'a> PeepholeOptimizations {
416416
if let Some(reference_id) = ident.reference_id.get() {
417417
if let Some(symbol_id) = ctx.scoping().get_reference(reference_id).symbol_id() {
418418
if ctx.state.empty_functions.contains(&symbol_id) {
419-
if e.arguments.is_empty() {
419+
let mut exprs =
420+
Self::fold_arguments_into_needed_expressions(&mut e.arguments, ctx);
421+
if exprs.is_empty() {
420422
*expr = ctx.ast.void_0(e.span);
421423
ctx.state.changed = true;
422424
return;
423425
}
424-
let mut exprs = ctx.ast.vec();
425-
for arg in e.arguments.drain(..) {
426-
match arg {
427-
Argument::SpreadElement(e) => {
428-
exprs.push(e.unbox().argument);
429-
}
430-
match_expression!(Argument) => {
431-
exprs.push(arg.into_expression());
432-
}
433-
}
434-
}
435426
exprs.push(ctx.ast.void_0(e.span));
436427
*expr = ctx.ast.expression_sequence(e.span, exprs);
437428
ctx.state.changed = true;
@@ -724,8 +715,8 @@ mod test {
724715
test_options("var foo = () => {}; foo()", "", &options);
725716
test_options("var foo = () => {}; foo(a)", "a", &options);
726717
test_options("var foo = () => {}; foo(a, b)", "a, b", &options);
727-
test_options("var foo = () => {}; foo(...a, b)", "a, b", &options);
728-
test_options("var foo = () => {}; foo(...a, ...b)", "a, b", &options);
718+
test_options("var foo = () => {}; foo(...a, b)", "[...a], b", &options);
719+
test_options("var foo = () => {}; foo(...a, ...b)", "[...a], [...b]", &options);
729720
test_options("var foo = () => {}; x = foo()", "x = void 0", &options);
730721
test_options("var foo = () => {}; x = foo(a(), b())", "x = (a(), b(), void 0)", &options);
731722
test_options("var foo = function () {}; foo()", "", &options);

crates/oxc_minifier/src/peephole/remove_unused_expression.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -580,9 +580,8 @@ impl<'a> PeepholeOptimizations {
580580
!call_expr.may_have_side_effects(ctx)
581581
}
582582

583-
fn fold_arguments_into_needed_expressions(
583+
pub fn fold_arguments_into_needed_expressions(
584584
args: &mut Vec<'a, Argument<'a>>,
585-
586585
ctx: &mut Ctx<'a, '_>,
587586
) -> Vec<'a, Expression<'a>> {
588587
ctx.ast.vec_from_iter(args.drain(..).filter_map(|arg| {

0 commit comments

Comments
 (0)