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

Commit c15dee0

Browse files
author
Maxim
committed
Implement printing of parens for rhs of await expression correctly.
1 parent bff364e commit c15dee0

File tree

7 files changed

+141
-10
lines changed

7 files changed

+141
-10
lines changed

src/res_parens.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ let flattenOperandRhs parentOperator rhs =
175175
| _ when ParsetreeViewer.isTernaryExpr rhs -> true
176176
| _ -> false
177177

178-
let lazyOrAssertExprRhs expr =
178+
let lazyOrAssertOrAwaitExprRhs expr =
179179
let optBraces, _ = ParsetreeViewer.processBracesAttr expr in
180180
match optBraces with
181181
| Some ({Location.loc = bracesLoc}, _) -> Braced bracesLoc

src/res_parens.mli

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ val subBinaryExprOperand : string -> string -> bool
1010
val rhsBinaryExprOperand : string -> Parsetree.expression -> bool
1111
val flattenOperandRhs : string -> Parsetree.expression -> bool
1212

13-
val lazyOrAssertExprRhs : Parsetree.expression -> kind
13+
val lazyOrAssertOrAwaitExprRhs : Parsetree.expression -> kind
1414

1515
val fieldExpr : Parsetree.expression -> kind
1616

src/res_parsetree_viewer.ml

+2
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,8 @@ let isPrintableAttribute attr =
532532

533533
let hasPrintableAttributes attrs = List.exists isPrintableAttribute attrs
534534

535+
let filterPrintableAttributes attrs = List.filter isPrintableAttribute attrs
536+
535537
let partitionPrintableAttributes attrs =
536538
List.partition isPrintableAttribute attrs
537539

src/res_parsetree_viewer.mli

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ val hasOptionalAttribute : Parsetree.attributes -> bool
9999
val shouldIndentBinaryExpr : Parsetree.expression -> bool
100100
val shouldInlineRhsBinaryExpr : Parsetree.expression -> bool
101101
val hasPrintableAttributes : Parsetree.attributes -> bool
102+
val filterPrintableAttributes : Parsetree.attributes -> Parsetree.attributes
102103
val partitionPrintableAttributes :
103104
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes
104105

src/res_printer.ml

+21-8
Original file line numberDiff line numberDiff line change
@@ -3095,7 +3095,7 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
30953095
| Pexp_assert expr ->
30963096
let rhs =
30973097
let doc = printExpressionWithComments ~customLayout expr cmtTbl in
3098-
match Parens.lazyOrAssertExprRhs expr with
3098+
match Parens.lazyOrAssertOrAwaitExprRhs expr with
30993099
| Parens.Parenthesized -> addParens doc
31003100
| Braced braces -> printBraces doc expr braces
31013101
| Nothing -> doc
@@ -3104,7 +3104,7 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
31043104
| Pexp_lazy expr ->
31053105
let rhs =
31063106
let doc = printExpressionWithComments ~customLayout expr cmtTbl in
3107-
match Parens.lazyOrAssertExprRhs expr with
3107+
match Parens.lazyOrAssertOrAwaitExprRhs expr with
31083108
| Parens.Parenthesized -> addParens doc
31093109
| Braced braces -> printBraces doc expr braces
31103110
| Nothing -> doc
@@ -3282,7 +3282,24 @@ and printExpression ~customLayout (e : Parsetree.expression) cmtTbl =
32823282
in
32833283
let exprWithAwait =
32843284
if ParsetreeViewer.hasAwaitAttribute e.pexp_attributes then
3285-
Doc.concat [Doc.text "await "; printedExpression]
3285+
let rhs =
3286+
match
3287+
Parens.lazyOrAssertOrAwaitExprRhs
3288+
{
3289+
e with
3290+
pexp_attributes =
3291+
List.filter
3292+
(function
3293+
| {Location.txt = "res.await" | "ns.braces"}, _ -> false
3294+
| _ -> true)
3295+
e.pexp_attributes;
3296+
}
3297+
with
3298+
| Parens.Parenthesized -> addParens printedExpression
3299+
| Braced braces -> printBraces printedExpression e braces
3300+
| Nothing -> printedExpression
3301+
in
3302+
Doc.concat [Doc.text "await "; rhs]
32863303
else printedExpression
32873304
in
32883305
let shouldPrintItsOwnAttributes =
@@ -3680,11 +3697,7 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl =
36803697
{
36813698
expr with
36823699
pexp_attributes =
3683-
List.filter
3684-
(fun attr ->
3685-
match attr with
3686-
| {Location.txt = "ns.braces"}, _ -> false
3687-
| _ -> true)
3700+
ParsetreeViewer.filterPrintableAttributes
36883701
expr.pexp_attributes;
36893702
}
36903703
with

tests/printer/expr/asyncAwait.res

+46
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,50 @@ user.data = await fetch()
3434
let inBinaryExpression = await x->Js.Promise.resolve + 1
3535
let inBinaryExpression = await x->Js.Promise.resolve + await y->Js.Promise.resolve
3636

37+
let withCallback = async (. ()) => {
38+
async (. x) => await (x->Js.promise.resolve) + 1
39+
}
40+
3741
let () = await (await fetch(url))->(await resolve)
42+
43+
let _ = await (1 + 1)
44+
let _ = await 1 + 1
45+
let _ = await (-1)
46+
let _ = await - 1
47+
let _ = await !ref
48+
let _ = await f
49+
let _ = await %extension
50+
let _ = await "test"
51+
let _ = await ((a, b) => a + b)
52+
let _ = await (async (a, b) => a + b)
53+
let _ = await (switch x { | A => () | B => ()})
54+
let _ = await [1, 2, 3]
55+
let _ = await (1, 2, 3)
56+
let _ = await {name: "Steve", age: 32}
57+
let _ = await (user.name = "Steve")
58+
let _ = await (if 20 { true } else {false})
59+
let _ = await (condition() ? true : false)
60+
let _ = await f(x)
61+
let _ = await f(. x)
62+
let _ = await (f(x) : Js.Promise.t<unit>)
63+
let _ = await (while true { infiniteLoop() })
64+
let _ = await (try ok() catch { | _ => logError() })
65+
let _ = await (for i in 0 to 10 { sideEffect()})
66+
let _ = await (lazy x)
67+
let _ = await (assert x)
68+
let _ = await promises[0]
69+
let _ = await promises["resolved"]
70+
let _ = await (promises["resolved"] = sideEffect())
71+
let _ = await (@attr expr)
72+
let _ = await module(Foo)
73+
let _ = await module(Foo: Bar)
74+
let _ = await Promise
75+
let _ = await Promise(status)
76+
77+
// braces are being dropped, because the ast only has space to record braces surrounding the await
78+
let _ = await {x}
79+
// here braces stay, because of precedence
80+
let _ = await {
81+
let x = 1
82+
Js.Promise.resolve(x)
83+
}

tests/printer/expr/expected/asyncAwait.res.txt

+69
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,73 @@ user.data = await fetch()
3333
let inBinaryExpression = (await x)->Js.Promise.resolve + 1
3434
let inBinaryExpression = (await x)->Js.Promise.resolve + (await y)->Js.Promise.resolve
3535

36+
let withCallback = async (. ()) => {
37+
async (. x) => await (x->Js.promise.resolve) + 1
38+
}
39+
3640
let () = (await fetch(url))->(await resolve)
41+
42+
let _ = await (1 + 1)
43+
let _ = (await 1) + 1
44+
let _ = await -1
45+
let _ = await -1
46+
let _ = await !ref
47+
let _ = await f
48+
let _ = await %extension
49+
let _ = await "test"
50+
let _ = await ((a, b) => a + b)
51+
let _ = await (async (a, b) => a + b)
52+
let _ = await (
53+
switch x {
54+
| A => ()
55+
| B => ()
56+
}
57+
)
58+
let _ = await [1, 2, 3]
59+
let _ = await (1, 2, 3)
60+
let _ = await {name: "Steve", age: 32}
61+
let _ = await (user.name = "Steve")
62+
let _ = await (
63+
if 20 {
64+
true
65+
} else {
66+
false
67+
}
68+
)
69+
let _ = await (condition() ? true : false)
70+
let _ = await f(x)
71+
let _ = await f(. x)
72+
let _ = (await (f(x): Js.Promise.t<unit>))
73+
let _ = await (
74+
while true {
75+
infiniteLoop()
76+
}
77+
)
78+
let _ = await (
79+
try ok() catch {
80+
| _ => logError()
81+
}
82+
)
83+
let _ = await (
84+
for i in 0 to 10 {
85+
sideEffect()
86+
}
87+
)
88+
let _ = await (lazy x)
89+
let _ = await (assert x)
90+
let _ = await promises[0]
91+
let _ = await promises["resolved"]
92+
let _ = await promises["resolved"] = sideEffect()
93+
let _ = @attr await (expr)
94+
let _ = await module(Foo)
95+
let _ = await module(Foo: Bar)
96+
let _ = await Promise
97+
let _ = await Promise(status)
98+
99+
// braces are being dropped, because the ast only has space to record braces surrounding the await
100+
let _ = await x
101+
// here braces stay, because of precedence
102+
let _ = await {
103+
let x = 1
104+
Js.Promise.resolve(x)
105+
}

0 commit comments

Comments
 (0)