Skip to content
This repository was archived by the owner on Aug 31, 2023. It is now read-only.

Commit 4c8cf32

Browse files
fix(rome_js_formatter): improve handling of arrow parentheses (#4704)
* fix(rome_js_formatter): improve handling of arrow parentheses * chore: move the new tests to their own file
1 parent adf9d69 commit 4c8cf32

File tree

5 files changed

+130
-10
lines changed

5 files changed

+130
-10
lines changed

crates/rome_js_formatter/src/js/expressions/arrow_function_expression.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,12 @@ pub fn can_avoid_parentheses(arrow: &JsArrowFunctionExpression, f: &mut JsFormat
344344
.as_js_parameters()
345345
.and_then(|p| p.items().first()?.ok())
346346
.and_then(|p| JsFormalParameter::cast(p.syntax().clone()))
347-
.is_some_and(|p| p.initializer().is_some())
347+
.is_some_and(|p| {
348+
f.context().comments().has_comments(p.syntax())
349+
|| p.initializer().is_some()
350+
|| p.question_mark_token().is_some()
351+
|| p.type_annotation().is_some()
352+
})
348353
})
349354
}
350355

crates/rome_js_formatter/tests/quick_test.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ mod language {
1313
// use this test check if your snippet prints as you wish, without using a snapshot
1414
fn quick_test() {
1515
let src = r#"
16-
action => {}
17-
(action) => {}
16+
(action: h) => {}
17+
(action?) => {}
18+
(action
19+
// yes
20+
) => {}
1821
({ action }) => {}
1922
([ action ]) => {}
2023
(...action) => {}
@@ -28,7 +31,7 @@ fn quick_test() {
2831
);
2932
dbg!(tree.syntax());
3033
let options = JsFormatOptions::new(syntax)
31-
.with_semicolons(Semicolons::AsNeeded)
34+
.with_semicolons(Semicolons::Always)
3235
.with_quote_style(QuoteStyle::Double)
3336
.with_jsx_quote_style(QuoteStyle::Single)
3437
.with_arrow_parentheses(ArrowParentheses::AsNeeded);
@@ -46,12 +49,16 @@ fn quick_test() {
4649
// I don't know why semicolons are added there, but it's not related to my code changes so ¯\_(ツ)_/¯
4750
assert_eq!(
4851
result.as_code(),
49-
r#";action => {}
50-
;action => {}
51-
;({ action }) => {}
52-
;([action]) => {}
53-
;(...action) => {}
54-
;(action = 1) => {}
52+
r#"(action: h) => {};
53+
(action?) => {};
54+
(
55+
action,
56+
// yes
57+
) => {};
58+
({ action }) => {};
59+
([action]) => {};
60+
(...action) => {};
61+
(action = 1) => {};
5562
"#
5663
);
5764
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
action => {}
2+
(action) => {}
3+
(action?) => {}
4+
(action: string) => {}
5+
(action): void => {}
6+
(
7+
action
8+
// hhhhhhhh
9+
) => {}
10+
({ action }) => {}
11+
([ action ]) => {}
12+
(...action) => {}
13+
(action = 1) => {}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
---
2+
source: crates/rome_formatter_test/src/snapshot_builder.rs
3+
info: ts/arrow/arrow_parentheses.ts
4+
---
5+
6+
# Input
7+
8+
```ts
9+
action => {}
10+
(action) => {}
11+
(action?) => {}
12+
(action: string) => {}
13+
(action): void => {}
14+
(
15+
action
16+
// hhhhhhhh
17+
) => {}
18+
({ action }) => {}
19+
([ action ]) => {}
20+
(...action) => {}
21+
(action = 1) => {}
22+
23+
```
24+
25+
26+
=============================
27+
28+
# Outputs
29+
30+
## Output 1
31+
32+
-----
33+
Indent style: Tab
34+
Line width: 80
35+
Quote style: Double Quotes
36+
JSX quote style: Double Quotes
37+
Quote properties: As needed
38+
Trailing comma: All
39+
Semicolons: Always
40+
Arrow parentheses: Always
41+
-----
42+
43+
```ts
44+
(action) => {};
45+
(action) => {};
46+
(action?) => {};
47+
(action: string) => {};
48+
(action): void => {};
49+
(
50+
action,
51+
// hhhhhhhh
52+
) => {};
53+
({ action }) => {};
54+
([action]) => {};
55+
(...action) => {};
56+
(action = 1) => {};
57+
```
58+
59+
## Output 2
60+
61+
-----
62+
Indent style: Tab
63+
Line width: 80
64+
Quote style: Double Quotes
65+
JSX quote style: Double Quotes
66+
Quote properties: As needed
67+
Trailing comma: All
68+
Semicolons: Always
69+
Arrow parentheses: As needed
70+
-----
71+
72+
```ts
73+
action => {};
74+
action => {};
75+
(action?) => {};
76+
(action: string) => {};
77+
(action): void => {};
78+
(
79+
action,
80+
// hhhhhhhh
81+
) => {};
82+
({ action }) => {};
83+
([action]) => {};
84+
(...action) => {};
85+
(action = 1) => {};
86+
```
87+
88+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"cases": [
3+
{
4+
"arrow_parentheses": "AsNeeded"
5+
}
6+
]
7+
}

0 commit comments

Comments
 (0)