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

refactor: raise AwaitNotInAsyncContext when an AwaitExpression will be parsed #12716

Merged

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Jan 29, 2021

Q                       A
Fixed Issues? See below
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

When errorRecovery is enabled, Babel throws confusing AwaitNotInAsyncContext error 'await' is only allowed within async functions for function await () {}, (astexplorer) However await 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 an AwaitExpression. Therefore we can simply throw unexpected reserved word in checkReservedWord when we see await in [~Await] productions and script type is module, and then overwrite the thrown errors with AwaitNotInAsyncContext after we have more contextual information. In this case the await turns out to be a part of AwaitExpression 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.

@JLHwung JLHwung added PR: Polish 💅 A type of pull request used for our changelog categories pkg: parser area: errors labels Jan 29, 2021
@babel-bot
Copy link
Collaborator

babel-bot commented Jan 29, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/38921/

@codesandbox-ci
Copy link

codesandbox-ci bot commented Jan 29, 2021

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:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a 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.

@JLHwung
Copy link
Contributor Author

JLHwung commented Jan 30, 2021

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 errorRecovery: true but only throw the first one in errors if errorRecovery is false.

@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 May 4, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: errors outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: parser PR: Polish 💅 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants