Skip to content
Draft
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
29 changes: 28 additions & 1 deletion crates/oxc_codegen/tests/integration/comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,9 @@ export type TSTypeLiteral = {
}

pub mod coverage {
use crate::snapshot;
use oxc_codegen::{CodegenOptions, CommentOptions};

use crate::{snapshot, snapshot_options};

#[test]
fn comment() {
Expand Down Expand Up @@ -181,10 +183,35 @@ 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);
}

#[test]
fn minify() {
// Test that comments between catch param and body are stripped in true minify mode (comments disabled)
let cases = vec!["try { x } catch (err) /* v8 ignore next */ { y }"];
let options = CodegenOptions {
minify: true,
comments: CommentOptions::disabled(),
..CodegenOptions::default()
};
snapshot_options("coverage_minify", &cases, &options);
}
}

pub mod legal {
Expand Down
6 changes: 6 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,12 @@ 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 that catch clause with comment between param and body works in minified mode
// Note: Comments are preserved in minify mode when using default options (comments enabled)
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);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
---
########## 0
try { x } catch (err) /* v8 ignore next */ { y }
----------
try{x}catch(err){y}
Loading