From 921cc22e5b59a202c7f8c703b22c0b91a9e50d9f Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 2 Nov 2024 14:12:51 +0800 Subject: [PATCH] feat(codegen): always print legal comments on its own line --- crates/oxc_codegen/src/comment.rs | 10 +++++++--- crates/oxc_codegen/tests/integration/legal_comments.rs | 1 + .../integration/snapshots/legal_eof_comments.snap | 10 ++++++++++ .../integration/snapshots/legal_inline_comments.snap | 10 ++++++++++ .../integration/snapshots/legal_linked_comments.snap | 8 ++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/oxc_codegen/src/comment.rs b/crates/oxc_codegen/src/comment.rs index d204c8dd88490..2a5b9166a4dcc 100644 --- a/crates/oxc_codegen/src/comment.rs +++ b/crates/oxc_codegen/src/comment.rs @@ -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); diff --git a/crates/oxc_codegen/tests/integration/legal_comments.rs b/crates/oxc_codegen/tests/integration/legal_comments.rs index bfee378412974..feb8b809ba639 100644 --- a/crates/oxc_codegen/tests/integration/legal_comments.rs +++ b/crates/oxc_codegen/tests/integration/legal_comments.rs @@ -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;", ] } 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 a4c429fe61146..8a5abbb4765d5 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 */ @@ -40,3 +41,12 @@ foo; bar; /* @license */ /*! KEEP */ + +########## 4 +/* @license *//*! KEEP */ +foo;bar; +---------- +foo; +bar; +/* @license */ +/*! KEEP */ 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 b237544718aef..b248b55a9b938 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 */ @@ -40,3 +41,12 @@ foo;bar; /*! KEEP */ foo; bar; + +########## 4 +/* @license *//*! KEEP */ +foo;bar; +---------- +/* @license */ +/*! KEEP */ +foo; +bar; 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 432167befeaad..872f10ffdea73 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 */ @@ -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 */