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

Commit 7512a60

Browse files
Maximcristianoc
Maxim
authored andcommitted
Implement printing of parens (precedence) for await expressions
1 parent 2246e6e commit 7512a60

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/res_parens.ml

+14
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ let callExpr expr =
4545
| Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
4646
} ->
4747
Parenthesized
48+
| _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
49+
Parenthesized
4850
| _ -> Nothing)
4951

5052
let structureExpr expr =
@@ -96,6 +98,8 @@ let unaryExprOperand expr =
9698
| Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
9799
} ->
98100
Parenthesized
101+
| _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
102+
Parenthesized
99103
| _ -> Nothing)
100104

101105
let binaryExprOperand ~isLhs expr =
@@ -120,6 +124,8 @@ let binaryExprOperand ~isLhs expr =
120124
| expr when ParsetreeViewer.isBinaryExpression expr -> Parenthesized
121125
| expr when ParsetreeViewer.isTernaryExpr expr -> Parenthesized
122126
| {pexp_desc = Pexp_lazy _ | Pexp_assert _} when isLhs -> Parenthesized
127+
| _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
128+
Parenthesized
123129
| {Parsetree.pexp_attributes = attrs} ->
124130
if ParsetreeViewer.hasPrintableAttributes attrs then Parenthesized
125131
else Nothing)
@@ -196,6 +202,8 @@ let lazyOrAssertExprRhs expr =
196202
| Pexp_try _ | Pexp_while _ | Pexp_for _ | Pexp_ifthenelse _ );
197203
} ->
198204
Parenthesized
205+
| _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
206+
Parenthesized
199207
| _ -> Nothing)
200208

201209
let isNegativeConstant constant =
@@ -240,6 +248,8 @@ let fieldExpr expr =
240248
| Pexp_ifthenelse _ );
241249
} ->
242250
Parenthesized
251+
| _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
252+
Parenthesized
243253
| _ -> Nothing)
244254

245255
let setFieldExprRhs expr =
@@ -302,6 +312,8 @@ let jsxPropExpr expr =
302312
}
303313
when startsWithMinus x ->
304314
Parenthesized
315+
| _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
316+
Parenthesized
305317
| {
306318
Parsetree.pexp_desc =
307319
( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _
@@ -338,6 +350,8 @@ let jsxChildExpr expr =
338350
}
339351
when startsWithMinus x ->
340352
Parenthesized
353+
| _ when ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes ->
354+
Parenthesized
341355
| {
342356
Parsetree.pexp_desc =
343357
( Pexp_ident _ | Pexp_constant _ | Pexp_field _ | Pexp_construct _

tests/printer/expr/asyncAwait.res

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,17 @@ let f = async (. a, b) => a + b
1616
let maybeSomeValue = switch await fetchData(url) {
1717
| data => Some(data)
1818
| exception JsError(_) => None
19-
}
19+
}
20+
21+
(await f)(a, b)
22+
-(await f)
23+
await 1 + await 2
24+
25+
lazy (await f())
26+
assert (await f())
27+
28+
(await f).json()
29+
30+
user.data = await fetch()
31+
32+
<Navbar promise={await gc()}>{await weirdReactSuspenseApi}</Navbar>

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

+13
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,16 @@ let maybeSomeValue = switch await fetchData(url) {
1616
| data => Some(data)
1717
| exception JsError(_) => None
1818
}
19+
20+
(await f)(a, b)
21+
-(await f)
22+
(await 1) + (await 2)
23+
24+
lazy (await f())
25+
assert (await f())
26+
27+
(await f).json()
28+
29+
user.data = await fetch()
30+
31+
<Navbar promise={await gc()}> {await weirdReactSuspenseApi} </Navbar>

0 commit comments

Comments
 (0)