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

Commit 85846b6

Browse files
Maximcristianoc
Maxim
authored andcommitted
Make sure the "async" identifier can be used without being interpreted as async arrow function
1 parent c2638e2 commit 85846b6

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;
@@ -2031,7 +2036,10 @@ and parseOperandExpr ~context p =
20312036
let expr = parseUnaryExpr p in
20322037
let loc = mkLoc startPos p.prevEndPos in
20332038
Ast_helper.Exp.assert_ ~loc expr
2034-
| Lident "async" -> parseAsyncExpression p
2039+
| Lident "async"
2040+
when isEs6ArrowExpression ~inTernary:(context = TernaryTrueBranchExpr) p
2041+
->
2042+
parseAsyncArrowExpression p
20352043
| Lident "await" -> parseAwaitExpression p
20362044
| Lazy ->
20372045
Parser.next p;
@@ -2746,8 +2754,8 @@ and parseBracedOrRecordExpr p =
27462754
let expr = parseRecordExpr ~startPos [] p in
27472755
Parser.expect Rbrace p;
27482756
expr
2749-
| Lident "async" ->
2750-
let expr = parseAsyncExpression p in
2757+
| Lident "async" when isEs6ArrowExpression ~inTernary:false p ->
2758+
let expr = parseAsyncArrowExpression p in
27512759
let expr = parseExprBlock ~first:expr p in
27522760
Parser.expect Rbrace p;
27532761
let loc = mkLoc startPos p.prevEndPos in
@@ -3115,7 +3123,7 @@ and parseExprBlock ?first p =
31153123
Parser.eatBreadcrumb p;
31163124
overParseConstrainedOrCoercedOrArrowExpression p blockExpr
31173125

3118-
and parseAsyncExpression p =
3126+
and parseAsyncArrowExpression p =
31193127
let startPos = p.Parser.startPos in
31203128
match p.token with
31213129
| 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)