From 46793d7c1c315402ec085b3d3e6e3430fe54f3d1 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Tue, 4 Nov 2025 06:38:40 +0000 Subject: [PATCH] fix(formatter): correct printing comments for `LabeledStatement` (#15260) --- crates/oxc_formatter/src/write/mod.rs | 14 +++++++++++--- .../snapshots/prettier.js.snap.md | 3 +-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/crates/oxc_formatter/src/write/mod.rs b/crates/oxc_formatter/src/write/mod.rs index e0fd943bc8b52..83b02ef364bd3 100644 --- a/crates/oxc_formatter/src/write/mod.rs +++ b/crates/oxc_formatter/src/write/mod.rs @@ -900,15 +900,23 @@ impl<'a> FormatWrite<'a> for AstNode<'a, WithStatement<'a>> { impl<'a> FormatWrite<'a> for AstNode<'a, LabeledStatement<'a>> { fn write(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { - let comments = f.context().comments().comments_before(self.body.span().start); + let comments = f.context().comments().line_comments_before(self.body.span().start); FormatLeadingComments::Comments(comments).fmt(f)?; let label = self.label(); let body = self.body(); write!(f, [label, ":"])?; if matches!(body.as_ref(), Statement::EmptyStatement(_)) { - // If the body is an empty statement, force semicolon insertion - write!(f, ";") + let empty_comments = f.context().comments().comments_before(self.span.end); + write!( + f, + [ + FormatTrailingComments::Comments(empty_comments), + maybe_space(!empty_comments.is_empty()), + // If the body is an empty statement, force semicolon insertion + ";" + ] + ) } else { write!(f, [space(), body]) } diff --git a/tasks/prettier_conformance/snapshots/prettier.js.snap.md b/tasks/prettier_conformance/snapshots/prettier.js.snap.md index fd3b394e4e064..453e389488638 100644 --- a/tasks/prettier_conformance/snapshots/prettier.js.snap.md +++ b/tasks/prettier_conformance/snapshots/prettier.js.snap.md @@ -1,4 +1,4 @@ -js compatibility: 699/749 (93.32%) +js compatibility: 700/749 (93.46%) # Failed @@ -30,7 +30,6 @@ js compatibility: 699/749 (93.32%) | js/if/expr_and_same_line_comments.js | 💥 | 97.73% | | js/if/if_comments.js | 💥 | 76.00% | | js/if/trailing_comment.js | 💥 | 91.43% | -| js/label/empty_label.js | 💥 | 66.67% | | js/last-argument-expansion/dangling-comment-in-arrow-function.js | 💥 | 22.22% | | js/object-multiline/multiline.js | 💥✨ | 22.22% | | js/quote-props/classes.js | 💥💥✨✨ | 47.06% |