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
6 changes: 6 additions & 0 deletions crates/oxc_codegen/src/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,12 @@ impl Gen for CatchClause<'_> {
p.print_str(")");
}
p.print_soft_space();
p.print_comments_at(self.body.span.start);
// Consume the space flag set by comment printing to ensure proper spacing before the opening brace
if !p.options.minify && p.print_next_indent_as_space {
p.print_hard_space();
p.print_next_indent_as_space = false;
}
p.print_block_statement(&self.body, ctx);
}
}
Expand Down
13 changes: 13 additions & 0 deletions crates/oxc_codegen/tests/integration/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ catch(e) {
// should never happen
}
",
// Inline comment between catch param and body
"try { console.log('test'); }
catch (err) /* v8 ignore next */ { console.error(err); }",
// Multiple comments between catch param and body
"try { something(); }
catch (err) /* c8 ignore next */ /* istanbul ignore next */ { handle(err); }",
// Line comment between catch param and body.
// NOTE: Line comments after `)` are classified as trailing comments by the parser,
// so they are not preserved. Use block comments instead.
// See: https://github.com/oxc-project/oxc/pull/16167#discussion_r2567604139
"try { something(); }
catch (err) // v8 ignore next
{ handle(err); }",
];

snapshot("coverage", &cases);
Expand Down
4 changes: 4 additions & 0 deletions crates/oxc_codegen/tests/integration/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ fn do_while_stmt() {
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(
"try { x } catch (err) /* v8 ignore next */ { y }",
"try{x}catch(err)/* v8 ignore next */{y}",
);
test_minify("do do ; while(true) while (true)", "do do;while(true);while(true);");
}

Expand Down
32 changes: 32 additions & 0 deletions crates/oxc_codegen/tests/integration/snapshots/coverage.snap
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,35 @@ try {
}
/* istanbul ignore next */
catch (e) {}

########## 20
try { console.log('test'); }
catch (err) /* v8 ignore next */ { console.error(err); }
----------
try {
console.log('test');
} catch (err) /* v8 ignore next */ {
console.error(err);
}

########## 21
try { something(); }
catch (err) /* c8 ignore next */ /* istanbul ignore next */ { handle(err); }
----------
try {
something();
} catch (err) /* c8 ignore next *//* istanbul ignore next */ {
handle(err);
}

########## 22
try { something(); }
catch (err) // v8 ignore next
{ handle(err); }
----------
try {
something();
} catch (err) {
handle(err);
}

Loading