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

Commit bff364e

Browse files
author
Maxim
committed
Fix printing of await expression inside binary expressions
1 parent 3b17a08 commit bff364e

File tree

6 files changed

+26
-18
lines changed

6 files changed

+26
-18
lines changed

src/res_parsetree_viewer.ml

+2-4
Original file line numberDiff line numberDiff line change
@@ -523,17 +523,15 @@ let isPrintableAttribute attr =
523523
match attr with
524524
| ( {
525525
Location.txt =
526-
( "bs" | "res.template" | "ns.ternary" | "ns.braces" | "ns.iflet"
527-
| "JSX" );
526+
( "bs" | "ns.iflet" | "ns.braces" | "JSX" | "res.async" | "res.await"
527+
| "res.template" | "ns.ternary" );
528528
},
529529
_ ) ->
530530
false
531531
| _ -> true
532532

533533
let hasPrintableAttributes attrs = List.exists isPrintableAttribute attrs
534534

535-
let filterPrintableAttributes attrs = List.filter isPrintableAttribute attrs
536-
537535
let partitionPrintableAttributes attrs =
538536
List.partition isPrintableAttribute attrs
539537

src/res_parsetree_viewer.mli

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ 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
103102
val partitionPrintableAttributes :
104103
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes
105104

src/res_printer.ml

+13-10
Original file line numberDiff line numberDiff line change
@@ -3519,28 +3519,28 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl =
35193519
then
35203520
let leftPrinted = flatten ~isLhs:true left operator in
35213521
let rightPrinted =
3522-
let _, rightAttrs =
3522+
let rightPrinteableAttrs, rightInternalAttrs =
35233523
ParsetreeViewer.partitionPrintableAttributes
35243524
right.pexp_attributes
35253525
in
35263526
let doc =
35273527
printExpressionWithComments ~customLayout
3528-
{right with pexp_attributes = rightAttrs}
3528+
{right with pexp_attributes = rightInternalAttrs}
35293529
cmtTbl
35303530
in
35313531
let doc =
35323532
if Parens.flattenOperandRhs parentOperator right then
35333533
Doc.concat [Doc.lparen; doc; Doc.rparen]
35343534
else doc
35353535
in
3536-
let printableAttrs =
3537-
ParsetreeViewer.filterPrintableAttributes right.pexp_attributes
3538-
in
35393536
let doc =
35403537
Doc.concat
3541-
[printAttributes ~customLayout printableAttrs cmtTbl; doc]
3538+
[
3539+
printAttributes ~customLayout rightPrinteableAttrs cmtTbl;
3540+
doc;
3541+
]
35423542
in
3543-
match printableAttrs with
3543+
match rightPrinteableAttrs with
35443544
| [] -> doc
35453545
| _ -> addParens doc
35463546
in
@@ -3559,22 +3559,25 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl =
35593559
in
35603560
printComments doc cmtTbl expr.pexp_loc
35613561
else
3562+
let printeableAttrs, internalAttrs =
3563+
ParsetreeViewer.partitionPrintableAttributes expr.pexp_attributes
3564+
in
35623565
let doc =
35633566
printExpressionWithComments ~customLayout
3564-
{expr with pexp_attributes = []}
3567+
{expr with pexp_attributes = internalAttrs}
35653568
cmtTbl
35663569
in
35673570
let doc =
35683571
if
35693572
Parens.subBinaryExprOperand parentOperator operator
3570-
|| expr.pexp_attributes <> []
3573+
|| printeableAttrs <> []
35713574
&& (ParsetreeViewer.isBinaryExpression expr
35723575
|| ParsetreeViewer.isTernaryExpr expr)
35733576
then Doc.concat [Doc.lparen; doc; Doc.rparen]
35743577
else doc
35753578
in
35763579
Doc.concat
3577-
[printAttributes ~customLayout expr.pexp_attributes cmtTbl; doc]
3580+
[printAttributes ~customLayout printeableAttrs cmtTbl; doc]
35783581
| _ -> assert false
35793582
else
35803583
match expr.pexp_desc with

test.res

-2
This file was deleted.

tests/printer/expr/asyncAwait.res

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ assert (await f())
2929

3030
user.data = await fetch()
3131

32-
<Navbar promise={await gc()}>{await weirdReactSuspenseApi}</Navbar>
32+
<Navbar promise={await gc()}>{await weirdReactSuspenseApi}</Navbar>
33+
34+
let inBinaryExpression = await x->Js.Promise.resolve + 1
35+
let inBinaryExpression = await x->Js.Promise.resolve + await y->Js.Promise.resolve
36+
37+
let () = await (await fetch(url))->(await resolve)

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

+5
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ assert (await f())
2929
user.data = await fetch()
3030

3131
<Navbar promise={await gc()}> {await weirdReactSuspenseApi} </Navbar>
32+
33+
let inBinaryExpression = (await x)->Js.Promise.resolve + 1
34+
let inBinaryExpression = (await x)->Js.Promise.resolve + (await y)->Js.Promise.resolve
35+
36+
let () = (await fetch(url))->(await resolve)

0 commit comments

Comments
 (0)