Skip to content

Commit 4e0951c

Browse files
committed
Refactor
1 parent 7f8bf01 commit 4e0951c

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

compiler/syntax/src/res_printer.ml

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4312,42 +4312,44 @@ 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
4315+
let tag_has_trailing_comment = has_trailing_comments cmt_tbl tag_name.loc in
4316+
let tag_has_no_props = List.is_empty props in
43164317
let props_doc =
4317-
match props with
4318-
| [] -> Doc.nil
4319-
| _ ->
4318+
if tag_has_no_props then Doc.nil
4319+
else
43204320
Doc.indent
43214321
(Doc.concat
43224322
[Doc.line; Doc.group (Doc.join formatted_props ~sep:Doc.line)])
43234323
in
4324-
let tag_has_trailing_comment = has_trailing_comments cmt_tbl tag_name.loc in
43254324
let opening_tag =
43264325
print_comments
43274326
(Doc.concat [Doc.less_than; name])
43284327
cmt_tbl tag_name.Asttypes.loc
43294328
in
4330-
let opening_tag =
4331-
if tag_has_trailing_comment && not has_no_props then Doc.indent opening_tag
4329+
let opening_tag_doc =
4330+
if tag_has_trailing_comment && not tag_has_no_props then
4331+
Doc.indent opening_tag
43324332
else opening_tag
43334333
in
4334-
let closing_tag =
4335-
if Doc.will_break props_doc then Doc.concat [Doc.soft_line; 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-
]
4334+
let closing_tag_doc =
4335+
let sep =
4336+
match
4337+
(Doc.will_break props_doc, tag_has_trailing_comment, tag_has_no_props)
4338+
with
4339+
| true, _, _ -> Doc.soft_line
4340+
| false, true, true -> Doc.nil
4341+
| false, _, _ -> Doc.line
4342+
in
4343+
Doc.concat [sep; Doc.text "/>"]
43424344
in
43434345
Doc.group
43444346
(Doc.concat
43454347
[
4346-
opening_tag;
4348+
opening_tag_doc;
43474349
props_doc;
4348-
(if tag_has_trailing_comment && has_no_props then Doc.hard_line
4350+
(if tag_has_trailing_comment && tag_has_no_props then Doc.hard_line
43494351
else Doc.nil);
4350-
closing_tag;
4352+
closing_tag_doc;
43514353
])
43524354

43534355
and print_jsx_container_tag ~state tag_name props

0 commit comments

Comments
 (0)