Skip to content

Commit d198c59

Browse files
committed
Be stricter while parsing arrow function heads in conditional expressions
1 parent f383c3c commit d198c59

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

src/compiler/parser.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3695,7 +3695,7 @@ namespace ts {
36953695
// - "a ? (b): c" will have "(b):" parsed as a signature with a return type annotation.
36963696
//
36973697
// So we need just a bit of lookahead to ensure that it can only be a signature.
3698-
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && token() !== SyntaxKind.OpenBraceToken) {
3698+
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && (contextFlags & NodeFlags.InConditionalWhenTrue || token() !== SyntaxKind.OpenBraceToken)) {
36993699
// Returning undefined here will cause our caller to rewind to where we started from.
37003700
return undefined;
37013701
}
@@ -3747,7 +3747,9 @@ namespace ts {
37473747
const node = <ConditionalExpression>createNode(SyntaxKind.ConditionalExpression, leftOperand.pos);
37483748
node.condition = leftOperand;
37493749
node.questionToken = questionToken;
3750-
node.whenTrue = doOutsideOfContext(disallowInAndDecoratorContext, parseAssignmentExpressionOrHigher);
3750+
node.whenTrue = doInsideOfContext(
3751+
NodeFlags.InConditionalWhenTrue,
3752+
() => doOutsideOfContext(disallowInAndDecoratorContext, parseAssignmentExpressionOrHigher));
37513753
node.colonToken = parseExpectedToken(SyntaxKind.ColonToken);
37523754
node.whenFalse = nodeIsPresent(node.colonToken)
37533755
? parseAssignmentExpressionOrHigher()

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@ namespace ts {
553553
/* @internal */ Ambient = 1 << 22, // If node was inside an ambient context -- a declaration file, or inside something with the `declare` modifier.
554554
/* @internal */ InWithStatement = 1 << 23, // If any ancestor of node was the `statement` of a WithStatement (not the `expression`)
555555
JsonFile = 1 << 24, // If node was parsed in a Json
556+
/* @internal */ InConditionalWhenTrue = 1 << 25, // If node was parsed in the true side of a ConditionalExpression
556557

557558
BlockScoped = Let | Const,
558559

0 commit comments

Comments
 (0)