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

Raise recoverable error for await expressions in sync functions #12520

Merged
merged 7 commits into from Jan 24, 2021

Conversation

sosukesuzuki
Copy link
Member

@sosukesuzuki sosukesuzuki commented Dec 17, 2020

Q                       A
Fixed Issues? Fixes #12436, closes #11090
Patch: Bug Fix? Y
Tests Added + Pass? Yes
License MIT

This PR addresses only await problem, not yield.

@sosukesuzuki sosukesuzuki added pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Dec 17, 2020
@babel-bot
Copy link
Collaborator

babel-bot commented Dec 17, 2020

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 17, 2020

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 6d3b9c5:

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

Comment on lines 501 to 502
const lookahead = this.lookahead();
if (lookahead.type.startsExpr && !this.isAmbiguousAwait(lookahead)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably avoid the lookahead by moving this logic to the end of this function: after calling parseUpdate(), it it returns an identifier whose name is await, we can check if it could be an await expression and the call parseAwait().

parseAwait() then needs to be modified to accept the startLoc and startPos parameters (we can get .start and .loc.start from parseUpdate's result), and call startNodeAt instead of startNode in parseAwait.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolo-ribaudo
Copy link
Member

Oh I found #11090 - I completely forgot about it so thanks for opening a new PR!

Something that I notice is missing from this PR is support for TLA: when the plugin is enabled, it we are parsing a script and there is a top-level await expression, we can report an error.

@sosukesuzuki
Copy link
Member Author

Something that I notice is missing from this PR is support for TLA: when the plugin is enabled, it we are parsing a script and there is a top-level await expression, we can report an error.

1affea3

@sosukesuzuki
Copy link
Member Author

@JLHwung Thank you for your reviews. I fixed reviewed points!

"start":0,"end":33,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions (2:6)",
"SyntaxError: 'await' is only allowed within async functions (2:6)"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this is reported twice 🤔

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because the error is raised in duplicate with checkReserved.

if (!this.prodParam.hasAwait && word === "await") {
this.raise(
startLoc,
this.hasPlugin("topLevelAwait")
? Errors.AwaitNotInAsyncContext
: Errors.AwaitNotInAsyncFunction,
);

I've fixed with workaround by adding a set called invalidAwaitErrors to state at 6d3b9c5. Is there better way?

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.

Thanks!

@nicolo-ribaudo nicolo-ribaudo changed the title Parse await expression in no-async function with recoverable error Raise recoverable error for await expressions in sync functions Jan 24, 2021
@nicolo-ribaudo nicolo-ribaudo merged commit 8fcba6e into babel:main Jan 24, 2021
@nicolo-ribaudo
Copy link
Member

I tweaked the PR title a bit to make it fit the max commit title length shown by GitHub

@sosukesuzuki sosukesuzuki deleted the fix-12436 branch January 24, 2021 06:42
"start":0,"end":55,"loc":{"start":{"line":1,"column":0},"end":{"line":3,"column":1}},
"errors": [
"SyntaxError: 'await' is only allowed within async functions (2:19)",
"SyntaxError: await is not allowed in async function parameters (2:19)"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are throwing different errors for

async function foo() {
  function bar(x = await 2) {}
}

@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 1, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 1, 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: Bug Fix 🐛 A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Throw better error message when await is not used in async functions in errorRecovery mode
5 participants