diff --git a/crates/oxc_codegen/src/comment.rs b/crates/oxc_codegen/src/comment.rs index d022255573dde..2366067149d3b 100644 --- a/crates/oxc_codegen/src/comment.rs +++ b/crates/oxc_codegen/src/comment.rs @@ -164,17 +164,21 @@ impl<'a> Codegen<'a> { fn print_comments(&mut self, start: u32, comments: &[Comment], unused_comments: Vec) { for (i, comment) in comments.iter().enumerate() { - if i == 0 && comment.preceded_by_newline { - // Skip printing newline if this comment is already on a newline. - if let Some(b) = self.last_byte() { - match b { - b'\n' => self.print_indent(), - b'\t' => { /* noop */ } - _ => { - self.print_hard_newline(); - self.print_indent(); + if i == 0 { + if comment.preceded_by_newline { + // Skip printing newline if this comment is already on a newline. + if let Some(b) = self.last_byte() { + match b { + b'\n' => self.print_indent(), + b'\t' => { /* noop */ } + _ => { + self.print_hard_newline(); + self.print_indent(); + } } } + } else { + self.print_indent(); } } if i >= 1 { @@ -186,8 +190,12 @@ impl<'a> Codegen<'a> { } } self.print_comment(comment); - if i == comments.len() - 1 && (comment.is_line() || comment.followed_by_newline) { - self.print_hard_newline(); + if i == comments.len() - 1 { + if comment.is_line() || comment.followed_by_newline { + self.print_hard_newline(); + } else { + self.print_next_indent_as_space = true; + } } } diff --git a/crates/oxc_codegen/tests/integration/legal_comments.rs b/crates/oxc_codegen/tests/integration/legal_comments.rs index d418f7bfac619..d6a3887eb3d18 100644 --- a/crates/oxc_codegen/tests/integration/legal_comments.rs +++ b/crates/oxc_codegen/tests/integration/legal_comments.rs @@ -16,6 +16,7 @@ fn cases() -> Vec<&'static str> { */ bar; }", + "function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } }", ] } 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 1b6874a74aeef..ae79acc40787c 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/legal_eof_comments.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_codegen/tests/integration/main.rs +snapshot_kind: text --- ########## 0 /* @license */ @@ -66,3 +67,12 @@ function() { * @license * Copyright notice 2 */ + +########## 6 +function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } } +---------- +function bar() { + var foo; + function() {} +} +/*! #__NO_SIDE_EFFECTS__ */ 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 fc37ecef67f6a..c4bfbd417f47d 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/legal_inline_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/legal_inline_comments.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_codegen/tests/integration/main.rs +snapshot_kind: text --- ########## 0 /* @license */ @@ -66,3 +67,11 @@ function() { */ bar; } + +########## 6 +function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } } +---------- +function bar() { + var foo; + /*! #__NO_SIDE_EFFECTS__ */ function() {} +} 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 73dc94b8362d3..14d64e1f6fc9a 100644 --- a/crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap +++ b/crates/oxc_codegen/tests/integration/snapshots/legal_linked_comments.snap @@ -1,5 +1,6 @@ --- source: crates/oxc_codegen/tests/integration/main.rs +snapshot_kind: text --- ########## 0 /* @license */ @@ -53,3 +54,11 @@ function() { bar; } /*! For license information please see test.js */ +########## 6 +function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } } +---------- +function bar() { + var foo; + function() {} +} +/*! For license information please see test.js */