Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions crates/oxc_formatter/src/write/union_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)))]
);
Expand Down
22 changes: 22 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/comments/union.ts
Original file line number Diff line number Diff line change
@@ -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
)
)
);
44 changes: 44 additions & 0 deletions crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -14,11 +36,33 @@ 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 }
-------------------
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 =====================
4 changes: 1 addition & 3 deletions tasks/coverage/snapshots/formatter_typescript.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading