diff --git a/crates/oxc_codegen/src/gen.rs b/crates/oxc_codegen/src/gen.rs index 7662a053639eff..4bf94abd21b664 100644 --- a/crates/oxc_codegen/src/gen.rs +++ b/crates/oxc_codegen/src/gen.rs @@ -48,6 +48,8 @@ impl<'a> Gen for Program<'a> { stmt.print(p, ctx); p.print_semicolon_if_needed(); } + // Print trailing statement comments. + p.print_statement_comments(self.span.end); } } @@ -662,7 +664,16 @@ impl<'a> Gen for Function<'a> { impl<'a> Gen for FunctionBody<'a> { fn gen(&self, p: &mut Codegen, ctx: Context) { - p.print_curly_braces(self.span, self.is_empty(), |p| { + let is_empty = if self.is_empty() { + if self.span.end == 0 { + true + } else { + !p.comments.contains_key(&(self.span.end - 1)) + } + } else { + false + }; + p.print_curly_braces(self.span, is_empty, |p| { for directive in &self.directives { directive.print(p, ctx); } @@ -670,6 +681,10 @@ impl<'a> Gen for FunctionBody<'a> { p.print_semicolon_if_needed(); stmt.print(p, ctx); } + // Print trailing statement comments. + if self.span.end != 0 { + p.print_statement_comments(self.span.end - 1); + } }); p.needs_semicolon = false; } diff --git a/crates/oxc_codegen/tests/integration/legal_comments.rs b/crates/oxc_codegen/tests/integration/legal_comments.rs index 7ac57d2b4b5b05..44be0ca06467e2 100644 --- a/crates/oxc_codegen/tests/integration/legal_comments.rs +++ b/crates/oxc_codegen/tests/integration/legal_comments.rs @@ -17,6 +17,23 @@ fn cases() -> Vec<&'static str> { bar; }", "function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } }", + "function foo() { + (() => { + /** + * @preserve + */ + })(); + /** + * @preserve + */ +} +/** + * @preserve + */", + "/** +* @preserve +*/ +", ] } diff --git a/crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap index ae79acc40787c3..11c641b4e303cc 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap @@ -76,3 +76,41 @@ function bar() { function() {} } /*! #__NO_SIDE_EFFECTS__ */ + +########## 7 +function foo() { + (() => { + /** + * @preserve + */ + })(); + /** + * @preserve + */ +} +/** + * @preserve + */ +---------- +function foo() { + (() => { + /** + * @preserve + */ + })(); + /** + * @preserve + */ +} +/** +* @preserve +*/ +########## 8 +/** +* @preserve +*/ + +---------- +/** +* @preserve +*/ diff --git a/crates/oxc_codegen/tests/integration/snapshots/legal_inline_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/legal_inline_comments.snap index c4bfbd417f47d4..ab83cd8fb16d8a 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/legal_inline_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/legal_inline_comments.snap @@ -75,3 +75,41 @@ function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function() {} } + +########## 7 +function foo() { + (() => { + /** + * @preserve + */ + })(); + /** + * @preserve + */ +} +/** + * @preserve + */ +---------- +function foo() { + (() => { + /** + * @preserve + */ + })(); + /** + * @preserve + */ +} +/** +* @preserve +*/ +########## 8 +/** +* @preserve +*/ + +---------- +/** +* @preserve +*/ diff --git a/crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap b/crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap index 14d64e1f6fc9af..22a1008aba54c4 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap @@ -62,3 +62,41 @@ function bar() { function() {} } /*! For license information please see test.js */ +########## 7 +function foo() { + (() => { + /** + * @preserve + */ + })(); + /** + * @preserve + */ +} +/** + * @preserve + */ +---------- +function foo() { + (() => { + /** + * @preserve + */ + })(); + /** + * @preserve + */ +} +/** +* @preserve +*//*! For license information please see test.js */ +########## 8 +/** +* @preserve +*/ + +---------- +/** +* @preserve +*/ +/*! For license information please see test.js */