Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions crates/oxc_minifier/src/peephole/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand All @@ -196,14 +197,31 @@ 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 {
if_stmt.consequent = var_stmt;
}
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 {
Expand Down
2 changes: 2 additions & 0 deletions crates/oxc_minifier/tests/peephole/dead_code_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/peephole/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
",
);
Expand Down
Loading