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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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 (0d9cda7) with main (d739883)

Summary

✅ 94 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Linter Area: linter A-Parser Area: parser 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