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

fix(rome_js_formatter): improve handling of arrow parentheses #4704

Merged
merged 2 commits into from
Jul 18, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,12 @@ pub fn can_avoid_parentheses(arrow: &JsArrowFunctionExpression, f: &mut JsFormat
.as_js_parameters()
.and_then(|p| p.items().first()?.ok())
.and_then(|p| JsFormalParameter::cast(p.syntax().clone()))
.is_some_and(|p| p.initializer().is_some())
.is_some_and(|p| {
f.context().comments().has_comments(p.syntax())
|| p.initializer().is_some()
|| p.question_mark_token().is_some()
|| p.type_annotation().is_some()
})
})
}

Expand Down
25 changes: 16 additions & 9 deletions crates/rome_js_formatter/tests/quick_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ mod language {
// use this test check if your snippet prints as you wish, without using a snapshot
fn quick_test() {
let src = r#"
action => {}
(action) => {}
(action: h) => {}
(action?) => {}
(action
// yes
) => {}
({ action }) => {}
([ action ]) => {}
(...action) => {}
Expand All @@ -28,7 +31,7 @@ fn quick_test() {
);
dbg!(tree.syntax());
let options = JsFormatOptions::new(syntax)
.with_semicolons(Semicolons::AsNeeded)
.with_semicolons(Semicolons::Always)
.with_quote_style(QuoteStyle::Double)
.with_jsx_quote_style(QuoteStyle::Single)
.with_arrow_parentheses(ArrowParentheses::AsNeeded);
Expand All @@ -46,12 +49,16 @@ fn quick_test() {
// I don't know why semicolons are added there, but it's not related to my code changes so ¯\_(ツ)_/¯
assert_eq!(
result.as_code(),
r#";action => {}
;action => {}
;({ action }) => {}
;([action]) => {}
;(...action) => {}
;(action = 1) => {}
r#"(action: h) => {};
(action?) => {};
(
action,
// yes
) => {};
({ action }) => {};
([action]) => {};
(...action) => {};
(action = 1) => {};
"#
);
}
13 changes: 13 additions & 0 deletions crates/rome_js_formatter/tests/specs/ts/arrow/arrow_parentheses.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
action => {}
(action) => {}
(action?) => {}
(action: string) => {}
(action): void => {}
(
action
// hhhhhhhh
) => {}
({ action }) => {}
([ action ]) => {}
(...action) => {}
(action = 1) => {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
source: crates/rome_formatter_test/src/snapshot_builder.rs
info: ts/arrow/arrow_parentheses.ts
---

# Input

```ts
action => {}
(action) => {}
(action?) => {}
(action: string) => {}
(action): void => {}
(
action
// hhhhhhhh
) => {}
({ action }) => {}
([ action ]) => {}
(...action) => {}
(action = 1) => {}

```


=============================

# Outputs

## Output 1

-----
Indent style: Tab
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
Arrow parentheses: Always
-----

```ts
(action) => {};
(action) => {};
(action?) => {};
(action: string) => {};
(action): void => {};
(
action,
// hhhhhhhh
) => {};
({ action }) => {};
([action]) => {};
(...action) => {};
(action = 1) => {};
```

## Output 2

-----
Indent style: Tab
Line width: 80
Quote style: Double Quotes
JSX quote style: Double Quotes
Quote properties: As needed
Trailing comma: All
Semicolons: Always
Arrow parentheses: As needed
-----

```ts
action => {};
action => {};
(action?) => {};
(action: string) => {};
(action): void => {};
(
action,
// hhhhhhhh
) => {};
({ action }) => {};
([action]) => {};
(...action) => {};
(action = 1) => {};
```


7 changes: 7 additions & 0 deletions crates/rome_js_formatter/tests/specs/ts/arrow/options.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"cases": [
{
"arrow_parentheses": "AsNeeded"
}
]
}