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
Fix Async Generic After Await Parsing Error #11092
Fix Async Generic After Await Parsing Error #11092
Conversation
@@ -1528,6 +1528,14 @@ export default (superClass: Class<Parser>): Class<Parser> => | |||
if (!this.isRelational("<")) { | |||
return undefined; | |||
} | |||
|
|||
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; |
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 wasn't 100% sure where to put this change, and the one below to reset things.
For resetting these values, it seemed best to do it before any attempt to parse is started, but strictly speaking it only seemed to need to happen before parseFunctionParams
.
For setting state back to oldState below, it looks like typically this is done before parseArrowExpression
is called (e.g. https://github.com/liamfd/babel/blob/master/packages/babel-parser/src/parser/expression.js#L972), so I matched that.
@@ -1528,6 +1528,14 @@ export default (superClass: Class<Parser>): Class<Parser> => | |||
if (!this.isRelational("<")) { | |||
return undefined; | |||
} | |||
|
|||
const oldMaybeInArrowParameters = this.state.maybeInArrowParameters; | |||
const oldYieldPos = this.state.yieldPos; |
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 added oldMaybeInArrowParameters
and oldYieldPos
at @JLHwung 's suggestion. From looking around, it does seem like these are generally reset together.
I also wasn't entirely sure whether oldMaybeInArrowParameters
should be set here, or whether it should be true
or false
. I opted to match https://github.com/liamfd/babel/blob/master/packages/babel-parser/src/parser/expression.js#L972, true
seemed more appropriate.
Looks like I've got some failures, I'll try to address them tonight. |
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.
The CI error is unrelated.
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!
* test: add test case * fix: reset awaitPos before parsing generic arrow fn * fix: maybeInArrowParameters reset to true
As outlined in #11007, code like the following would fail to compile:
This would only happen with an
await
followed by an async arrow function with a TypeScript generic defined. @JLHwung helped me understand the issue, and suggested the fix I've implemented in this PR.In short, when parsing the snippet above, Babel would throw an
Unexpected token, expected ";"
error, which was caused by anAwait cannot be used as name inside an async function (2:2)
error. I've attempted to address this by resettingawaitPos
intsTryParseGenericAsyncArrowFunction
.Thanks in advance for any feedback, and thank you @JLHwung for your guidance on this issue!