Skip to content

[TypeScript] Fix a bug in function type parsing. #3062

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

Merged

Conversation

Thom1729
Copy link
Collaborator

Fix #3059 (comment).

Function types are parsed similarly to arrow functions — the content of the parentheses is parsed as a parenthesized type, and then if an arrow is found after the close paren, the parser backtracks and re-parses as a function type. But if parsing goes far wrong within the parens, then it can't find the arrow. This can happen because function type parameter lists are not necessarily valid parenthesized types. For arrow function literals, we account for this by making sure that the expression grammar “covers” the parameter list grammar. That doesn't work here because we need to be very strict with bailing out of invalid types. Instead, when we see that a parenthesized type has gone wrong, we immediately backtrack and try to reparse as a function type.

This shouldn't affect any previously-valid syntax, but it might result in different behavior for invalid syntax. Probably it should result in better behavior, since it's more likely that a function type will be identified early while typing the argument list.

@deathaxe deathaxe merged commit 45ba3c1 into sublimehq:master Oct 1, 2021
@Thom1729 Thom1729 deleted the typescript-fix-function-type-parsing branch October 1, 2021 17:03
mitranim pushed a commit to mitranim/Packages that referenced this pull request Mar 25, 2022
Fix sublimehq#3059 (comment).

Function types are parsed similarly to arrow functions — the content of the
parentheses is parsed as a parenthesized type, and then if an arrow is found
after the close paren, the parser backtracks and re-parses as a function type.
But if parsing goes far wrong within the parens, then it can't find the arrow.
This can happen because function type parameter lists are not necessarily valid
parenthesized types. For arrow function literals, we account for this by making
sure that the expression grammar “covers” the parameter list grammar. That
doesn't work here because we need to be very strict with bailing out of invalid
types. Instead, when we see that a parenthesized type has gone wrong, we
immediately backtrack and try to reparse as a function type.

This shouldn't affect any previously-valid syntax, but it might result in
different behavior for invalid syntax. Probably it should result in better
behavior, since it's more likely that a function type will be identified early
while typing the argument list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Typescript] Complex arrow function as return type
4 participants