question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

await should be allowed when it is not in AsyncArrowHead

See original GitHub issue

Bug Report

  • I would like to work on a fix!

Current Behavior A clear and concise description of the behavior. The following snippet will throw SyntaxError: Await cannot be used as name inside an async function (1:1)

Input Code

  • REPL or Repo link if applicable: REPL
(await) => {}

Expected behavior/code It should not throw because it is not in async arrow parameters, which means await is a plain identifier and should be allowed in ArrowParameters because it does not have [Await] production parameter. https://tc39.es/ecma262/#sec-arrow-function-definitions

Babel Configuration REPL, no plugins are enabled

Environment

REPL

Possible Solution

Should revisit how awaitPos is tracked in the parser. You can pay attention to https://github.com/babel/babel/blob/a0a9c64a470acec77bec1da0b4bceec4ccf7f446/packages/babel-parser/src/parser/expression.js#L2172-L2175

where awaitPos is updated when it may be in arrow parameters. It was used to track awaitPos on cases like

async ( x = (await) => {}) => {}

however it becomes overkill when it is not in async arrow parameters. Consider add a new parser state maybeInAsyncArrowHead when parsing ambiguous async arrow

https://github.com/babel/babel/blob/master/packages/babel-parser/src/parser/expression.js#L649

It should be similar to maybeAsyncArrow but it should have longer lifecycle – maybeInAsyncArrowHead should be true during the whole async arrow parameters. i.e.

// top level
async ( // maybeInAsyncArrowHead : true
  x = ( // maybeInAsyncArrowHead : true
    await // register the `awaitPos`
  ) => async ( // oldMaybeInAsyncArrowHead should be saved, new maybeInAsyncArrowHead should be true
    y
  ) => {}
); // we does not see an arrow, so the parsed ambiguous async arrow is indeed a CallExpression, reset `awaitPos`.

When we are sure we are not parsing an async arrow, i.e. async(foo), we should reset awaitPos if state.maybeInAsyncArrowHead is false.

Then we can replace maybeInArrowParameters by maybeInAsyncArrowHead it should not record awaitPos for (await) => {}.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
arkucommented, Jan 22, 2020

An update on this issue: I took my time to debug the parse function on the input (await) => {} and was able to get a feel for the parser flow. There’s a lot going on in it but I have a basic idea of the process. I have started working on my PR and will submit a WIP PR in the next few days.

1reaction
rajasekarmcommented, Jan 14, 2020

@arku Go ahead. It’s yours now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why should a void method be async to be able to await?
The async keyword enables the await keyword. So any method using await must be marked async. First of all, I'm not sure what...
Read more >
[Bug]: incorrectly rejects `await` when following a function with ...
The error "'await' is not allowed in async function parameters." and "Yield expression is not allowed in formal parameters." are recorded in a ......
Read more >
await - JavaScript - MDN Web Docs
The await operator is used to wait for a Promise and get its fulfillment value. It can only be used inside an async...
Read more >
Understanding async/await - discord.js Guide
The following information is essential to know before working with async/await. You can only use the await keyword inside a function declared as...
Read more >
How to escape async/await hell - freeCodeCamp
If you forget to use await while calling an async function, the function starts executing. This means that await is not required for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found