Be stricter while parsing arrow function heads in conditional expressions

This commit is contained in:
Andrew Branch 2019-04-02 10:58:11 -07:00
parent f383c3c42d
commit d198c5906f
No known key found for this signature in database
GPG Key ID: 22CCA4B120C427D2
2 changed files with 5 additions and 2 deletions

View File

@ -3695,7 +3695,7 @@ namespace ts {
// - "a ? (b): c" will have "(b):" parsed as a signature with a return type annotation.
//
// So we need just a bit of lookahead to ensure that it can only be a signature.
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && token() !== SyntaxKind.OpenBraceToken) {
if (!allowAmbiguity && token() !== SyntaxKind.EqualsGreaterThanToken && (contextFlags & NodeFlags.InConditionalWhenTrue || token() !== SyntaxKind.OpenBraceToken)) {
// Returning undefined here will cause our caller to rewind to where we started from.
return undefined;
}
@ -3747,7 +3747,9 @@ namespace ts {
const node = <ConditionalExpression>createNode(SyntaxKind.ConditionalExpression, leftOperand.pos);
node.condition = leftOperand;
node.questionToken = questionToken;
node.whenTrue = doOutsideOfContext(disallowInAndDecoratorContext, parseAssignmentExpressionOrHigher);
node.whenTrue = doInsideOfContext(
NodeFlags.InConditionalWhenTrue,
() => doOutsideOfContext(disallowInAndDecoratorContext, parseAssignmentExpressionOrHigher));
node.colonToken = parseExpectedToken(SyntaxKind.ColonToken);
node.whenFalse = nodeIsPresent(node.colonToken)
? parseAssignmentExpressionOrHigher()

View File

@ -553,6 +553,7 @@ namespace ts {
/* @internal */ Ambient = 1 << 22, // If node was inside an ambient context -- a declaration file, or inside something with the `declare` modifier.
/* @internal */ InWithStatement = 1 << 23, // If any ancestor of node was the `statement` of a WithStatement (not the `expression`)
JsonFile = 1 << 24, // If node was parsed in a Json
/* @internal */ InConditionalWhenTrue = 1 << 25, // If node was parsed in the true side of a ConditionalExpression
BlockScoped = Let | Const,