diff --git a/CHANGELOG.md b/CHANGELOG.md index 586b648e..4a5e05fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## Unreleased +* Improve printing of trailing comments under lhs of "pipe" expression in [#329](https://github.com/rescript-lang/syntax/pull/239/files) * Improve printing of jsx children and props with leading line comment in [#236](https://github.com/rescript-lang/syntax/pull/236) * Improve conversion of quoted strings from Reason in [#238](https://github.com/rescript-lang/syntax/pull/238) * Print attributes/extension without bs prefix where possible in [#230](https://github.com/rescript-lang/syntax/pull/230) diff --git a/src/res_printer.ml b/src/res_printer.ml index d075edb1..63fc1f61 100644 --- a/src/res_printer.ml +++ b/src/res_printer.ml @@ -58,6 +58,14 @@ let hasLeadingLineComment tbl loc = | Some comment -> Comment.isSingleLineComment comment | None -> false +let hasCommentBelow tbl loc = + match Hashtbl.find tbl.CommentTable.trailing loc with + | comment::_ -> + let commentLoc = Comment.loc comment in + commentLoc.Location.loc_start.pos_lnum > loc.Location.loc_end.pos_lnum + | [] -> false + | exception Not_found -> false + let printMultilineCommentContent txt = (* Turns * |* first line @@ -3460,15 +3468,19 @@ and printBinaryExpression (expr : Parsetree.expression) cmtTbl = ParsetreeViewer.isBinaryExpression lhs || ParsetreeViewer.isBinaryExpression rhs ) -> + let lhsHasCommentBelow = hasCommentBelow cmtTbl lhs.pexp_loc in let lhsDoc = printOperand ~isLhs:true lhs op in let rhsDoc = printOperand ~isLhs:false rhs op in Doc.group ( Doc.concat [ lhsDoc; - (match op with - | "|." -> Doc.text "->" - | "|>" -> Doc.text " |> " - | _ -> assert false); + (match lhsHasCommentBelow, op with + | true, "|." -> Doc.concat [Doc.softLine; Doc.text "->"] + | false, "|." -> Doc.text "->" + | true, "|>" -> Doc.concat [Doc.line; Doc.text "|> "] + | false, "|>" -> Doc.text " |> " + | _ -> Doc.nil + ); rhsDoc; ] ) diff --git a/tests/printer/comments/__snapshots__/render.spec.js.snap b/tests/printer/comments/__snapshots__/render.spec.js.snap index 736ddaa9..29d9aae4 100644 --- a/tests/printer/comments/__snapshots__/render.spec.js.snap +++ b/tests/printer/comments/__snapshots__/render.spec.js.snap @@ -37,6 +37,64 @@ let truth = /* c0 */ a /* c1 */ == /* c2 */ b /* c3 */ && /* c4 */ c /* c5 */ == /* c6 */ d /* c7 */ && /* c8 */ e /* c9 */ == /* c10 */ f /* c11 */ + +promise +// TODO: This comment needs to be here +->Js.Promise.then(payload => { + Js.log2(\\"this payload was received\\", payload) + Js.Promise.resolve(\\"\\"->Obj.magic) +}) + +promise +/* TODO: This comment needs to be here */ +->Js.Promise.then(payload => { + Js.log2(\\"this payload was received\\", payload) + Js.Promise.resolve(\\"\\"->Obj.magic) +}) + +promise +// comment +->Js.Promise.then(payload => { + Js.Promise.resolve(\\"\\"->Obj.magic) +}) +// comment 2 +->Js.Promise.catch(err => { + Js.log2(\\"this error was caught\\", err) + Js.Promise.resolve(\\"\\"->Obj.magic) +}) + +promise +/* comment */ +->Js.Promise.then(payload => { + Js.Promise.resolve(\\"\\"->Obj.magic) +}) +/* comment 2 */ + +// comment 3 + +/* comment 4 */ +->Js.Promise.catch(err => { + Js.log2(\\"this error was caught\\", err) + Js.Promise.resolve(\\"\\"->Obj.magic) +}) + +promise +// TODO: This comment needs to be here +|> Js.Promise.then(payload => { + Js.log2(\\"this payload was received\\", payload) + Js.Promise.resolve(\\"\\"->Obj.magic) +}) + +promise +// comment +|> Js.Promise.then(payload => { + Js.Promise.resolve(\\"\\"->Obj.magic) +}) +// comment 2 +|> Js.Promise.catch(err => { + Js.log2(\\"this error was caught\\", err) + Js.Promise.resolve(\\"\\"->Obj.magic) +}) " `; diff --git a/tests/printer/comments/binaryExpr.res b/tests/printer/comments/binaryExpr.res index 55b20470..288d074c 100644 --- a/tests/printer/comments/binaryExpr.res +++ b/tests/printer/comments/binaryExpr.res @@ -34,3 +34,63 @@ let truth = /* c0 */ a /* c1 */ == /* c2 */ b /* c3 */ && /* c4 */ c /* c5 */ == /* c6 */ d /* c7 */ && /* c8 */ e /* c9 */ == /* c10 */ f /* c11 */ + +promise +// TODO: This comment needs to be here +->Js.Promise.then(payload => { + Js.log2("this payload was received", payload); + Js.Promise.resolve(""->Obj.magic); +}) + + +promise +/* TODO: This comment needs to be here */ +->Js.Promise.then(payload => { + Js.log2("this payload was received", payload); + Js.Promise.resolve(""->Obj.magic); +}) + +promise +// comment +->Js.Promise.then(payload => { + Js.Promise.resolve(""->Obj.magic); +}) +// comment 2 +->Js.Promise.catch(err => { + Js.log2("this error was caught", err); + Js.Promise.resolve(""->Obj.magic); +}) + + +promise +/* comment */ +->Js.Promise.then(payload => { + Js.Promise.resolve(""->Obj.magic); +}) +/* comment 2 */ + +// comment 3 + +/* comment 4 */ +->Js.Promise.catch(err => { + Js.log2("this error was caught", err); + Js.Promise.resolve(""->Obj.magic); +}) + +promise +// TODO: This comment needs to be here +|> Js.Promise.then(payload => { + Js.log2("this payload was received", payload); + Js.Promise.resolve(""->Obj.magic); +}) + +promise +// comment +|> Js.Promise.then(payload => { + Js.Promise.resolve(""->Obj.magic); +}) +// comment 2 +|> Js.Promise.catch(err => { + Js.log2("this error was caught", err); + Js.Promise.resolve(""->Obj.magic); +})