Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 05aa40f

Browse files
author
Iwan
committed
Fix parsing of arrow expressions in ternaries.
Arrow functions in the "ternary true branch" where picked up as nested arrows: `x ? y => () : onChange` for the parser was `x ? y => (() : onChange => …)` The fix is to pass the right context through. Fixes #363
1 parent 459d28f commit 05aa40f

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

src/res_core.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1576,7 +1576,7 @@ and parseTernaryExpr leftOperand p =
15761576
| _ ->
15771577
leftOperand
15781578

1579-
and parseEs6ArrowExpression ?parameters p =
1579+
and parseEs6ArrowExpression ?context ?parameters p =
15801580
let startPos = p.Parser.startPos in
15811581
Parser.leaveBreadcrumb p Grammar.Es6ArrowExpr;
15821582
let parameters = match parameters with
@@ -1592,7 +1592,7 @@ and parseEs6ArrowExpression ?parameters p =
15921592
in
15931593
Parser.expect EqualGreater p;
15941594
let body =
1595-
let expr = parseExpr p in
1595+
let expr = parseExpr ?context p in
15961596
match returnType with
15971597
| Some typ ->
15981598
Ast_helper.Exp.constraint_
@@ -2108,7 +2108,7 @@ and parseOperandExpr ~context p =
21082108
if (context != WhenExpr) &&
21092109
isEs6ArrowExpression ~inTernary:(context=TernaryTrueBranchExpr) p
21102110
then
2111-
parseEs6ArrowExpression p
2111+
parseEs6ArrowExpression ~context p
21122112
else
21132113
parseUnaryExpr p
21142114
in

tests/printer/expr/expected/ternary.res.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,3 +259,6 @@ a => a ? aasdasdasdasdasdasdaaasdasdasdasdasdasdasdasdasdasdasdasdasdaaaaaaaaa :
259259

260260
let x = @attrOnTernary (truth ? true : false)
261261
let x = @attrOnCondition truth ? true : false
262+
263+
x ? y => () : onChange
264+
let y = <input value onChange={x ? _ => () : onChange} />

tests/printer/expr/ternary.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,6 @@ a => a ? aasdasdasdasdasdasdaaasdasdasdasdasdasdasdasdasdasdasdasdasdaaaaaaaaa :
121121

122122
let x = @attrOnTernary (truth ? true : false)
123123
let x = @attrOnCondition truth ? true : false
124+
125+
x ? y => () : onChange
126+
let y = <input value onChange={x ? _ => () : onChange} />

0 commit comments

Comments
 (0)