Skip to content

Commit

Permalink
feat(codegen): improvement printing of legal comments (#7111)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Nov 4, 2024
1 parent 03a3085 commit 40d8acf
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
30 changes: 19 additions & 11 deletions crates/oxc_codegen/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,21 @@ impl<'a> Codegen<'a> {

fn print_comments(&mut self, start: u32, comments: &[Comment], unused_comments: Vec<Comment>) {
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 {
Expand All @@ -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;
}
}
}

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 @@ -16,6 +16,7 @@ fn cases() -> Vec<&'static str> {
*/
bar;
}",
"function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } }",
]
}

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 @@ -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__ */
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 @@ -66,3 +67,11 @@ function() {
*/
bar;
}

########## 6
function bar() { var foo; /*! #__NO_SIDE_EFFECTS__ */ function () { } }
----------
function bar() {
var foo;
/*! #__NO_SIDE_EFFECTS__ */ function() {}
}
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 @@ -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 */

0 comments on commit 40d8acf

Please sign in to comment.