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

Conversation

siketyan
Copy link
Member

Summary

Closes #4967

In ECMAScript, the return value of an arrow expression that starts with { needs to be parenthesised to be a valid expression, even if the expression is not a JsObjectExpression. This pull request fixes the condition to parenthesise the expression or not.

Test Plan

Snapshot tests added.

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Feb 21, 2025
Copy link

codspeed-hq bot commented Feb 21, 2025

CodSpeed Performance Report

Merging #5170 will not alter performance

Comparing siketyan:fix/GH-4967 (a5b3136) with main (1c3707d)

Summary

✅ 95 untouched benchmarks

@ematipico
Copy link
Member

We have a trait that checks if the expression needs parentheses: https://github.com/biomejs/biome/blob/main/crates%2Fbiome_js_syntax%2Fsrc%2Fparentheses%2Fmod.rs#L53

The trait should be implemented for the nodes you're working with. If not, it should be implemented

@github-actions github-actions bot added the A-Parser Area: parser label Feb 21, 2025
@siketyan
Copy link
Member Author

siketyan commented Feb 21, 2025

@ematipico I looked up NeedsParentheses impls and there is no impl for the arrow function body. I added impl for AnyJsFunctionBody but I am not sure it is the right place.

Plus, I tried to replace the existing needs_parentheses implementation within the module. However, the NeedsParentheses trait needs to the AST node to be placed under some parent, which is difficult in this situation. Any idea to determine whether a node A needs parens to be placed in node B? We know what is the parent node here from function_expression.parent(), but we need to construct a new tree that includes the converted arrow function to use NeedsParentheses.

@ematipico
Copy link
Member

Looking at the snapshots, I would have expected to have the whole arrow function expression to be parenthesized, not just the body.

For example, from the code of the original issue, I would expect a code fix similar to this:

const obj = {
  message: (arg) => {
    return {
      foo: "message response to foo",
      bar: "message response to bar",
      duck: "message response to duck",
    }[arg]
  }
};

So the NeedsParentheses trait should be applied to JsArrowFunctionExpression node. However, if this isn't the case, then probably we don't need to trait at all.

…h `{`

Signed-off-by: Naoki Ikeguchi <me@s6n.jp>
@github-actions github-actions bot removed the A-Parser Area: parser label Mar 6, 2025
@siketyan
Copy link
Member Author

siketyan commented Mar 6, 2025

@ematipico
No, this isn't that case.
In this case, the code fix was as follows:

const obj = {
  message: (arg) => {
    foo: "message response to foo",
    bar: "message response to bar",
    duck: "message response to duck",
  }[arg],
};

This isn't a valid ECMAScript code. We need parens around the return value expression:

const obj = {
  message: (arg) => ({
                 // ^
    foo: "message response to foo",
    bar: "message response to bar",
    duck: "message response to duck",
  }[arg]),
     // ^
};

I think this has different semantics from the NeedsParentheses trait.

I reverted to the original diff, with rebasing onto the latest main.

@siketyan siketyan requested review from a team March 6, 2025 13:08
@ematipico ematipico merged commit 890d31b into biomejs:main Mar 7, 2025
13 checks passed
@siketyan siketyan deleted the fix/GH-4967 branch March 7, 2025 08:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter L-JavaScript Language: JavaScript and super languages
Projects
None yet
Development

Successfully merging this pull request may close these issues.

💅 useArrowFunction breaks working code on safe lint
2 participants