Skip to content

Commit 7f8bf01

Browse files
committed
Handle some edge cases
1 parent b863888 commit 7f8bf01

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

compiler/syntax/src/res_comments_table.ml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,13 @@ and walk_expression expr t comments =
14721472
in
14731473
let xs = exprs |> List.map (fun e -> Expression e) in
14741474
walk_list xs t rest
1475-
| Pexp_jsx_unary_element {jsx_unary_element_props = props} ->
1475+
| Pexp_jsx_unary_element
1476+
{jsx_unary_element_tag_name = tag; jsx_unary_element_props = props} ->
1477+
(* handles the comments at the tag *)
1478+
let _, _, trailing = partition_by_loc comments tag.loc in
1479+
let after_expr, _ = partition_adjacent_trailing tag.loc trailing in
1480+
attach t.trailing tag.loc after_expr;
1481+
(* handles the comments for the actual props *)
14761482
List.iter
14771483
(fun prop ->
14781484
match prop with

compiler/syntax/src/res_printer.ml

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,6 +4312,7 @@ and print_pexp_apply ~state expr cmt_tbl =
43124312
and print_jsx_unary_tag ~state tag_name props cmt_tbl =
43134313
let name = print_jsx_name tag_name in
43144314
let formatted_props = print_jsx_props ~state props cmt_tbl in
4315+
let has_no_props = List.is_empty props in
43154316
let props_doc =
43164317
match props with
43174318
| [] -> Doc.nil
@@ -4320,17 +4321,32 @@ and print_jsx_unary_tag ~state tag_name props cmt_tbl =
43204321
(Doc.concat
43214322
[Doc.line; Doc.group (Doc.join formatted_props ~sep:Doc.line)])
43224323
in
4324+
let tag_has_trailing_comment = has_trailing_comments cmt_tbl tag_name.loc in
4325+
let opening_tag =
4326+
print_comments
4327+
(Doc.concat [Doc.less_than; name])
4328+
cmt_tbl tag_name.Asttypes.loc
4329+
in
4330+
let opening_tag =
4331+
if tag_has_trailing_comment && not has_no_props then Doc.indent opening_tag
4332+
else opening_tag
4333+
in
43234334
let closing_tag =
43244335
if Doc.will_break props_doc then Doc.concat [Doc.soft_line; Doc.text "/>"]
4325-
else Doc.concat [Doc.space; Doc.text "/>"]
4336+
else
4337+
Doc.concat
4338+
[
4339+
(if tag_has_trailing_comment && has_no_props then Doc.nil else Doc.line);
4340+
Doc.text "/>";
4341+
]
43264342
in
43274343
Doc.group
43284344
(Doc.concat
43294345
[
4330-
print_comments
4331-
(Doc.concat [Doc.less_than; name])
4332-
cmt_tbl tag_name.Asttypes.loc;
4346+
opening_tag;
43334347
props_doc;
4348+
(if tag_has_trailing_comment && has_no_props then Doc.hard_line
4349+
else Doc.nil);
43344350
closing_tag;
43354351
])
43364352

0 commit comments

Comments
 (0)