Skip to content

Commit

Permalink
fix(codegen): fix white space issue with do statements
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 8, 2025
1 parent 9a5c66a commit 1c84b9c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
11 changes: 8 additions & 3 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ impl Gen for Statement<'_> {
p.print_statement_comments(stmt.span.start);
stmt.print(p, ctx);
}

Self::ImportDeclaration(decl) => {
p.print_statement_comments(decl.span.start);
decl.print(p, ctx);
Expand All @@ -197,7 +196,6 @@ impl Gen for Statement<'_> {
p.print_statement_comments(decl.span.start);
decl.print(p, ctx);
}

Self::VariableDeclaration(decl) => {
p.print_statement_comments(decl.span.start);
p.print_indent();
Expand Down Expand Up @@ -395,6 +393,7 @@ impl Gen for ForInStatement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_space_before_identifier();
p.print_str("for");
p.print_soft_space();
p.print_ascii_byte(b'(');
Expand All @@ -413,6 +412,7 @@ impl Gen for ForOfStatement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_space_before_identifier();
p.print_str("for");
if self.r#await {
p.print_str(" await");
Expand Down Expand Up @@ -460,6 +460,7 @@ impl Gen for WhileStatement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_space_before_identifier();
p.print_str("while");
p.print_soft_space();
p.print_ascii_byte(b'(');
Expand Down Expand Up @@ -541,6 +542,7 @@ impl Gen for SwitchStatement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_space_before_identifier();
p.print_str("switch");
p.print_soft_space();
p.print_ascii_byte(b'(');
Expand Down Expand Up @@ -648,6 +650,7 @@ impl Gen for ThrowStatement<'_> {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_space_before_identifier();
p.print_str("throw");
p.print_soft_space();
p.print_expression(&self.argument);
Expand All @@ -659,6 +662,7 @@ impl Gen for WithStatement<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_space_before_identifier();
p.print_str("with");
p.print_ascii_byte(b'(');
p.print_expression(&self.object);
Expand All @@ -671,6 +675,7 @@ impl Gen for DebuggerStatement {
fn gen(&self, p: &mut Codegen, _ctx: Context) {
p.add_source_mapping(self.span);
p.print_indent();
p.print_space_before_identifier();
p.print_str("debugger");
p.print_semicolon_after_statement();
}
Expand All @@ -679,6 +684,7 @@ impl Gen for DebuggerStatement {
impl Gen for VariableDeclaration<'_> {
fn gen(&self, p: &mut Codegen, ctx: Context) {
p.add_source_mapping(self.span);
p.print_space_before_identifier();
if self.declare {
p.print_str("declare ");
}
Expand All @@ -692,7 +698,6 @@ impl Gen for VariableDeclaration<'_> {
p.start_of_annotation_comment = Some(self.span.start);
}

p.print_space_before_identifier();
p.print_str(match self.kind {
VariableDeclarationKind::Const => "const",
VariableDeclarationKind::Let => "let",
Expand Down
18 changes: 16 additions & 2 deletions crates/oxc_codegen/tests/integration/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,22 @@ 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("do ; while (true)", "do;\nwhile (true);\n");
test_minify("do ; while (true)", "do;while(true);");
test_minify("do break; while (true)", "do break;while(true);");
test_minify("do continue; while (true)", "do continue;while(true);");
test_minify("do debugger; while (true)", "do debugger;while(true);");
test_minify("do for(x in y); while (true)", "do for(x in y);while(true);");
test_minify("do for(x of y); while (true)", "do for(x of y);while(true);");
test_minify("do for(;;); while (true)", "do for(;;);while(true);");
test_minify("do if (test) {} while (true)", "do if(test){}while(true);");
test_minify("do foo:; while (true)", "do foo:;while(true);");
test_minify("do return; while (true)", "do return;while(true);");
test_minify("do switch(test){} while (true)", "do switch(test){}while(true);");
test_minify("do throw x; while (true)", "do throw x;while(true);");
test_minify("do with(x); while (true)", "do with(x);while(true);");
test_minify("do try{} catch{} while (true)", "do try{}catch{}while(true);");
test_minify("do do ; while(true) while (true)", "do do;while(true);while(true);");
}

#[test]
Expand Down

0 comments on commit 1c84b9c

Please sign in to comment.