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 parsing of generic type parameters in arrow functions with JSX syntax #59485

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

poteboy
Copy link

@poteboy poteboy commented Jul 31, 2024

Potentially fixes #15713

This PR addresses an issue where the TypeScript parser incorrectly interprets generic type parameters in arrow functions as JSX elements. Specifically, the code const a = <T>(b: T) => b was being parsed as a JSX element instead of a generic type parameter.

Testing:

Added a new test case jsxGenericArrowFunction.tsx to verify the fix. Before the change, the following errors were observed:

jsxGenericArrowFunction.tsx(1,12): error TS2304: Cannot find name 'T'.
jsxGenericArrowFunction.tsx(1,12): error TS17008: JSX element 'T' has no corresponding closing tag.
jsxGenericArrowFunction.tsx(1,22): error TS1382: Unexpected token. Did you mean `{'>'}` or `&gt;`?
jsxGenericArrowFunction.tsx(1,25): error TS1005: '</' expected.

After the change, these errors no longer occur.

This change ensures that generic type parameters in arrow functions are correctly parsed, improving TypeScript's type-checking and syntax parsing capabilities.

Additionally, as noted in the Design Meeting Notes, there has been anticipation for a fix to this issue from the TypeScript community

@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Jul 31, 2024
@poteboy
Copy link
Author

poteboy commented Jul 31, 2024

@microsoft-github-policy-service agree

@poteboy poteboy marked this pull request as draft July 31, 2024 18:53
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 1, 2024

This would technically be a breaking change for if someone wrote something like

const a = <Formatted>() => 1234</Formatted>;

I'm surprised that we don't have a test like that at all though.

@poteboy
Copy link
Author

poteboy commented Aug 2, 2024

@DanielRosenwasser
You're right, I didn't take this case into account until the CI failed. Parsing this case as an arrow function and backtracking when we hit the JSX closing element would solve the issue, but it could potentially have exponential time complexity. If that's acceptable, it could be a viable solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Generic usage reported as JSX Error
3 participants