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
Conversation
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/37207/ |
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:
|
const lookahead = this.lookahead(); | ||
if (lookahead.type.startsExpr && !this.isAmbiguousAwait(lookahead)) { |
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.
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
.
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.
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 |
|
1affea3
to
6e69209
Compare
@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)" |
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.
I wonder why this is reported twice 🤔
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.
This is because the error is raised in duplicate with checkReserved
.
babel/packages/babel-parser/src/parser/expression.js
Lines 2349 to 2355 in 6e69209
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?
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.
Thanks!
I tweaked the PR title a bit to make it fit the max commit title length shown by GitHub |
"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)" |
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.
We are throwing different errors for
async function foo() {
function bar(x = await 2) {}
}
This PR addresses only
await
problem, notyield
.