Skip to content

Commit ce7de81

Browse files
committed
fix(formatter): inconsistent union type output between two runs
1 parent a50a3ba commit ce7de81

File tree

4 files changed

+81
-10
lines changed

4 files changed

+81
-10
lines changed

crates/oxc_formatter/src/write/union_type.rs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,24 @@ impl<'a> FormatWrite<'a> for AstNode<'a, TSUnionType<'a>> {
128128
});
129129

130130
if has_leading_comments {
131-
let has_own_line_leading_comment = union_type_at_top.types.len() > 1
132-
&& leading_comments.iter().any(|comment| f.comments().is_own_line_comment(comment));
133-
let is_end_of_line_comment = leading_comments
134-
.last()
135-
.is_some_and(|comment| f.comments().is_end_of_line_comment(comment));
131+
let only_type = union_type_at_top.types.len() == 1;
132+
let (has_own_line_comment, has_end_of_line_comment) =
133+
leading_comments.iter().fold((false, false), |(own_line, end_of_line), comment| {
134+
(
135+
end_of_line || own_line || f.comments().is_own_line_comment(comment),
136+
end_of_line || f.comments().is_end_of_line_comment(comment),
137+
)
138+
});
139+
136140
write!(
137141
f,
138142
[group(&indent(&format_args!(
139-
has_own_line_leading_comment.then(soft_line_break),
143+
((has_own_line_comment && !only_type)
144+
|| (has_end_of_line_comment && only_type))
145+
.then(soft_line_break),
140146
FormatLeadingComments::Comments(leading_comments),
141-
(!is_end_of_line_comment).then(soft_line_break),
147+
(!has_end_of_line_comment && has_own_line_comment && only_type)
148+
.then(soft_line_break),
142149
group(&content)
143150
)))]
144151
);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
interface _KeywordDef {
22
type?: JSONType | JSONType[] // data types that keyword applies to
33
}
4+
5+
type C1 = | (
6+
/* 1 */ /*1*/ | (
7+
| (
8+
| A
9+
// A comment to force break
10+
| B
11+
)
12+
)
13+
);
14+
15+
16+
type C2 = | (
17+
/* 1 */ /*1*/
18+
/* 1 */ | (
19+
| (
20+
| A
21+
// A comment to force break
22+
| B
23+
)
24+
)
25+
);

crates/oxc_formatter/tests/fixtures/ts/comments/union.ts.snap

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ interface _KeywordDef {
66
type?: JSONType | JSONType[] // data types that keyword applies to
77
}
88

9+
type C1 = | (
10+
/* 1 */ /*1*/ | (
11+
| (
12+
| A
13+
// A comment to force break
14+
| B
15+
)
16+
)
17+
);
18+
19+
20+
type C2 = | (
21+
/* 1 */ /*1*/
22+
/* 1 */ | (
23+
| (
24+
| A
25+
// A comment to force break
26+
| B
27+
)
28+
)
29+
);
30+
931
==================== Output ====================
1032
------------------
1133
{ printWidth: 80 }
@@ -14,11 +36,33 @@ interface _KeywordDef {
1436
type?: JSONType | JSONType[]; // data types that keyword applies to
1537
}
1638

39+
type C1 = /* 1 */ /*1*/
40+
| A
41+
// A comment to force break
42+
| B;
43+
44+
type C2 =
45+
/* 1 */ /*1*/
46+
/* 1 */ | A
47+
// A comment to force break
48+
| B;
49+
1750
-------------------
1851
{ printWidth: 100 }
1952
-------------------
2053
interface _KeywordDef {
2154
type?: JSONType | JSONType[]; // data types that keyword applies to
2255
}
2356

57+
type C1 = /* 1 */ /*1*/
58+
| A
59+
// A comment to force break
60+
| B;
61+
62+
type C2 =
63+
/* 1 */ /*1*/
64+
/* 1 */ | A
65+
// A comment to force break
66+
| B;
67+
2468
===================== End =====================

tasks/coverage/snapshots/formatter_typescript.snap

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,13 @@ commit: 669c25c0
22

33
formatter_typescript Summary:
44
AST Parsed : 9822/9822 (100.00%)
5-
Positive Passed: 9814/9822 (99.92%)
5+
Positive Passed: 9815/9822 (99.93%)
66
Mismatch: tasks/coverage/typescript/tests/cases/compiler/amdLikeInputDeclarationEmit.ts
77

88
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/genericTypeAssertions3.ts
99
Unexpected token
1010
Expect to Parse: tasks/coverage/typescript/tests/cases/compiler/typeAssertionToGenericFunctionType.ts
1111
Unexpected token
12-
Mismatch: tasks/coverage/typescript/tests/cases/compiler/unionTypeWithLeadingOperator.ts
13-
1412
Mismatch: tasks/coverage/typescript/tests/cases/conformance/expressions/contextualTyping/parenthesizedContexualTyping2.ts
1513

1614
Expect to Parse: tasks/coverage/typescript/tests/cases/conformance/expressions/elementAccess/letIdentifierInElementAccess01.ts

0 commit comments

Comments
 (0)