From bb464b2566b1b1d74cbac784185fb6c06b073e01 Mon Sep 17 00:00:00 2001 From: Boshen Date: Tue, 14 Jan 2025 23:28:26 +0800 Subject: [PATCH] fix(minifier): always keep the last value of sequence expression --- .../src/ast_passes/peephole_remove_dead_code.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs index 79bcb8be7c8780..da750d8e0dd64c 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs @@ -496,8 +496,6 @@ impl<'a, 'b> PeepholeRemoveDeadCode { sequence_expr: &mut SequenceExpression<'a>, ctx: Ctx<'a, 'b>, ) -> Option> { - let should_include_ret_val = - !matches!(ctx.parent(), Ancestor::ExpressionStatementExpression(_)); let should_keep_as_sequence_expr = matches!( ctx.parent(), Ancestor::CallExpressionCallee(_) | Ancestor::TaggedTemplateExpressionTag(_) @@ -510,9 +508,7 @@ impl<'a, 'b> PeepholeRemoveDeadCode { let (should_fold, new_len) = sequence_expr.expressions.iter().enumerate().fold( (false, 0), |(mut should_fold, mut new_len), (i, expr)| { - if expr.may_have_side_effects() - || (should_include_ret_val && i == sequence_expr.expressions.len() - 1) - { + if i == sequence_expr.expressions.len() - 1 || expr.may_have_side_effects() { new_len += 1; } else { should_fold = true; @@ -529,7 +525,7 @@ impl<'a, 'b> PeepholeRemoveDeadCode { let mut new_exprs = ctx.ast.vec_with_capacity(new_len); let len = sequence_expr.expressions.len(); for (i, expr) in sequence_expr.expressions.iter_mut().enumerate() { - if expr.may_have_side_effects() || (should_include_ret_val && i == len - 1) { + if i == len - 1 || expr.may_have_side_effects() { new_exprs.push(ctx.ast.move_expression(expr)); } } @@ -744,6 +740,7 @@ mod test { fold("var obj = Object((null, 2, 3), 1, 2);", "var obj = Object(3, 1, 2);"); fold_same("(0 instanceof 0, foo)"); fold_same("(0 in 0, foo)"); + fold_same("React.useEffect(() => (isMountRef.current = false, () => { isMountRef.current = true; }), [])"); } #[test]