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

Commit 1d109fb

Browse files
author
Maxim
committed
Make sure the "async" identifier can be used without being interpreted as async arrow function
1 parent f58a1e8 commit 1d109fb

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/res_core.ml

+12-4
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,11 @@ let rec goToClosing closingToken state =
237237
(* Madness *)
238238
let isEs6ArrowExpression ~inTernary p =
239239
Parser.lookahead p (fun state ->
240+
let () =
241+
match state.Parser.token with
242+
| Lident "async" -> Parser.next state
243+
| _ -> ()
244+
in
240245
match state.Parser.token with
241246
| Lident _ | Underscore -> (
242247
Parser.next state;
@@ -2014,7 +2019,10 @@ and parseOperandExpr ~context p =
20142019
let expr = parseUnaryExpr p in
20152020
let loc = mkLoc startPos p.prevEndPos in
20162021
Ast_helper.Exp.assert_ ~loc expr
2017-
| Lident "async" -> parseAsyncExpression p
2022+
| Lident "async"
2023+
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
2024+
->
2025+
parseAsyncArrowExpression p
20182026
| Lident "await" -> parseAwaitExpression p
20192027
| Lazy ->
20202028
Parser.next p;
@@ -2736,8 +2744,8 @@ and parseBracedOrRecordExpr p =
27362744
let expr = parseRecordExpr ~startPos [] p in
27372745
Parser.expect Rbrace p;
27382746
expr
2739-
| Lident "async" ->
2740-
let expr = parseAsyncExpression p in
2747+
| Lident "async" when isEs6ArrowExpression ~inTernary:false p ->
2748+
let expr = parseAsyncArrowExpression p in
27412749
let expr = parseExprBlock ~first:expr p in
27422750
Parser.expect Rbrace p;
27432751
let loc = mkLoc startPos p.prevEndPos in
@@ -3105,7 +3113,7 @@ and parseExprBlock ?first p =
31053113
Parser.eatBreadcrumb p;
31063114
overParseConstrainedOrCoercedOrArrowExpression p blockExpr
31073115

3108-
and parseAsyncExpression p =
3116+
and parseAsyncArrowExpression p =
31093117
let startPos = p.Parser.startPos in
31103118
match p.token with
31113119
| Lident "async" ->

tests/parsing/grammar/expressions/async.res

+12
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,16 @@ let fetch = {
1212
let fetch2 = {
1313
async (. url) => browserFetch(. url)
1414
async (. url) => browserFetch2(. url)
15+
}
16+
17+
// don't parse async es6 arrow
18+
let async = {
19+
let f = async()
20+
()->async
21+
async()
22+
async.async
23+
24+
{async: async[async]}
25+
26+
result->async->mapAsync(a => doStuff(a))
1527
}

tests/parsing/grammar/expressions/expected/async.res.txt

+8
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,12 @@ let fetch2 =
1212
[@async ][@bs ]);
1313
(((fun url -> ((browserFetch2 url)[@bs ])))
1414
[@async ][@bs ]))
15+
[@ns.braces ])
16+
let async =
17+
((let f = async () in
18+
() |. async;
19+
async ();
20+
async.async;
21+
{ async = (async.(async)) };
22+
(result |. async) |. (mapAsync (fun a -> doStuff a)))
1523
[@ns.braces ])

0 commit comments

Comments
 (0)