Skip to content

Commit

Permalink
feat(codegen): always print legal comments on its own line
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 2, 2024
1 parent 413973d commit 921cc22
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 3 deletions.
10 changes: 7 additions & 3 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,13 @@ impl<'a> Codegen<'a> {
}

for (i, comment) in comments.iter().enumerate() {
if i >= 1 && comment.preceded_by_newline {
self.print_hard_newline();
self.print_indent();
if i >= 1 {
if comment.preceded_by_newline {
self.print_hard_newline();
self.print_indent();
} else if comment.is_legal(self.source_text) {
self.print_hard_newline();
}
}

self.print_comment(comment);
Expand Down
1 change: 1 addition & 0 deletions crates/oxc_codegen/tests/integration/legal_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ fn cases() -> Vec<&'static str> {
"/* @license */\n/* @preserve */\nfoo;bar;",
"/* @license */\n//! KEEP\nfoo;bar;",
"/* @license */\n/*! KEEP */\nfoo;bar;",
"/* @license *//*! KEEP */\nfoo;bar;",
]
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
snapshot_kind: text
---
########## 0
/* @license */
Expand Down Expand Up @@ -40,3 +41,12 @@ foo;
bar;
/* @license */
/*! KEEP */

########## 4
/* @license *//*! KEEP */
foo;bar;
----------
foo;
bar;
/* @license */
/*! KEEP */
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
snapshot_kind: text
---
########## 0
/* @license */
Expand Down Expand Up @@ -40,3 +41,12 @@ foo;bar;
/*! KEEP */
foo;
bar;

########## 4
/* @license *//*! KEEP */
foo;bar;
----------
/* @license */
/*! KEEP */
foo;
bar;
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/oxc_codegen/tests/integration/main.rs
snapshot_kind: text
---
########## 0
/* @license */
Expand Down Expand Up @@ -33,3 +34,10 @@ foo;bar;
foo;
bar;
/*! For license information please see test.js */
########## 4
/* @license *//*! KEEP */
foo;bar;
----------
foo;
bar;
/*! For license information please see test.js */

0 comments on commit 921cc22

Please sign in to comment.