Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 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.print_next_indent_as_space {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot add !p.options.minify and add tests for it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added !p.options.minify guard to the space handling logic and added tests for both minify mode with comments enabled (preserves comments) and true minify mode with comments disabled (strips comments). See 4674fd9.

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
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