Skip to content

Raise recoverable error for await expressions in sync functions#12520

Merged
nicolo-ribaudo merged 7 commits into
babel:mainfrom
sosukesuzuki:fix-12436
Jan 24, 2021
Merged

Raise recoverable error for await expressions in sync functions#12520
nicolo-ribaudo merged 7 commits into
babel:mainfrom
sosukesuzuki:fix-12436

Conversation

@sosukesuzuki

@sosukesuzuki sosukesuzuki commented Dec 17, 2020

Copy link
Copy Markdown
Contributor
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

babel-bot commented Dec 17, 2020

Copy link
Copy Markdown
Collaborator

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

@codesandbox-ci

codesandbox-ci Bot commented Dec 17, 2020

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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

Comment thread packages/babel-parser/src/parser/expression.js Outdated
Comment thread packages/babel-parser/src/parser/expression.js Outdated
@sosukesuzuki

Copy link
Copy Markdown
Contributor 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
Copy Markdown
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
Copy Markdown
Contributor 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?

@nicolo-ribaudo nicolo-ribaudo left a comment

Copy link
Copy Markdown
Member

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
Copy Markdown
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
Copy Markdown
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