diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 1b0899c5204bd..a1429e614d1b9 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -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); } } diff --git a/crates/oxc_codegen/tests/integration/comments.rs b/crates/oxc_codegen/tests/integration/comments.rs index 3c2db2c107e09..886315a9ff59c 100644 --- a/crates/oxc_codegen/tests/integration/comments.rs +++ b/crates/oxc_codegen/tests/integration/comments.rs @@ -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() { @@ -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 { diff --git a/crates/oxc_codegen/tests/integration/js.rs b/crates/oxc_codegen/tests/integration/js.rs index 27527eae18a94..b4bcc86df17fd 100644 --- a/crates/oxc_codegen/tests/integration/js.rs +++ b/crates/oxc_codegen/tests/integration/js.rs @@ -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);"); } diff --git a/crates/oxc_codegen/tests/integration/snapshots/coverage.snap b/crates/oxc_codegen/tests/integration/snapshots/coverage.snap index d5ef186b59c41..41b8d6ffdb0c8 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/coverage.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/coverage.snap @@ -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); +} + diff --git a/crates/oxc_codegen/tests/integration/snapshots/coverage_minify.snap b/crates/oxc_codegen/tests/integration/snapshots/coverage_minify.snap new file mode 100644 index 0000000000000..a53c68e303820 --- /dev/null +++ b/crates/oxc_codegen/tests/integration/snapshots/coverage_minify.snap @@ -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}