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

Commit 694075f

Browse files
authored
put > on a new line if having trailing comments (#685)
1 parent b9f2f7b commit 694075f

File tree

2 files changed

+16
-27
lines changed

2 files changed

+16
-27
lines changed

src/res_printer.ml

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -113,26 +113,16 @@ let hasNestedJsxOrMoreThanOneChild expr =
113113
in
114114
loop false expr
115115

116-
let hasTailSingleLineComment tbl loc =
117-
let rec getLastElement elements =
118-
match elements with
119-
| [] -> None
120-
| [element] -> Some element
121-
| _ :: rest -> getLastElement rest
122-
in
123-
match Hashtbl.find_opt tbl.CommentTable.trailing loc with
124-
| None -> false
125-
| Some comments -> (
126-
let lastComment = getLastElement comments in
127-
match lastComment with
128-
| None -> false
129-
| Some comment -> Comment.isSingleLineComment comment)
130-
131116
let hasCommentsInside tbl loc =
132117
match Hashtbl.find_opt tbl.CommentTable.inside loc with
133118
| None -> false
134119
| _ -> true
135120

121+
let hasTrailingComments tbl loc =
122+
match Hashtbl.find_opt tbl.CommentTable.trailing loc with
123+
| None -> false
124+
| _ -> true
125+
136126
let printMultilineCommentContent txt =
137127
(* Turns
138128
* |* first line
@@ -4058,18 +4048,15 @@ and printJsxExpression ~customLayout lident args cmtTbl =
40584048
when isSelfClosing ->
40594049
Doc.text "/>"
40604050
| _ ->
4061-
(* if last trailing comment of tag is single line comment then put > on the next line
4051+
(* if tag A has trailing comments then put > on the next line
40624052
<A
4063-
// single line comment
4053+
// comments
40644054
>
40654055
</A>
40664056
*)
4067-
if hasTailSingleLineComment cmtTbl lident.Asttypes.loc then
4057+
if hasTrailingComments cmtTbl lident.Asttypes.loc then
40684058
Doc.concat [Doc.softLine; Doc.greaterThan]
4069-
else
4070-
Doc.ifBreaks
4071-
(Doc.lineSuffix Doc.greaterThan)
4072-
Doc.greaterThan);
4059+
else Doc.greaterThan);
40734060
]);
40744061
(if isSelfClosing then Doc.nil
40754062
else
@@ -4216,7 +4203,7 @@ and printJsxProps ~customLayout args cmtTbl :
42164203
{loc with loc_end = expr.pexp_loc.loc_end}
42174204
| _ -> expr.pexp_loc
42184205
in
4219-
let tailSingleLineCommentPresent = hasTailSingleLineComment cmtTbl loc in
4206+
let trailingCommentsPresent = hasTrailingComments cmtTbl loc in
42204207
let propDoc = printJsxProp ~customLayout lastProp cmtTbl in
42214208
let formattedProps =
42224209
Doc.concat
@@ -4228,8 +4215,8 @@ and printJsxProps ~customLayout args cmtTbl :
42284215
Doc.group
42294216
(Doc.join ~sep:Doc.line (propDoc :: props |> List.rev));
42304217
]);
4231-
(* print > on new line if last comment is single line comment *)
4232-
(match (isSelfClosing children, tailSingleLineCommentPresent) with
4218+
(* print > on new line if the last prop has trailing comments *)
4219+
(match (isSelfClosing children, trailingCommentsPresent) with
42334220
(* we always put /> on a new line when a self-closing tag breaks *)
42344221
| true, _ -> Doc.line
42354222
| false, true -> Doc.softLine

tests/printer/comments/expected/jsx.res.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ module Cite = {
3333

3434
<A
3535
value=""
36-
/* comment */>
36+
/* comment */
37+
>
3738
<B />
3839
</A>
3940

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

4647
<A
47-
/* comment */>
48+
/* comment */
49+
>
4850
<B />
4951
</A>
5052

0 commit comments

Comments
 (0)