Skip to content

Commit

Permalink
feat(minifier): minimize do{}while(true) -> do;while(true)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 7, 2025
1 parent e3ff81e commit fce5725
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
26 changes: 15 additions & 11 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,21 @@ impl Gen for DoWhileStatement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_str("do ");
if let Statement::BlockStatement(block) = &self.body {
p.print_block_statement(block, ctx);
p.print_soft_space();
} else {
p.print_soft_newline();
p.indent();
self.body.print(p, ctx);
p.print_semicolon_if_needed();
p.dedent();
p.print_indent();
p.print_str("do");
match &self.body {
Statement::BlockStatement(block) => {
p.print_block_statement(block, ctx);
p.print_soft_space();
}
Statement::EmptyStatement(s) => s.print(p, ctx),
_ => {
p.print_soft_newline();
p.indent();
self.body.print(p, ctx);
p.print_semicolon_if_needed();
p.dedent();
p.print_indent();
}
}
p.print_str("while");
p.print_soft_space();
Expand Down
6 changes: 6 additions & 0 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ fn for_stmt() {
);
}

#[test]
fn do_while_stmt() {
test("do ; while (true);", "do;\nwhile (true);\n");
test_minify("do ; while (true);", "do;while(true);");
}

#[test]
fn if_stmt() {
test(
Expand Down
24 changes: 14 additions & 10 deletions crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,19 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
if stmt.body.len() == 1 && !stmt.body[0].is_declaration() {
return Some(stmt.body.remove(0));
}
if stmt.body.len() == 0
&& (ctx.parent().is_while_statement()
|| ctx.parent().is_for_statement()
|| ctx.parent().is_for_in_statement()
|| ctx.parent().is_for_of_statement()
|| ctx.parent().is_block_statement()
|| ctx.parent().is_program())
{
// Remove the block if it is empty and the parent is a block statement.
return Some(ctx.ast.statement_empty(stmt.span));
if stmt.body.len() == 0 {
let parent = ctx.parent();
if parent.is_while_statement()
|| parent.is_do_while_statement()
|| parent.is_for_statement()
|| parent.is_for_in_statement()
|| parent.is_for_of_statement()
|| parent.is_block_statement()
|| parent.is_program()
{
// Remove the block if it is empty and the parent is a block statement.
return Some(ctx.ast.statement_empty(stmt.span));
}
}
None
}
Expand Down Expand Up @@ -563,6 +566,7 @@ mod test {
// fold("for (x of y) {x}", "for(x of y);");
fold("for (let x = 1; x <10; x++ ) {}", "for (let x = 1; x <10; x++ );");
fold("for (var x = 1; x <10; x++ ) {}", "for (var x = 1; x <10; x++ );");
fold("do { } while (true)", "do;while(true)");
}

#[test]
Expand Down

0 comments on commit fce5725

Please sign in to comment.