[flow] Fix parsing of arrows in conditional exprs in parens (#13655)

This commit is contained in:
Nicolò Ribaudo
2021-08-10 20:46:38 +02:00
committed by GitHub
parent 9d0aa1ec9d
commit e721f61110
22 changed files with 739 additions and 24 deletions

View File

@@ -1911,24 +1911,21 @@ export default (superClass: Class<Parser>): Class<Parser> =>
): N.Expression {
if (!this.match(tt.question)) return expr;
// only use the expensive "tryParse" method if there is a question mark
// and if we come from inside parens
if (this.state.maybeInArrowParameters) {
const result = this.tryParse(() =>
super.parseConditional(expr, startPos, startLoc),
);
if (!result.node) {
if (result.error) {
/*:: invariant(refExpressionErrors != null) */
super.setOptionalParametersError(refExpressionErrors, result.error);
}
const nextCh = this.lookaheadCharCode();
// These tokens cannot start an expression, so if one of them follows
// ? then we are probably in an arrow function parameters list and we
// don't parse the conditional expression.
if (
nextCh === charCodes.comma || // (a?, b) => c
nextCh === charCodes.equalsTo || // (a? = b) => c
nextCh === charCodes.colon || // (a?: b) => c
nextCh === charCodes.rightParenthesis // (a?) => c
) {
/*:: invariant(refExpressionErrors != null) */
this.setOptionalParametersError(refExpressionErrors);
return expr;
}
if (result.error) this.state = result.failState;
return result.node;
}
this.expect(tt.question);