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

fix: put > on a new line if the tag or last prop has trailing comments #685

Merged
merged 1 commit into from
Oct 16, 2022
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
37 changes: 12 additions & 25 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,16 @@ let hasNestedJsxOrMoreThanOneChild expr =
in
loop false expr

let hasTailSingleLineComment tbl loc =
let rec getLastElement elements =
match elements with
| [] -> None
| [element] -> Some element
| _ :: rest -> getLastElement rest
in
match Hashtbl.find_opt tbl.CommentTable.trailing loc with
| None -> false
| Some comments -> (
let lastComment = getLastElement comments in
match lastComment with
| None -> false
| Some comment -> Comment.isSingleLineComment comment)

let hasCommentsInside tbl loc =
match Hashtbl.find_opt tbl.CommentTable.inside loc with
| None -> false
| _ -> true

let hasTrailingComments tbl loc =
match Hashtbl.find_opt tbl.CommentTable.trailing loc with
| None -> false
| _ -> true

let printMultilineCommentContent txt =
(* Turns
* |* first line
Expand Down Expand Up @@ -4058,18 +4048,15 @@ and printJsxExpression ~customLayout lident args cmtTbl =
when isSelfClosing ->
Doc.text "/>"
| _ ->
(* if last trailing comment of tag is single line comment then put > on the next line
(* if tag A has trailing comments then put > on the next line
<A
// single line comment
// comments
>
</A>
*)
if hasTailSingleLineComment cmtTbl lident.Asttypes.loc then
if hasTrailingComments cmtTbl lident.Asttypes.loc then
Doc.concat [Doc.softLine; Doc.greaterThan]
else
Doc.ifBreaks
(Doc.lineSuffix Doc.greaterThan)
Doc.greaterThan);
else Doc.greaterThan);
]);
(if isSelfClosing then Doc.nil
else
Expand Down Expand Up @@ -4216,7 +4203,7 @@ and printJsxProps ~customLayout args cmtTbl :
{loc with loc_end = expr.pexp_loc.loc_end}
| _ -> expr.pexp_loc
in
let tailSingleLineCommentPresent = hasTailSingleLineComment cmtTbl loc in
let trailingCommentsPresent = hasTrailingComments cmtTbl loc in
let propDoc = printJsxProp ~customLayout lastProp cmtTbl in
let formattedProps =
Doc.concat
Expand All @@ -4228,8 +4215,8 @@ and printJsxProps ~customLayout args cmtTbl :
Doc.group
(Doc.join ~sep:Doc.line (propDoc :: props |> List.rev));
]);
(* print > on new line if last comment is single line comment *)
(match (isSelfClosing children, tailSingleLineCommentPresent) with
(* print > on new line if the last prop has trailing comments *)
(match (isSelfClosing children, trailingCommentsPresent) with
(* we always put /> on a new line when a self-closing tag breaks *)
| true, _ -> Doc.line
| false, true -> Doc.softLine
Expand Down
6 changes: 4 additions & 2 deletions tests/printer/comments/expected/jsx.res.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ module Cite = {

<A
value=""
/* comment */>
/* comment */
>
<B />
</A>

Expand All @@ -44,7 +45,8 @@ module Cite = {
</A>

<A
/* comment */>
/* comment */
>
<B />
</A>

Expand Down