Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flow] Fix parsing of arrows in conditional expressions in parentheses #13655

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 12 additions & 15 deletions packages/babel-parser/src/plugins/flow/index.js
Expand Up @@ -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);
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)