Skip to content

Commit

Permalink
fix(lint): useArrowFunction should parenthesise the expr starting wit…
Browse files Browse the repository at this point in the history
…h `{` (#5170)

Signed-off-by: Naoki Ikeguchi <me@s6n.jp>
  • Loading branch information
siketyan authored Mar 7, 2025
1 parent 55efc35 commit 890d31b
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/dull-clocks-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Fixed the safe fix of lint rule `useArrowFunction` breaks the function body starting with `{`.
Original file line number Diff line number Diff line change
Expand Up @@ -355,10 +355,12 @@ fn to_arrow_body(body: JsFunctionBody) -> AnyJsFunctionBody {
// To keep comments, we keep the regular function body
return early_result;
}
if matches!(
return_arg,
AnyJsExpression::JsSequenceExpression(_) | AnyJsExpression::JsObjectExpression(_)
) {
if matches!(return_arg, AnyJsExpression::JsSequenceExpression(_))
|| return_arg
.syntax()
.first_token()
.is_some_and(|token| token.kind() == T!['{'])
{
// () => (first, second)
// () => ({ ... })
return AnyJsFunctionBody::AnyJsExpression(make::parenthesized(return_arg).into());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,12 @@ const withDirective = function () {
"use server";
return 0;
}

// https://github.com/biomejs/biome/issues/4967
const needsParentheses1 = function () {
return { foo: "bar" }["foo"];
}

const needsParentheses2 = function () {
return { foo: "bar" }.foo;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid.ts
snapshot_kind: text
---
# Input
```ts
Expand Down Expand Up @@ -64,6 +63,15 @@ const withDirective = function () {
return 0;
}

// https://github.com/biomejs/biome/issues/4967
const needsParentheses1 = function () {
return { foo: "bar" }["foo"];
}

const needsParentheses2 = function () {
return { foo: "bar" }.foo;
}

```

# Diagnostics
Expand Down Expand Up @@ -583,6 +591,7 @@ invalid.ts:55:23 lint/complexity/useArrowFunction FIXABLE ━━━━━━
> 58 │ }
│ ^
59 │
60 │ // https://github.com/biomejs/biome/issues/4967
i Function expressions that don't use this can be turned into arrow functions.
Expand All @@ -597,3 +606,62 @@ invalid.ts:55:23 lint/complexity/useArrowFunction FIXABLE ━━━━━━
```

```
invalid.ts:61:27 lint/complexity/useArrowFunction FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This function expression can be turned into an arrow function.
60 │ // https://github.com/biomejs/biome/issues/4967
> 61 │ const needsParentheses1 = function () {
│ ^^^^^^^^^^^^^
> 62 │ return { foo: "bar" }["foo"];
> 63 │ }
│ ^
64 │
65 │ const needsParentheses2 = function () {
i Function expressions that don't use this can be turned into arrow functions.
i Safe fix: Use an arrow function instead.
59 59 │
60 60 │ // https://github.com/biomejs/biome/issues/4967
61 │ - const·needsParentheses1·=·function·()·{
62 │ - → return·{·foo:·"bar"·}["foo"];
63 │ - }
61 │ + const·needsParentheses1·=·()·=>·({·foo:·"bar"·}["foo"])
64 62 │
65 63 │ const needsParentheses2 = function () {
```

```
invalid.ts:65:27 lint/complexity/useArrowFunction FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This function expression can be turned into an arrow function.
63 │ }
64 │
> 65 │ const needsParentheses2 = function () {
│ ^^^^^^^^^^^^^
> 66 │ return { foo: "bar" }.foo;
> 67 │ }
│ ^
68 │
i Function expressions that don't use this can be turned into arrow functions.
i Safe fix: Use an arrow function instead.
63 63 │ }
64 64 │
65 │ - const·needsParentheses2·=·function·()·{
66 │ - → return·{·foo:·"bar"·}.foo;
67 │ - }
65 │ + const·needsParentheses2·=·()·=>·({·foo:·"bar"·}.foo)
68 66 │
```

0 comments on commit 890d31b

Please sign in to comment.