From 001058a8e48fece20fc655e46d41e2ccc73dfe52 Mon Sep 17 00:00:00 2001 From: Boshen Date: Sat, 2 Nov 2024 15:55:23 +0800 Subject: [PATCH] feat(codegen): always print legal comments on its own line (#7089) --- 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 1069bb6b5..7edb37b9e 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 bfee37841..feb8b809b 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 a4c429fe6..8a5abbb47 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 b23754471..b248b55a9 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 432167bef..872f10ffd 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 */