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

Commit

Permalink
Fix printing of await expression inside binary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim authored and cristianoc committed Jul 11, 2022
1 parent 4a22b85 commit 46d3d59
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
6 changes: 2 additions & 4 deletions src/res_parsetree_viewer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -523,17 +523,15 @@ let isPrintableAttribute attr =
match attr with
| ( {
Location.txt =
( "bs" | "res.template" | "ns.ternary" | "ns.braces" | "ns.iflet"
| "JSX" );
( "bs" | "ns.iflet" | "ns.braces" | "JSX" | "res.async" | "res.await"
| "res.template" | "ns.ternary" );
},
_ ) ->
false
| _ -> true

let hasPrintableAttributes attrs = List.exists isPrintableAttribute attrs

let filterPrintableAttributes attrs = List.filter isPrintableAttribute attrs

let partitionPrintableAttributes attrs =
List.partition isPrintableAttribute attrs

Expand Down
1 change: 0 additions & 1 deletion src/res_parsetree_viewer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@ val hasOptionalAttribute : Parsetree.attributes -> bool
val shouldIndentBinaryExpr : Parsetree.expression -> bool
val shouldInlineRhsBinaryExpr : Parsetree.expression -> bool
val hasPrintableAttributes : Parsetree.attributes -> bool
val filterPrintableAttributes : Parsetree.attributes -> Parsetree.attributes
val partitionPrintableAttributes :
Parsetree.attributes -> Parsetree.attributes * Parsetree.attributes

Expand Down
23 changes: 13 additions & 10 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -3519,28 +3519,28 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl =
then
let leftPrinted = flatten ~isLhs:true left operator in
let rightPrinted =
let _, rightAttrs =
let rightPrinteableAttrs, rightInternalAttrs =
ParsetreeViewer.partitionPrintableAttributes
right.pexp_attributes
in
let doc =
printExpressionWithComments ~customLayout
{right with pexp_attributes = rightAttrs}
{right with pexp_attributes = rightInternalAttrs}
cmtTbl
in
let doc =
if Parens.flattenOperandRhs parentOperator right then
Doc.concat [Doc.lparen; doc; Doc.rparen]
else doc
in
let printableAttrs =
ParsetreeViewer.filterPrintableAttributes right.pexp_attributes
in
let doc =
Doc.concat
[printAttributes ~customLayout printableAttrs cmtTbl; doc]
[
printAttributes ~customLayout rightPrinteableAttrs cmtTbl;
doc;
]
in
match printableAttrs with
match rightPrinteableAttrs with
| [] -> doc
| _ -> addParens doc
in
Expand All @@ -3559,22 +3559,25 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl =
in
printComments doc cmtTbl expr.pexp_loc
else
let printeableAttrs, internalAttrs =
ParsetreeViewer.partitionPrintableAttributes expr.pexp_attributes
in
let doc =
printExpressionWithComments ~customLayout
{expr with pexp_attributes = []}
{expr with pexp_attributes = internalAttrs}
cmtTbl
in
let doc =
if
Parens.subBinaryExprOperand parentOperator operator
|| expr.pexp_attributes <> []
|| printeableAttrs <> []
&& (ParsetreeViewer.isBinaryExpression expr
|| ParsetreeViewer.isTernaryExpr expr)
then Doc.concat [Doc.lparen; doc; Doc.rparen]
else doc
in
Doc.concat
[printAttributes ~customLayout expr.pexp_attributes cmtTbl; doc]
[printAttributes ~customLayout printeableAttrs cmtTbl; doc]
| _ -> assert false
else
match expr.pexp_desc with
Expand Down
2 changes: 0 additions & 2 deletions test.res

This file was deleted.

7 changes: 6 additions & 1 deletion tests/printer/expr/asyncAwait.res
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ assert (await f())

user.data = await fetch()

<Navbar promise={await gc()}>{await weirdReactSuspenseApi}</Navbar>
<Navbar promise={await gc()}>{await weirdReactSuspenseApi}</Navbar>

let inBinaryExpression = await x->Js.Promise.resolve + 1
let inBinaryExpression = await x->Js.Promise.resolve + await y->Js.Promise.resolve

let () = await (await fetch(url))->(await resolve)
5 changes: 5 additions & 0 deletions tests/printer/expr/expected/asyncAwait.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ assert (await f())
user.data = await fetch()

<Navbar promise={await gc()}> {await weirdReactSuspenseApi} </Navbar>

let inBinaryExpression = (await x)->Js.Promise.resolve + 1
let inBinaryExpression = (await x)->Js.Promise.resolve + (await y)->Js.Promise.resolve

let () = (await fetch(url))->(await resolve)

0 comments on commit 46d3d59

Please sign in to comment.