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.
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.
|
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.
I wonder why this is reported twice 🤔
There was a problem hiding this comment.
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
I've fixed with workaround by adding a set called invalidAwaitErrors to state at 6d3b9c5. Is there better way?
|
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.
We are throwing different errors for
async function foo() {
function bar(x = await 2) {}
}
This PR addresses only
awaitproblem, notyield.