Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(lint): useArrowFunction should parenthesise the expr starting with { #5170

Merged
merged 1 commit into from
Mar 7, 2025
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
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 │


```