Skip to content

Commit

Permalink
[flow] Fix parsing of arrows in conditional expressions in parentheses
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Aug 7, 2021
1 parent bdc75dc commit 836215b
Show file tree
Hide file tree
Showing 22 changed files with 738 additions and 24 deletions.
26 changes: 11 additions & 15 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -1911,24 +1911,20 @@ 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
) {
this.setOptionalParametersError(refExpressionErrors);
return expr;
}

if (result.error) this.state = result.failState;
return result.node;
}

this.expect(tt.question);
Expand Down
@@ -0,0 +1,11 @@
(a ? (b = c) : d => e); // a ? (b = c) : (d => e)
(a ? (b += c) : d => e); // a ? (b += c) : (d => e)

(a ? (b = c) : d => e : f); // a ? ((b = c): d => e) : f
(a ? (b += c) : d => e : f); // ((a ? (b += c) : (d => e)) : f)

(a ? b => (c = d) : e => f); // a ? (b => (c = d)) : (e => f)
(a ? b => (c += d) : e => f); // a ? (b => (c += d)) : (e => f)

(a ? b => (c = d) : e => f : g); // a ? (b => ((c = d): e => f)) : g
(a ? b => (c += d) : e => f : g); // ((a ? (b => (c += d)) : (e => f)) : g)

0 comments on commit 836215b

Please sign in to comment.