diff --git a/crates/oxc_formatter/src/write/union_type.rs b/crates/oxc_formatter/src/write/union_type.rs index ed2523d722909..6f1aaf093adc6 100644 --- a/crates/oxc_formatter/src/write/union_type.rs +++ b/crates/oxc_formatter/src/write/union_type.rs @@ -128,17 +128,24 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> { }); if has_leading_comments { - let has_own_line_leading_comment = union_type_at_top.types.len() > 1 - && leading_comments.iter().any(|comment| f.comments().is_own_line_comment(comment)); - let is_end_of_line_comment = leading_comments - .last() - .is_some_and(|comment| f.comments().is_end_of_line_comment(comment)); + let only_type = union_type_at_top.types.len() == 1; + let (has_own_line_comment, has_end_of_line_comment) = + leading_comments.iter().fold((false, false), |(own_line, end_of_line), comment| { + ( + own_line || f.comments().is_own_line_comment(comment), + end_of_line || f.comments().is_end_of_line_comment(comment), + ) + }); + write!( f, [group(&indent(&format_args!( - has_own_line_leading_comment.then(soft_line_break), + ((has_own_line_comment && !only_type) + || (has_end_of_line_comment && only_type)) + .then(soft_line_break), FormatLeadingComments::Comments(leading_comments), - (!is_end_of_line_comment).then(soft_line_break), + (!has_end_of_line_comment && has_own_line_comment && only_type) + .then(soft_line_break), group(&content) )))] ); diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts index 9242bdb6daa68..881b6ee1186a2 100644 --- a/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts @@ -1,3 +1,25 @@ interface _KeywordDef { type?: JSONType | JSONType[] // data types that keyword applies to } + +type C1 = | ( + /* 1 */ /*1*/ | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); + + +type C2 = | ( + /* 1 */ /*1*/ + /* 1 */ | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); diff --git a/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap index 0c2edfac1abae..68639af4a4df4 100644 --- a/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap +++ b/crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap @@ -6,6 +6,28 @@ interface _KeywordDef { type?: JSONType | JSONType[] // data types that keyword applies to } +type C1 = | ( + /* 1 */ /*1*/ | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); + + +type C2 = | ( + /* 1 */ /*1*/ + /* 1 */ | ( + | ( + | A + // A comment to force break + | B + ) + ) + ); + ==================== Output ==================== ------------------ { printWidth: 80 } @@ -14,6 +36,17 @@ interface _KeywordDef { type?: JSONType | JSONType[]; // data types that keyword applies to } +type C1 = /* 1 */ /*1*/ + | A + // A comment to force break + | B; + +type C2 = + /* 1 */ /*1*/ + /* 1 */ | A + // A comment to force break + | B; + ------------------- { printWidth: 100 } ------------------- @@ -21,4 +54,15 @@ interface _KeywordDef { type?: JSONType | JSONType[]; // data types that keyword applies to } +type C1 = /* 1 */ /*1*/ + | A + // A comment to force break + | B; + +type C2 = + /* 1 */ /*1*/ + /* 1 */ | A + // A comment to force break + | B; + ===================== End ===================== diff --git a/tasks/coverage/snapshots/formatter_typescript.snap b/tasks/coverage/snapshots/formatter_typescript.snap index 416fb002c6ca8..7540025f47880 100644 --- a/tasks/coverage/snapshots/formatter_typescript.snap +++ b/tasks/coverage/snapshots/formatter_typescript.snap @@ -2,15 +2,13 @@ commit: 669c25c0 formatter_typescript Summary: AST Parsed : 9822/9822 (100.00%) -Positive Passed: 9814/9822 (99.92%) +Positive Passed: 9815/9822 (99.93%) Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericTypeAssertions3.ts Unexpected token Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typeAssertionToGenericFunctionType.ts Unexpected token -Mismatch: tasks/coverage/typescript/tests/cases/compiler/unionTypeWithLeadingOperator.ts - Mismatch: tasks/coverage/typescript/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts