From cd0a368ab721bb3f6bb098f05bb93ab84eb39736 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 16 Oct 2022 09:35:28 +0100 Subject: [PATCH 1/3] Fix issue where nested pipe discards await Fixes https://github.com/rescript-lang/syntax/issues/687 Certain nested patterns for binary operators are printed "manually" so the special printing for await is not used. --- src/res_printer.ml | 10 ++++++++-- tests/printer/expr/asyncAwait.res | 3 +++ tests/printer/expr/expected/asyncAwait.res.txt | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/res_printer.ml b/src/res_printer.ml index 8a0bf8cc..cb67d916 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -3604,17 +3604,23 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl = | [] -> doc | _ -> addParens doc in + let isAwait = + ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes + in let doc = Doc.concat [ + (if isAwait then Doc.text "await " else Doc.nil); leftPrinted; printBinaryOperator ~inlineRhs:false operator; rightPrinted; ] in let doc = - if (not isLhs) && Parens.rhsBinaryExprOperand operator expr then - Doc.concat [Doc.lparen; doc; Doc.rparen] + if + isAwait + || ((not isLhs) && Parens.rhsBinaryExprOperand operator expr) + then Doc.concat [Doc.lparen; doc; Doc.rparen] else doc in printComments doc cmtTbl expr.pexp_loc diff --git a/tests/printer/expr/asyncAwait.res b/tests/printer/expr/asyncAwait.res index 32c77349..765e6597 100644 --- a/tests/printer/expr/asyncAwait.res +++ b/tests/printer/expr/asyncAwait.res @@ -96,3 +96,6 @@ let f11 = (. ~x) => (. ~y) => 3 let f12 = @a (@b x) => 3 let f13 = @a @b (~x) => 3 + +let aw = (await (server->start))->foo +let aw = (@foo (server->start))->foo \ No newline at end of file diff --git a/tests/printer/expr/expected/asyncAwait.res.txt b/tests/printer/expr/expected/asyncAwait.res.txt index 01dab207..38190d16 100644 --- a/tests/printer/expr/expected/asyncAwait.res.txt +++ b/tests/printer/expr/expected/asyncAwait.res.txt @@ -118,3 +118,6 @@ let f11 = (. ~x) => (. ~y) => 3 let f12 = @a x => 3 let f13 = (@a @b ~x) => 3 + +let aw = (await server->start)->foo +let aw = @foo (server->start)->foo From e895708cc7ccc72d07bbff3ef271fe0f63f263dc Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 16 Oct 2022 09:36:30 +0100 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26273502..87a36ca1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -42,6 +42,7 @@ - Fix issue where the formatter would delete `async` in a function with labelled arguments. - Fix several printing issues with `async` including an infinite loop https://github.com/rescript-lang/syntax/pull/680 - Fix issue where certain JSX expressions would be formatted differenctly in compiler 10.1.0-rc.1 https://github.com/rescript-lang/syntax/issues/675 +- Fix issue where printing nested pipe discards await https://github.com/rescript-lang/syntax/issues/687 #### :eyeglasses: Spec Compliance From 629194c36fffd5d7e94320d601c10c78975d8409 Mon Sep 17 00:00:00 2001 From: Cristiano Calcagno Date: Sun, 16 Oct 2022 09:49:33 +0100 Subject: [PATCH 3/3] parens --- src/res_printer.ml | 31 ++++++++++++------- .../printer/expr/expected/asyncAwait.res.txt | 2 +- 2 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/res_printer.ml b/src/res_printer.ml index cb67d916..f3625e88 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -3608,19 +3608,28 @@ and printBinaryExpression ~customLayout (expr : Parsetree.expression) cmtTbl = ParsetreeViewer.hasAwaitAttribute expr.pexp_attributes in let doc = - Doc.concat - [ - (if isAwait then Doc.text "await " else Doc.nil); - leftPrinted; - printBinaryOperator ~inlineRhs:false operator; - rightPrinted; - ] + if isAwait then + Doc.concat + [ + Doc.text "await "; + Doc.lparen; + leftPrinted; + printBinaryOperator ~inlineRhs:false operator; + rightPrinted; + Doc.rparen; + ] + else + Doc.concat + [ + leftPrinted; + printBinaryOperator ~inlineRhs:false operator; + rightPrinted; + ] in + let doc = - if - isAwait - || ((not isLhs) && Parens.rhsBinaryExprOperand operator expr) - then Doc.concat [Doc.lparen; doc; Doc.rparen] + if (not isLhs) && Parens.rhsBinaryExprOperand operator expr then + Doc.concat [Doc.lparen; doc; Doc.rparen] else doc in printComments doc cmtTbl expr.pexp_loc diff --git a/tests/printer/expr/expected/asyncAwait.res.txt b/tests/printer/expr/expected/asyncAwait.res.txt index 38190d16..bfd223bc 100644 --- a/tests/printer/expr/expected/asyncAwait.res.txt +++ b/tests/printer/expr/expected/asyncAwait.res.txt @@ -119,5 +119,5 @@ let f11 = (. ~x) => (. ~y) => 3 let f12 = @a x => 3 let f13 = (@a @b ~x) => 3 -let aw = (await server->start)->foo +let aw = await (server->start)->foo let aw = @foo (server->start)->foo