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
refactor: raise AwaitNotInAsyncContext when an AwaitExpression will be parsed #12716
refactor: raise AwaitNotInAsyncContext when an AwaitExpression will be parsed #12716
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/38921/ |
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit afbcc32:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without errorRecovery: true
, Babel will still throw the first "wrong" error.
Since the error is similar I wouldn't worry too much about it, but we should add a big warning in raiseOverwrite
's JSDoc description.
Yeah that's the why I mentioned "we can only achieve this on error recovery mode", although we could do a try catch and recasting the errors. Alternatively, we can parse internally with |
When
errorRecovery
isenabled
, Babel throws confusingAwaitNotInAsyncContext
error'await' is only allowed within async functions
forfunction await () {}
, (astexplorer) Howeverawait
here is parsed as a binding identifier, there is no AwaitExpression in the AST.Based on #12520, we can throw
AwaitNotInAsyncContext
only before we are sure that the expression could be interpreted as anAwaitExpression
. Therefore we can simply throw unexpected reserved word incheckReservedWord
when we seeawait
in[~Await]
productions and script type ismodule
, and then overwrite the thrown errors withAwaitNotInAsyncContext
after we have more contextual information. In this case theawait
turns out to be a part ofAwaitExpression
because we know there is a following UpdateExpression.However we can only achieve this on error recovery mode, in which, essentially, all errors are delayed until the AST is completely parsed. We could consider enable
errorRecovery
by default in Babel 8 because of better errors.