Skip to content

Commit 2fb63a4

Browse files
fix(format/js): preserve newline in type alias if right side has a comment (#8910)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 9b1eea8 commit 2fb63a4

File tree

5 files changed

+229
-101
lines changed

5 files changed

+229
-101
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#8774](https://github.com/biomejs/biome/issues/8774): Type aliases with generic parameters that have `extends` constraints now properly indent comments after the equals sign.
6+
7+
Previously, comments after the `=` in type aliases with `extends` constraints were not indented:
8+
9+
```diff
10+
-type A<B, C extends D> = // Some comment
11+
-undefined;
12+
+type A<B, C extends D> =
13+
+ // Some comment
14+
+ undefined;
15+
```

crates/biome_js_formatter/src/utils/assignment_like.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,14 @@ impl AnyJsAssignmentLike {
680680
return Ok(AssignmentLikeLayout::NeverBreakAfterOperator);
681681
}
682682

683+
// For type aliases, check for leading comments before checking if we should break the left-hand side.
684+
// This ensures comments after the `=` sign are properly indented.
685+
if let Self::TsTypeAliasDeclaration(_) = self
686+
&& self.should_break_after_operator(&right, f)?
687+
{
688+
return Ok(AssignmentLikeLayout::BreakAfterOperator);
689+
}
690+
683691
if self.should_break_left_hand_side()? {
684692
return Ok(AssignmentLikeLayout::BreakLeftHandSide);
685693
}

crates/biome_js_formatter/tests/specs/prettier/typescript/type-alias/conditional.ts.snap

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
// Test case 1: Basic type alias with extends and trailing comment
2+
type A<B, C extends D> =
3+
// Some comment
4+
undefined;
5+
6+
// Test case 2: Type alias with extends, comment after = on new line
7+
type A2<B, C extends D> =
8+
// Some comment
9+
undefined;
10+
11+
// Test case 3: Multiple type parameters with extends
12+
type E<F, G extends H, I extends J> =
13+
// Multiple comments
14+
// On multiple lines
15+
string;
16+
17+
// Test case 4: Type alias without extends (should work already)
18+
type K<L, M> =
19+
// Works without extends
20+
// Some comment
21+
undefined;
22+
23+
// Test case 5: Type alias with default value and comment
24+
type N<O extends P = Q> =
25+
// Comment with default value
26+
R;
27+
28+
// Test case 6: Type alias with both extends and default
29+
type S<T extends U = V, W extends X = Y> =
30+
// Comment with both extends and default
31+
Z;
32+
33+
// Test case 7: Complex right-hand side with extends and comment
34+
type AA<BB, CC extends DD> =
35+
// Comment before object type
36+
{
37+
property: string;
38+
};
39+
40+
// Test case 8: Union type with extends and comment
41+
type EE<FF, GG extends HH> =
42+
// Comment before union
43+
| string
44+
| number
45+
| boolean;
46+
47+
// Test case 9: Conditional type with extends and comment
48+
type II<JJ, KK extends LL> =
49+
// Comment before conditional
50+
MM extends NN ? OO : PP;
51+
52+
// Test case 10: Type alias with multiple extends and long comment
53+
type QQ<RR extends SS, TT extends UU, VV extends WW> =
54+
// Very long comment that spans
55+
// multiple lines to test
56+
// proper indentation
57+
XX;
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
---
2+
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
info: ts/type/type_alias/issue-8774.ts
4+
---
5+
# Input
6+
7+
```ts
8+
// Test case 1: Basic type alias with extends and trailing comment
9+
type A<B, C extends D> =
10+
// Some comment
11+
undefined;
12+
13+
// Test case 2: Type alias with extends, comment after = on new line
14+
type A2<B, C extends D> =
15+
// Some comment
16+
undefined;
17+
18+
// Test case 3: Multiple type parameters with extends
19+
type E<F, G extends H, I extends J> =
20+
// Multiple comments
21+
// On multiple lines
22+
string;
23+
24+
// Test case 4: Type alias without extends (should work already)
25+
type K<L, M> =
26+
// Works without extends
27+
// Some comment
28+
undefined;
29+
30+
// Test case 5: Type alias with default value and comment
31+
type N<O extends P = Q> =
32+
// Comment with default value
33+
R;
34+
35+
// Test case 6: Type alias with both extends and default
36+
type S<T extends U = V, W extends X = Y> =
37+
// Comment with both extends and default
38+
Z;
39+
40+
// Test case 7: Complex right-hand side with extends and comment
41+
type AA<BB, CC extends DD> =
42+
// Comment before object type
43+
{
44+
property: string;
45+
};
46+
47+
// Test case 8: Union type with extends and comment
48+
type EE<FF, GG extends HH> =
49+
// Comment before union
50+
| string
51+
| number
52+
| boolean;
53+
54+
// Test case 9: Conditional type with extends and comment
55+
type II<JJ, KK extends LL> =
56+
// Comment before conditional
57+
MM extends NN ? OO : PP;
58+
59+
// Test case 10: Type alias with multiple extends and long comment
60+
type QQ<RR extends SS, TT extends UU, VV extends WW> =
61+
// Very long comment that spans
62+
// multiple lines to test
63+
// proper indentation
64+
XX;
65+
66+
```
67+
68+
69+
=============================
70+
71+
# Outputs
72+
73+
## Output 1
74+
75+
-----
76+
Indent style: Tab
77+
Indent width: 2
78+
Line ending: LF
79+
Line width: 80
80+
Quote style: Double Quotes
81+
JSX quote style: Double Quotes
82+
Quote properties: As needed
83+
Trailing commas: All
84+
Semicolons: Always
85+
Arrow parentheses: Always
86+
Bracket spacing: true
87+
Bracket same line: false
88+
Attribute Position: Auto
89+
Expand lists: Auto
90+
Operator linebreak: After
91+
-----
92+
93+
```ts
94+
// Test case 1: Basic type alias with extends and trailing comment
95+
type A<B, C extends D> =
96+
// Some comment
97+
undefined;
98+
99+
// Test case 2: Type alias with extends, comment after = on new line
100+
type A2<B, C extends D> =
101+
// Some comment
102+
undefined;
103+
104+
// Test case 3: Multiple type parameters with extends
105+
type E<F, G extends H, I extends J> =
106+
// Multiple comments
107+
// On multiple lines
108+
string;
109+
110+
// Test case 4: Type alias without extends (should work already)
111+
type K<L, M> =
112+
// Works without extends
113+
// Some comment
114+
undefined;
115+
116+
// Test case 5: Type alias with default value and comment
117+
type N<O extends P = Q> =
118+
// Comment with default value
119+
R;
120+
121+
// Test case 6: Type alias with both extends and default
122+
type S<T extends U = V, W extends X = Y> =
123+
// Comment with both extends and default
124+
Z;
125+
126+
// Test case 7: Complex right-hand side with extends and comment
127+
type AA<BB, CC extends DD> =
128+
// Comment before object type
129+
{
130+
property: string;
131+
};
132+
133+
// Test case 8: Union type with extends and comment
134+
type EE<FF, GG extends HH> =
135+
// Comment before union
136+
string | number | boolean;
137+
138+
// Test case 9: Conditional type with extends and comment
139+
type II<JJ, KK extends LL> =
140+
// Comment before conditional
141+
MM extends NN ? OO : PP;
142+
143+
// Test case 10: Type alias with multiple extends and long comment
144+
type QQ<RR extends SS, TT extends UU, VV extends WW> =
145+
// Very long comment that spans
146+
// multiple lines to test
147+
// proper indentation
148+
XX;
149+
```

0 commit comments

Comments
 (0)