fix(parser): correctly disambiguate typescript arrow return type in t…#17879
Closed
NssGourav wants to merge 1 commit intobabel:mainfrom
Closed
fix(parser): correctly disambiguate typescript arrow return type in t…#17879NssGourav wants to merge 1 commit intobabel:mainfrom
NssGourav wants to merge 1 commit intobabel:mainfrom
Conversation
…ernary consequent Similar to how async arrows were handled, this ensures that a colon encountered after a parenthesized expression in the consequent of a ternary is not misinterpreted as an arrow function return type annotation, as colons in ternary consequents are reserved for the ternary separator. We store the outer inConditionalConsequent flag before it gets reset by parseParenAndDistinguishExpression so that shouldParseArrow (which runs after the reset) can correctly identify when to reject the arrow interpretation. We reset this stored flag when entering an arrow function body to allow typed arrows inside arrow bodies within a ternary consequent. Fixes babel#17140.
Collaborator
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61197 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 TypeScript Arrow Function Disambiguation in Ternary Expressions
Summary
This pull request fixes a long-standing issue in the TypeScript parser where the ternary separator (
:) was incorrectly interpreted as an arrow function return type annotation when the ternary consequent was a parenthesized expression.Issue: #17140
The Problem
In valid TypeScript, the following code should parse correctly:
However, the Babel parser's TypeScript plugin would misinterpret
(b) : c => 0as a single arrow function expression, leading to a "Unexpected token, expected ':'" error at the end of the ternary because it consumed the ternary separator as part of the arrow.This was previously documented as a "known limitation" in Babel's test fixtures (specifically in arrow-ambiguity).
The Solution
The fix involves two key changes to the TypeScript parser plugin:
shouldParseArrow: Mirroring the logic for async arrows,shouldParseArrownow checks if the parser is currently inside a ternary consequent. If so, a colon encountered after a parenthesized expression is treated as a ternary separator rather than a return type annotation.parseParenAndDistinguishExpressionresets theinConditionalConsequentflag (which is necessary for async arrows inside nested expressions), we now track the state more precisely. We save the outer conditional state before resetting and check it specifically inshouldParseArrow. We also reset this tracking when entering an arrow function body, ensuring that typed arrows inside arrow bodies continue to be correctly parsed.Changes
packages/babel-parser/src/plugins/typescript/index.tsto implement the disambiguation logic inshouldParseArrow,parseParenAndDistinguishExpression, andparseArrowExpression.packages/babel-parser/test/fixtures/typescript/conditional/arrow-in-conditional-consequent/.Verification
@babel/parserpass with zero regressions.Fixes #17140.