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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Parser can't parse "for await (async of []);" #13235

Closed
1 task done
Zalathar opened this issue Apr 30, 2021 · 2 comments 路 Fixed by #13244
Closed
1 task done

[Bug]: Parser can't parse "for await (async of []);" #13235

Zalathar opened this issue Apr 30, 2021 · 2 comments 路 Fixed by #13244
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser

Comments

@Zalathar
Copy link
Contributor

Zalathar commented Apr 30, 2021

馃捇

  • Would you like to work on a fix?

How are you using Babel?

Programmatic API (babel.transform, babel.parse)

Input code

async function f() {
    for await (async of []);
}

Babel REPL

Configuration file name

No response

Configuration

{}

Current and expected behavior

Expected:

  • The parser parses a for await statement with the identifier async as its left-hand-side.

Actual:

/repl.js: Unexpected token, expected "=>" (2:22)

  1 | async function f() {
> 2 |   for await (async of []);
    |                       ^
  3 | }

Environment

  • Babel 7.14.0
  • Current Babel main (b99c4f0)

Possible solution

After for await ( not followed by var/let/const, the ECMAScript grammar requires a LeftHandExpression followed by of.

Babel instead parses an arbitrary expression, and then checks that the expression can be converted to a left-hand-side. This mostly works, except that when the next two tokens are async of the expression parser sees them as the start of an async arrow function with parameter of (which is not a LeftHandExpression).

The solution I currently have in mind is to explicitly look ahead for async of after the for await (, and parse the left-hand-side as an identifier instead of an expression. That will fix this edge-case, and the existing approach of parsing an expression should remain viable in all other cases for now.

Additional context

#13208
prettier/prettier#10781 (review)

@babel-bot
Copy link
Collaborator

Hey @Zalathar! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@JLHwung
Copy link
Contributor

JLHwung commented Apr 30, 2021

The error 'Unexpected token, expected "=>"' is thrown because Babel is parsing async of as the start of an UnaryAsyncArrowExpression async of => {}:

which is called in

return this.parseAsyncArrowUnaryFunction(id);

We can guard it with an extra => lookahead so the async here will be eventually returned as an Identifier. We can either check if lookahead().type is tt.arrow, like V8 does, or we simply check if lookaheadCharCode() is =, I guess the latter approach is sufficient. It is also more performant than the former approach because it does not clone the tokenizer state, since async var => {} is more popular than (async of []), so the lookahead will be executed with high probability.

Note that unlike for await, for (async of []) is forbidden . Although Babel current throws, it is thrown because Babel is parsing async of [] as arrow function like I mentioned before. When we add the extra lookahead, we should specifically throw for for (async of []).

@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Aug 1, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 1, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
i: bug outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants