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

[babel 8] Recover from error when parsing <T>() => {}</T> #14616

Merged

Conversation

nicolo-ribaudo
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented May 29, 2022

Q                       A
Fixed Issues? Closes #14612
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

cc @liuxingbaoyu

// If the state was cloned in the JSX parsing branch above but there
// have been any error in the tryParse call, this.state is set to state
// so we still need to clone it.
if (!state || state === this.state) state = this.state.clone();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change is because the below tryParse call should always receive a clone of this.state, and not this.state itself.

if (!parameter.constraint) {
// A single type parameter must either have constraints
// or a trailing comma, otherwise it's ambiguous with JSX.
this.raise(TSErrors.SingleTypeParameterWithoutTrailingComma, {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved this error within the this.tryParse call, so that it know that there is a problem with the function. Otherwise, when parsing <T>() => {}</T> it would give the precedence to the arrow function because it thought that <T>() => {} could be pared without errors.

@babel-bot
Copy link
Collaborator

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

Copy link
Member

@liuxingbaoyu liuxingbaoyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing work!

@nicolo-ribaudo
Copy link
Member Author

It looks like the prettier test is not failing anymore on main (I don't know why), but this is still a good improvement.

@nicolo-ribaudo nicolo-ribaudo merged commit 52cdab7 into babel:main May 31, 2022
@nicolo-ribaudo nicolo-ribaudo deleted the babel-8-recovery-arrow-like-jsx branch May 31, 2022 05:28
@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 Aug 31, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 31, 2022
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants