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

Commit b4d155b

Browse files
author
Maxim
committed
Implement printer support for async arrow expressions
1 parent 283c971 commit b4d155b

File tree

6 files changed

+67
-15
lines changed

6 files changed

+67
-15
lines changed

src/res_parsetree_viewer.ml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ let arrowType ct =
1111
process attrsBefore (arg :: acc) typ2
1212
| {
1313
ptyp_desc = Ptyp_arrow ((Nolabel as lbl), typ1, typ2);
14-
ptyp_attributes = [({txt = "bs"}, _)] as attrs;
14+
ptyp_attributes = [({txt = "bs" | "async"}, _)] as attrs;
1515
} ->
1616
let arg = (attrs, lbl, typ1) in
1717
process attrsBefore (arg :: acc) typ2
@@ -55,6 +55,16 @@ let processUncurriedAttribute attrs =
5555
in
5656
process false [] attrs
5757

58+
let processFunctionAttributes attrs =
59+
let rec process async uncurried acc attrs =
60+
match attrs with
61+
| [] -> (async, uncurried, List.rev acc)
62+
| ({Location.txt = "bs"}, _) :: rest -> process async true acc rest
63+
| ({Location.txt = "async"}, _) :: rest -> process true uncurried acc rest
64+
| attr :: rest -> process async uncurried (attr :: acc) rest
65+
in
66+
process false false [] attrs
67+
5868
let collectListExpressions expr =
5969
let rec collect acc expr =
6070
match expr.pexp_desc with
@@ -316,7 +326,8 @@ let hasAttributes attrs =
316326
match attr with
317327
| ( {
318328
Location.txt =
319-
"bs" | "res.template" | "ns.ternary" | "ns.braces" | "ns.iflet";
329+
( "bs" | "async" | "res.template" | "ns.ternary" | "ns.braces"
330+
| "ns.iflet" );
320331
},
321332
_ ) ->
322333
false

src/res_parsetree_viewer.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ val functorType :
1717
val processUncurriedAttribute :
1818
Parsetree.attributes -> bool * Parsetree.attributes
1919

20+
(* determines whether a function is async and/or uncurried based on the given attributes *)
21+
val processFunctionAttributes :
22+
Parsetree.attributes ->
23+
bool (* async *) * bool (* uncurried *) * Parsetree.attributes
24+
2025
type ifConditionKind =
2126
| If of Parsetree.expression
2227
| IfLet of Parsetree.pattern * Parsetree.expression

src/res_printer.ml

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,8 +3135,8 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
31353135
cmtTbl
31363136
| Pexp_fun _ | Pexp_newtype _ ->
31373137
let attrsOnArrow, parameters, returnExpr = ParsetreeViewer.funExpr e in
3138-
let uncurried, attrs =
3139-
ParsetreeViewer.processUncurriedAttribute attrsOnArrow
3138+
let async, uncurried, attrs =
3139+
ParsetreeViewer.processFunctionAttributes attrsOnArrow
31403140
in
31413141
let returnExpr, typConstraint =
31423142
match returnExpr.pexp_desc with
@@ -3156,7 +3156,7 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
31563156
in
31573157
let parametersDoc =
31583158
printExprFunParameters ~customLayout ~inCallback:NoCallback ~uncurried
3159-
~hasConstraint parameters cmtTbl
3159+
~async ~hasConstraint parameters cmtTbl
31603160
in
31613161
let returnExprDoc =
31623162
let optBraces, _ = ParsetreeViewer.processBracesAttr returnExpr in
@@ -3298,8 +3298,8 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
32983298

32993299
and printPexpFun ~customLayout ~inCallback e cmtTbl =
33003300
let attrsOnArrow, parameters, returnExpr = ParsetreeViewer.funExpr e in
3301-
let uncurried, attrs =
3302-
ParsetreeViewer.processUncurriedAttribute attrsOnArrow
3301+
let async, uncurried, attrs =
3302+
ParsetreeViewer.processFunctionAttributes attrsOnArrow
33033303
in
33043304
let returnExpr, typConstraint =
33053305
match returnExpr.pexp_desc with
@@ -3313,7 +3313,7 @@ and printPexpFun ~customLayout ~inCallback e cmtTbl =
33133313
| _ -> (returnExpr, None)
33143314
in
33153315
let parametersDoc =
3316-
printExprFunParameters ~customLayout ~inCallback ~uncurried
3316+
printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
33173317
~hasConstraint:
33183318
(match typConstraint with
33193319
| Some _ -> true
@@ -4575,8 +4575,8 @@ and printCase ~customLayout (case : Parsetree.case) cmtTbl =
45754575
in
45764576
Doc.group (Doc.concat [Doc.text "| "; content])
45774577

4578-
and printExprFunParameters ~customLayout ~inCallback ~uncurried ~hasConstraint
4579-
parameters cmtTbl =
4578+
and printExprFunParameters ~customLayout ~inCallback ~async ~uncurried
4579+
~hasConstraint parameters cmtTbl =
45804580
match parameters with
45814581
(* let f = _ => () *)
45824582
| [
@@ -4589,7 +4589,8 @@ and printExprFunParameters ~customLayout ~inCallback ~uncurried ~hasConstraint
45894589
};
45904590
]
45914591
when not uncurried ->
4592-
if hasConstraint then Doc.text "(_)" else Doc.text "_"
4592+
let any = if hasConstraint then Doc.text "(_)" else Doc.text "_" in
4593+
if async then Doc.concat [Doc.text "async "; any] else any
45934594
(* let f = a => () *)
45944595
| [
45954596
ParsetreeViewer.Parameter
@@ -4603,7 +4604,8 @@ and printExprFunParameters ~customLayout ~inCallback ~uncurried ~hasConstraint
46034604
when not uncurried ->
46044605
let txtDoc =
46054606
let var = printIdentLike stringLoc.txt in
4606-
if hasConstraint then addParens var else var
4607+
let var = if hasConstraint then addParens var else var in
4608+
if async then Doc.concat [Doc.text "async ("; var; Doc.rparen] else var
46074609
in
46084610
printComments txtDoc cmtTbl stringLoc.loc
46094611
(* let f = () => () *)
@@ -4617,15 +4619,21 @@ and printExprFunParameters ~customLayout ~inCallback ~uncurried ~hasConstraint
46174619
};
46184620
]
46194621
when not uncurried ->
4620-
Doc.text "()"
4622+
if async then Doc.text "async ()" else Doc.text "()"
46214623
(* let f = (~greeting, ~from as hometown, ~x=?) => () *)
46224624
| parameters ->
46234625
let inCallback =
46244626
match inCallback with
46254627
| FitsOnOneLine -> true
46264628
| _ -> false
46274629
in
4628-
let lparen = if uncurried then Doc.text "(. " else Doc.lparen in
4630+
let maybeAsyncLparen =
4631+
match (async, uncurried) with
4632+
| true, true -> Doc.text "async (. "
4633+
| true, false -> Doc.text "async ("
4634+
| false, true -> Doc.text "(. "
4635+
| false, false -> Doc.lparen
4636+
in
46294637
let shouldHug = ParsetreeViewer.parametersShouldHug parameters in
46304638
let printedParamaters =
46314639
Doc.concat
@@ -4641,7 +4649,7 @@ and printExprFunParameters ~customLayout ~inCallback ~uncurried ~hasConstraint
46414649
Doc.group
46424650
(Doc.concat
46434651
[
4644-
lparen;
4652+
maybeAsyncLparen;
46454653
(if shouldHug || inCallback then printedParamaters
46464654
else
46474655
Doc.concat

test.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
let f = (.) => ()
2+
let f = async () => delay(20)

tests/printer/expr/asyncAwait.res

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let sequentialAwait = async () => {
2+
let result1 = await paused("first")
3+
nodeJsAssert.equal(result1, "first")
4+
5+
let result2 = await paused("second")
6+
nodeJsAssert.equal(result2, "second")
7+
}
8+
9+
let f = async () => ()
10+
let f = async (.) => ()
11+
let f = async f => f()
12+
let f = async (a, b) => a + b
13+
let f = async (. a, b) => a + b
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let sequentialAwait = async () => {
2+
let result1 = @await paused("first")
3+
nodeJsAssert.equal(result1, "first")
4+
5+
let result2 = @await paused("second")
6+
nodeJsAssert.equal(result2, "second")
7+
}
8+
9+
let f = async () => ()
10+
let f = async (. ()) => ()
11+
let f = async (f) => f()
12+
let f = async (a, b) => a + b
13+
let f = async (. a, b) => a + b

0 commit comments

Comments
 (0)