[TypeScript] Fix a bug in function type parsing. #3062
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.