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
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ mod test {
// Verify function deletion tracking.
// test("if(!!true||function(){}) {}", "if(1) {}");
// Don't bother with FOR inits as there are normalized out.
test("for(!!true;;) foo()", "for(!0;;) foo()");
test("for(!!true;;) foo()", "for(;;) foo()");

// These test tryMinimizeCondition
test("for(;!!x;) foo()", "for(;x;) foo()");
Expand Down
27 changes: 27 additions & 0 deletions crates/oxc_minifier/src/peephole/remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ impl<'a, 'b> PeepholeOptimizations {
for_stmt: &mut ForStatement<'a>,
ctx: Ctx<'a, 'b>,
) -> Option<Statement<'a>> {
if let Some(init) = &mut for_stmt.init {
if let Some(init) = init.as_expression_mut() {
if self.remove_unused_expression(init, ctx) {
for_stmt.init = None;
self.mark_current_function_as_changed();
}
}
}
if let Some(update) = &mut for_stmt.update {
if self.remove_unused_expression(update, ctx) {
for_stmt.update = None;
self.mark_current_function_as_changed();
}
}

let test_boolean =
for_stmt.test.as_ref().and_then(|test| test.evaluate_value_to_boolean(&ctx));
if for_stmt.test.as_ref().is_some_and(|test| test.may_have_side_effects(&ctx)) {
Expand Down Expand Up @@ -591,4 +606,16 @@ mod test {
test("while(true) { throw a; unreachable;}", "for(;;) throw a");
test("while(true) { return a; unreachable;}", "for(;;) return a");
}

#[test]
fn remove_unused_expressions_in_for() {
test(
"var i; for (i = 0, 0; i < 10; i++) foo(i);",
"var i; for (i = 0; i < 10; i++) foo(i);",
);
test(
"var i; for (i = 0; i < 10; 0, i++, 0) foo(i);",
"var i; for (i = 0; i < 10; i++) foo(i);",
);
}
}
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 && !0) var d;
"if ((() => console.log('effect'))(), !1) for (var c = 1, c; unknownGlobal; unknownGlobal) var d;
console.log(c, d);
",
);
Expand Down
Loading