Skip to content

Commit 2f8bf80

Browse files
mdm317ematipico
andauthored
fix(ts/formatter): format comment after => in arrow functions (#9527)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
1 parent 4b64145 commit 2f8bf80

4 files changed

Lines changed: 99 additions & 1 deletion

File tree

.changeset/cuddly-dogs-hide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#8959](https://github.com/biomejs/biome/issues/8959): Fixed TypeScript arrow function formatting when a comment appears after `=>`.

crates/biome_js_formatter/src/comments.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use biome_js_syntax::{
1515
JsBlockStatement, JsCallArguments, JsCatchClause, JsEmptyStatement, JsFinallyClause,
1616
JsFormalParameter, JsFunctionBody, JsIdentifierBinding, JsIdentifierExpression, JsIfStatement,
1717
JsLanguage, JsNamedImportSpecifiers, JsParameters, JsSyntaxKind, JsSyntaxNode,
18-
JsVariableDeclarator, JsWhileStatement, TsInterfaceDeclaration, TsMappedType,
18+
JsVariableDeclarator, JsWhileStatement, TsFunctionType, TsInterfaceDeclaration, TsMappedType,
1919
};
2020
use biome_rowan::{AstNode, SyntaxNodeOptionExt, SyntaxTriviaPieceComments, TextLen};
2121
use biome_suppression::{SuppressionKind, parse_suppression_comment};
@@ -180,6 +180,12 @@ fn handle_typecast_comment(comment: DecoratedComment<JsLanguage>) -> CommentPlac
180180
fn handle_after_arrow_fat_arrow_comment(
181181
comment: DecoratedComment<JsLanguage>,
182182
) -> CommentPlacement<JsLanguage> {
183+
if TsFunctionType::can_cast(comment.enclosing_node().kind()) {
184+
if let Some(following_node) = comment.following_node() {
185+
return CommentPlacement::leading(following_node.clone(), comment);
186+
}
187+
return CommentPlacement::Default(comment);
188+
}
183189
if JsArrowFunctionExpression::can_cast(comment.enclosing_node().kind()) {
184190
// input
185191
// ```javascript
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
type First = () =>
2+
// Comment
3+
string
4+
5+
type Second = () => // Comment
6+
string
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
source: crates/biome_formatter_test/src/snapshot_builder.rs
3+
assertion_line: 212
4+
info: ts/arrow/after_arrow_comment.ts
5+
---
6+
7+
# Input
8+
9+
```ts
10+
type First = () =>
11+
// Comment
12+
string
13+
14+
type Second = () => // Comment
15+
string
16+
```
17+
18+
19+
=============================
20+
21+
# Outputs
22+
23+
## Output 1
24+
25+
-----
26+
Indent style: Tab
27+
Indent width: 2
28+
Line ending: LF
29+
Line width: 80
30+
Quote style: Double Quotes
31+
JSX quote style: Double Quotes
32+
Quote properties: As needed
33+
Trailing commas: All
34+
Semicolons: Always
35+
Arrow parentheses: Always
36+
Bracket spacing: true
37+
Bracket same line: false
38+
Attribute Position: Auto
39+
Expand lists: Auto
40+
Operator linebreak: After
41+
Trailing newline: true
42+
-----
43+
44+
```ts
45+
type First = () => // Comment
46+
string;
47+
48+
type Second = () => // Comment
49+
string;
50+
51+
```
52+
53+
## Output 1
54+
55+
-----
56+
Indent style: Tab
57+
Indent width: 2
58+
Line ending: LF
59+
Line width: 80
60+
Quote style: Double Quotes
61+
JSX quote style: Double Quotes
62+
Quote properties: As needed
63+
Trailing commas: All
64+
Semicolons: Always
65+
Arrow parentheses: As needed
66+
Bracket spacing: true
67+
Bracket same line: false
68+
Attribute Position: Auto
69+
Expand lists: Auto
70+
Operator linebreak: After
71+
Trailing newline: true
72+
-----
73+
74+
```ts
75+
type First = () => // Comment
76+
string;
77+
78+
type Second = () => // Comment
79+
string;
80+
81+
```

0 commit comments

Comments
 (0)