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

rules that add await keyword don't add parentheses #21

Open
jefflee-figma opened this issue Mar 1, 2024 · 0 comments
Open

rules that add await keyword don't add parentheses #21

jefflee-figma opened this issue Mar 1, 2024 · 0 comments

Comments

@jefflee-figma
Copy link
Contributor

jefflee-figma commented Mar 1, 2024

Currently, the ban-deprecated-sync-method fixer converts this:

figma.getNodeById(...).id

into this:

await figma.getNodeByIdAsync(...).id

which is incorrect. It should be:

(await figma.getNodeByIdAsync(...)).id

This problem affects all rules that use addAsyncCallFix. The latter has some heuristics for when to add parens, but the heuristics don't seem to be correct.

There are a couple different solutions:

Easy but clumsy

The easy solution is to simply always add an extra set of parentheses. The parentheses may be redundant in some situations, such as this:

const node = (await figma.getNodeByIdAsync(...))

For folks using prettier or some other code formatter, this is a moot point, but it could be annoying to clutter user code with the extra braces.

Cleaner output, trickier implementation

To avoid adding extra braces, we could try to use AST information to detect when the braces are necessary.

An allowlist approach would enumerate all parent expression types where the braces are necessary. For example:

  • member access expressions such as (await asyncCall()).member
  • unary operator expressions such as !(await asyncCall())
  • maybe more?

Using an allowlist introduces the risk that we may miss some expression types where braces should be added. So, we could use a denylist instead, identifying parent expression types where braces aren't necessary. Examples include:

  • assignments such as const foo = await asyncCall()
  • function calls where the await is used as an argument, such as foo(await asyncCall())
  • etc.

Using a denylist has two downsides: it is probably longer than the allowlist, and risks adding redundant parentheses where they aren't necessary. However, it might be the least-bad option.

It might be worth looking at what prettier or other code formatters do...there might be a tried-and-true heuristic that we can borrow.

@jefflee-figma jefflee-figma changed the title await-requires-async fix doesn't add parentheses rules that add await keyword doesn't add parentheses Mar 6, 2024
@jefflee-figma jefflee-figma changed the title rules that add await keyword doesn't add parentheses rules that add await keyword don't add parentheses Mar 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant