Skip to content

Commit

Permalink
Fix erroneous pattern parsing inside ternary expressions (#2188)
Browse files Browse the repository at this point in the history
  • Loading branch information
IwanKaramazow authored and chenglou committed Sep 21, 2018
1 parent e120a83 commit 44238a3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions formatTest/unit_tests/expected_output/syntax.re
Original file line number Diff line number Diff line change
Expand Up @@ -1389,3 +1389,10 @@ foo(~(a :> int));
foo(~a=?Foo.a);

foo(~a=Foo.a);

/* https://github.com/facebook/reason/issues/2155#issuecomment-422077648 */
true ? (Update({...a, b: 1}), None) : x;
true ? {...a, b: 1} : a;
true ? (a, {...a, b: 1}) : a;

true ? ([x, ...xs]) => f(x, xs) : a;
7 changes: 7 additions & 0 deletions formatTest/unit_tests/input/syntax.re
Original file line number Diff line number Diff line change
Expand Up @@ -1215,3 +1215,10 @@ foo(~a :> int);
foo(~Foo.a?);

foo(~Foo.a);

/* https://github.com/facebook/reason/issues/2155#issuecomment-422077648 */
true ? (Update({...a, b: 1}), None) : x;
true ? ({...a, b: 1}) : a;
true ? (a, {...a, b: 1}) : a;

true ? ([x, ...xs]) => f(x, xs) : a;
14 changes: 14 additions & 0 deletions src/reason-parser/reason_lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,20 @@ and skip_sharp_bang = parse
let acc = if is_triggering_token tok' then inject_es6_fun acc else acc in
lex_balanced_step closing lexbuf acc tok'
end
(* `...` with a closing `}` indicates that we're definitely not in an es6_fun
* Image the following:
* true ? (Update({...a, b: 1}), None) : x;
* true ? ({...a, b: 1}) : a;
* true ? (a, {...a, b: 1}) : a;
* The lookahead_esfun is triggered initiating the lex_balanced procedure.
* Since we now "over"-parse spread operators in pattern position (for
* better errors), the ... pattern in ({...a, b: 1}) is now a valid path.
* This means that the above expression `({...a, b: 1}) :` is seen as a pattern.
* I.e. the arguments of an es6 function: (pattern) :type => expr
* We exit here, to indicate that an expression needs to be parsed instead
* of a pattern.
*)
| (DOTDOTDOT, RBRACE) -> acc
| _ -> lex_balanced closing lexbuf acc
and lex_balanced closing lexbuf acc =
Expand Down

0 comments on commit 44238a3

Please sign in to comment.