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

Commit 2246e6e

Browse files
Maximcristianoc
Maxim
authored andcommitted
Implement printer support for await expressions
1 parent 0570b2f commit 2246e6e

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

src/res_parsetree_viewer.ml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ let processFunctionAttributes attrs =
6565
in
6666
process false false [] attrs
6767

68+
let hasAwaitAttribute attrs =
69+
List.exists
70+
(function
71+
| {Location.txt = "await"}, _ -> true
72+
| _ -> false)
73+
attrs
74+
6875
let collectListExpressions expr =
6976
let rec collect acc expr =
7077
match expr.pexp_desc with
@@ -178,8 +185,8 @@ let filterParsingAttrs attrs =
178185
match attr with
179186
| ( {
180187
Location.txt =
181-
( "ns.ternary" | "ns.braces" | "res.template" | "bs" | "ns.iflet"
182-
| "ns.namedArgLoc" | "ns.optional" );
188+
( "ns.ternary" | "ns.braces" | "res.template" | "await" | "bs"
189+
| "ns.iflet" | "ns.namedArgLoc" | "ns.optional" );
183190
},
184191
_ ) ->
185192
false

src/res_parsetree_viewer.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ val processFunctionAttributes :
2222
Parsetree.attributes ->
2323
bool (* async *) * bool (* uncurried *) * Parsetree.attributes
2424

25+
val hasAwaitAttribute : Parsetree.attributes -> bool
26+
2527
type ifConditionKind =
2628
| If of Parsetree.expression
2729
| IfLet of Parsetree.pattern * Parsetree.expression

src/res_printer.ml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,11 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
32783278
| Pexp_poly _ -> Doc.text "Pexp_poly not impemented in printer"
32793279
| Pexp_object _ -> Doc.text "Pexp_object not impemented in printer"
32803280
in
3281+
let exprWithAwait =
3282+
if ParsetreeViewer.hasAwaitAttribute e.pexp_attributes then
3283+
Doc.concat [Doc.text "await "; printedExpression]
3284+
else printedExpression
3285+
in
32813286
let shouldPrintItsOwnAttributes =
32823287
match e.pexp_desc with
32833288
| Pexp_apply _ | Pexp_fun _ | Pexp_newtype _ | Pexp_setfield _
@@ -3289,12 +3294,11 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
32893294
| _ -> false
32903295
in
32913296
match e.pexp_attributes with
3292-
| [] -> printedExpression
3297+
| [] -> exprWithAwait
32933298
| attrs when not shouldPrintItsOwnAttributes ->
32943299
Doc.group
3295-
(Doc.concat
3296-
[printAttributes ~customLayout attrs cmtTbl; printedExpression])
3297-
| _ -> printedExpression
3300+
(Doc.concat [printAttributes ~customLayout attrs cmtTbl; exprWithAwait])
3301+
| _ -> exprWithAwait
32983302

32993303
and printPexpFun ~customLayout ~inCallback e cmtTbl =
33003304
let attrsOnArrow, parameters, returnExpr = ParsetreeViewer.funExpr e in

tests/printer/expr/asyncAwait.res

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,10 @@ let f = async () => ()
1010
let f = async (.) => ()
1111
let f = async f => f()
1212
let f = async (a, b) => a + b
13-
let f = async (. a, b) => a + b
13+
let f = async (. a, b) => a + b
14+
15+
16+
let maybeSomeValue = switch await fetchData(url) {
17+
| data => Some(data)
18+
| exception JsError(_) => None
19+
}
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
let sequentialAwait = async () => {
2-
let result1 = @await paused("first")
2+
let result1 = await paused("first")
33
nodeJsAssert.equal(result1, "first")
44

5-
let result2 = @await paused("second")
5+
let result2 = await paused("second")
66
nodeJsAssert.equal(result2, "second")
77
}
88

@@ -11,3 +11,8 @@ let f = async (. ()) => ()
1111
let f = async (f) => f()
1212
let f = async (a, b) => a + b
1313
let f = async (. a, b) => a + b
14+
15+
let maybeSomeValue = switch await fetchData(url) {
16+
| data => Some(data)
17+
| exception JsError(_) => None
18+
}

0 commit comments

Comments
 (0)