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

Fix printing of trailing comments under lhs of "pipe" expression. #239

Merged
merged 2 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
20 changes: 16 additions & 4 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
]
)
Expand Down
58 changes: 58 additions & 0 deletions tests/printer/comments/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
"
`;

Expand Down
60 changes: 60 additions & 0 deletions tests/printer/comments/binaryExpr.res
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})