From 4d5565407ba842be694a339f1b77cba3d9f0e1f4 Mon Sep 17 00:00:00 2001 From: Dunqing <29533304+Dunqing@users.noreply.github.com> Date: Mon, 13 Oct 2025 06:16:05 +0000 Subject: [PATCH] fix(formatter): correct printing comments for the end of union type (#14498) --- crates/oxc_formatter/src/write/union_type.rs | 10 +++++++--- .../tests/fixtures/ts/comments/union.ts | 3 +++ .../tests/fixtures/ts/comments/union.ts.snap | 14 ++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 crates/oxc_formatter/tests/fixtures/ts/comments/union.ts create mode 100644 crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap diff --git a/crates/oxc_formatter/src/write/union_type.rs b/crates/oxc_formatter/src/write/union_type.rs index e416a9e43ed2e..f93847cb3c64c 100644 --- a/crates/oxc_formatter/src/write/union_type.rs +++ b/crates/oxc_formatter/src/write/union_type.rs @@ -130,7 +130,7 @@ pub struct FormatTSType<'a, 'b> { } impl<'a> Format<'a> for FormatTSType<'a, '_> { - fn fmt(&self, f: &mut crate::formatter::Formatter<'_, 'a>) -> FormatResult<()> { + fn fmt(&self, f: &mut Formatter<'_, 'a>) -> FormatResult<()> { let format_element = format_once(|f| { self.element.fmt(f)?; Ok(()) @@ -158,7 +158,7 @@ impl<'a> Format<'a> for FormatTSType<'a, '_> { // // comment // | B // ``` - // If there is a leading own line comment between `|` and the next node, we need to put print comments + // If there is a leading own line comment between `|` and the next node, we need to put printing comments // before `|` instead of after it. if f.comments().has_leading_own_line_comment(next_node_span.start) { let comments = f.context().comments().comments_before(next_node_span.start); @@ -171,7 +171,9 @@ impl<'a> Format<'a> for FormatTSType<'a, '_> { write!(f, [soft_line_break_or_space()])?; } write!(f, ["|"]) - } else { + } else if let AstNodes::TSUnionType(parent) = self.element.parent + && parent.needs_parentheses(f) + { // ```ts // type Foo = ( // | "thing1" // comment1 @@ -183,6 +185,8 @@ impl<'a> Format<'a> for FormatTSType<'a, '_> { let comments = f.context().comments().end_of_line_comments_after(self.element.span().end); FormatTrailingComments::Comments(comments).fmt(f) + } else { + Ok(()) } } } diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts new file mode 100644 index 0000000000000..9242bdb6daa68 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts @@ -0,0 +1,3 @@ +interface _KeywordDef { + type?: JSONType | JSONType[] // data types that keyword applies to +} diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap new file mode 100644 index 0000000000000..d6416c08f0817 --- /dev/null +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap @@ -0,0 +1,14 @@ +--- +source: crates/oxc_formatter/tests/fixtures/mod.rs +--- +==================== Input ==================== +interface _KeywordDef { + type?: JSONType | JSONType[] // data types that keyword applies to +} + +==================== Output ==================== +interface _KeywordDef { + type?: JSONType | JSONType[]; // data types that keyword applies to +} + +===================== End =====================