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

Fix Async Generic After Await Parsing Error #11092

Conversation

liamfd
Copy link
Contributor

@liamfd liamfd commented Feb 4, 2020

Q                       A
Fixed Issues? Fixes #11007
Patch: Bug Fix? yes
Major: Breaking Change? no
Minor: New Feature? no
Tests Added + Pass? yes
Documentation PR Link n/a
Any Dependency Changes? no
License MIT

As outlined in #11007, code like the following would fail to compile:

export const foo = async () => {
  await Promise.resolve({});

  const bar = async <T>() => 'foo';
};

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 an Await cannot be used as name inside an async function (2:2) error. I've attempted to address this by resetting awaitPos in tsTryParseGenericAsyncArrowFunction.

Thanks in advance for any feedback, and thank you @JLHwung for your guidance on this issue!

@@ -1528,6 +1528,14 @@ export default (superClass: Class<Parser>): Class<Parser> =>
if (!this.isRelational("<")) {
return undefined;
}

const oldMaybeInArrowParameters = this.state.maybeInArrowParameters;
Copy link
Contributor Author

@liamfd liamfd Feb 4, 2020

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;
Copy link
Contributor Author

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.

@liamfd
Copy link
Contributor Author

liamfd commented Feb 4, 2020

Looks like I've got some failures, I'll try to address them tonight.

Copy link
Contributor

@JLHwung JLHwung left a 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.

@nicolo-ribaudo nicolo-ribaudo added area: typescript pkg: parser PR: Bug Fix 🐛 A type of pull request used for our changelog categories labels Feb 4, 2020
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 merged commit 11d9826 into babel:master Feb 4, 2020
rajasekarm pushed a commit to rajasekarm/babel that referenced this pull request Feb 17, 2020
* test: add test case

* fix: reset awaitPos before parsing generic arrow fn

* fix: maybeInArrowParameters reset to true
@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 6, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area: typescript 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.

Error when Mixing Nested Async Functions and TypeScript Generics
3 participants