diff --git a/crates/oxc_minifier/src/peephole/remove_dead_code.rs b/crates/oxc_minifier/src/peephole/remove_dead_code.rs index 80afb469af14e..e46e163dedad7 100644 --- a/crates/oxc_minifier/src/peephole/remove_dead_code.rs +++ b/crates/oxc_minifier/src/peephole/remove_dead_code.rs @@ -177,13 +177,14 @@ impl<'a, 'b> PeepholeOptimizations { _ => {} } - if let Some(boolean) = if_stmt.test.get_side_free_boolean_value(&ctx) { + if let Some(boolean) = if_stmt.test.evaluate_value_to_boolean(&ctx) { + let test_has_side_effects = if_stmt.test.may_have_side_effects(&ctx); // Use "1" and "0" instead of "true" and "false" to be shorter. - // And also prevent swapping consequent and alternate when `!0` is encourtnered. - if let Expression::BooleanLiteral(b) = &if_stmt.test { + // And also prevent swapping consequent and alternate when `!0` is encountered. + if !test_has_side_effects { if_stmt.test = ctx.ast.expression_numeric_literal( - b.span, - if b.value { 1.0 } else { 0.0 }, + if_stmt.test.span(), + if boolean { 1.0 } else { 0.0 }, None, NumberBase::Decimal, ); @@ -196,7 +197,9 @@ impl<'a, 'b> PeepholeOptimizations { } else { keep_var.visit_statement(&if_stmt.consequent); }; - if let Some(var_stmt) = keep_var.get_variable_declaration_statement() { + let var_stmt = keep_var.get_variable_declaration_statement(); + let has_var_stmt = var_stmt.is_some(); + if let Some(var_stmt) = var_stmt { if boolean { if_stmt.alternate = Some(var_stmt); } else { @@ -204,6 +207,21 @@ impl<'a, 'b> PeepholeOptimizations { } return None; } + if test_has_side_effects { + if !has_var_stmt { + if boolean { + if_stmt.alternate = None; + } else { + if_stmt.consequent = ctx.ast.statement_empty(if_stmt.consequent.span()); + } + } + return Some(ctx.ast.statement_if( + if_stmt.span, + ctx.ast.move_expression(&mut if_stmt.test), + ctx.ast.move_statement(&mut if_stmt.consequent), + if_stmt.alternate.as_mut().map(|alternate| ctx.ast.move_statement(alternate)), + )); + } return Some(if boolean { ctx.ast.move_statement(&mut if_stmt.consequent) } else { diff --git a/crates/oxc_minifier/tests/peephole/dead_code_elimination.rs b/crates/oxc_minifier/tests/peephole/dead_code_elimination.rs index 107493d2f4149..f2f594f226669 100644 --- a/crates/oxc_minifier/tests/peephole/dead_code_elimination.rs +++ b/crates/oxc_minifier/tests/peephole/dead_code_elimination.rs @@ -68,9 +68,11 @@ fn dce_if_statement() { test("if (!false && xxx) { foo }", "if (xxx) foo"); test("if (!true && yyy) { foo } else { bar }", "bar"); + test("if (xxx && false) { foo } else { bar }", "if (xxx && false); else bar"); test("if (true || xxx) { foo }", "foo"); test("if (false || xxx) { foo }", "if (xxx) foo"); + test("if (xxx || true) { foo } else { bar }", "if (xxx || true) foo"); test("if ('production' == 'production') { foo } else { bar }", "foo"); test("if ('development' == 'production') { foo } else { bar }", "bar"); diff --git a/crates/oxc_minifier/tests/peephole/mod.rs b/crates/oxc_minifier/tests/peephole/mod.rs index 0c0f3fe625bcb..b6283a7f0fa1c 100644 --- a/crates/oxc_minifier/tests/peephole/mod.rs +++ b/crates/oxc_minifier/tests/peephole/mod.rs @@ -57,7 +57,7 @@ fn integration() { } console.log(c, d); ", - "if (console.log('effect'), !1) for (var c = 1, c; unknownGlobal; unknownGlobal) var d; + "if (console.log('effect'), !1) var c, c, d; console.log(c, d); ", );