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

Commit

Permalink
Make sure the "async" identifier can be used without being interprete…
Browse files Browse the repository at this point in the history
…d as async arrow function
  • Loading branch information
Maxim committed Jul 6, 2022
1 parent f58a1e8 commit 1d109fb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,11 @@ let rec goToClosing closingToken state =
(* Madness *)
let isEs6ArrowExpression ~inTernary p =
Parser.lookahead p (fun state ->
let () =
match state.Parser.token with
| Lident "async" -> Parser.next state
| _ -> ()
in
match state.Parser.token with
| Lident _ | Underscore -> (
Parser.next state;
Expand Down Expand Up @@ -2014,7 +2019,10 @@ and parseOperandExpr ~context p =
let expr = parseUnaryExpr p in
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Exp.assert_ ~loc expr
| Lident "async" -> parseAsyncExpression p
| Lident "async"
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
->
parseAsyncArrowExpression p
| Lident "await" -> parseAwaitExpression p
| Lazy ->
Parser.next p;
Expand Down Expand Up @@ -2736,8 +2744,8 @@ and parseBracedOrRecordExpr p =
let expr = parseRecordExpr ~startPos [] p in
Parser.expect Rbrace p;
expr
| Lident "async" ->
let expr = parseAsyncExpression p in
| Lident "async" when isEs6ArrowExpression ~inTernary:false p ->
let expr = parseAsyncArrowExpression p in
let expr = parseExprBlock ~first:expr p in
Parser.expect Rbrace p;
let loc = mkLoc startPos p.prevEndPos in
Expand Down Expand Up @@ -3105,7 +3113,7 @@ and parseExprBlock ?first p =
Parser.eatBreadcrumb p;
overParseConstrainedOrCoercedOrArrowExpression p blockExpr

and parseAsyncExpression p =
and parseAsyncArrowExpression p =
let startPos = p.Parser.startPos in
match p.token with
| Lident "async" ->
Expand Down
12 changes: 12 additions & 0 deletions tests/parsing/grammar/expressions/async.res
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ let fetch = {
let fetch2 = {
async (. url) => browserFetch(. url)
async (. url) => browserFetch2(. url)
}

// don't parse async es6 arrow
let async = {
let f = async()
()->async
async()
async.async

{async: async[async]}

result->async->mapAsync(a => doStuff(a))
}
8 changes: 8 additions & 0 deletions tests/parsing/grammar/expressions/expected/async.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ let fetch2 =
[@async ][@bs ]);
(((fun url -> ((browserFetch2 url)[@bs ])))
[@async ][@bs ]))
[@ns.braces ])
let async =
((let f = async () in
() |. async;
async ();
async.async;
{ async = (async.(async)) };
(result |. async) |. (mapAsync (fun a -> doStuff a)))
[@ns.braces ])

0 comments on commit 1d109fb

Please sign in to comment.