Skip to content

Commit f75f4f3

Browse files
committed
Keep empty line in JSX
1 parent c2ad39e commit f75f4f3

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

compiler/syntax/src/res_printer.ml

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4446,35 +4446,46 @@ and print_jsx_fragment ~state expr cmt_tbl =
44464446
and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep
44474447
cmt_tbl =
44484448
match children_expr.pexp_desc with
4449-
| Pexp_construct ({txt = Longident.Lident "::"}, _) ->
4449+
| Pexp_construct ({loc; txt = Longident.Lident "::"}, _) ->
44504450
let children, _ = ParsetreeViewer.collect_list_expressions children_expr in
4451-
Doc.group
4452-
(Doc.join ~sep
4453-
(List.map
4454-
(fun (expr : Parsetree.expression) ->
4455-
let leading_line_comment_present =
4456-
has_leading_line_comment cmt_tbl expr.pexp_loc
4457-
in
4458-
let expr_doc =
4459-
print_expression_with_comments ~state expr cmt_tbl
4460-
in
4461-
let add_parens_or_braces expr_doc =
4462-
(* {(20: int)} make sure that we also protect the expression inside *)
4463-
let inner_doc =
4464-
if Parens.braced_expr expr then add_parens expr_doc
4465-
else expr_doc
4466-
in
4467-
if leading_line_comment_present then add_braces inner_doc
4468-
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4469-
in
4470-
match Parens.jsx_child_expr expr with
4471-
| Nothing -> expr_doc
4472-
| Parenthesized -> add_parens_or_braces expr_doc
4473-
| Braced braces_loc ->
4474-
print_comments
4475-
(add_parens_or_braces expr_doc)
4476-
cmt_tbl braces_loc)
4477-
children))
4451+
let print_expr (expr : Parsetree.expression) =
4452+
let leading_line_comment_present =
4453+
has_leading_line_comment cmt_tbl expr.pexp_loc
4454+
in
4455+
let expr_doc = print_expression_with_comments ~state expr cmt_tbl in
4456+
let add_parens_or_braces expr_doc =
4457+
(* {(20: int)} make sure that we also protect the expression inside *)
4458+
let inner_doc =
4459+
if Parens.braced_expr expr then add_parens expr_doc else expr_doc
4460+
in
4461+
if leading_line_comment_present then add_braces inner_doc
4462+
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4463+
in
4464+
match Parens.jsx_child_expr expr with
4465+
| Nothing -> expr_doc
4466+
| Parenthesized -> add_parens_or_braces expr_doc
4467+
| Braced braces_loc ->
4468+
print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc
4469+
in
4470+
let rec loop prev acc exprs =
4471+
match exprs with
4472+
| [] -> List.rev acc
4473+
| ({Parsetree.pexp_loc = current_loc} as expr) :: tails ->
4474+
let docs =
4475+
if current_loc.loc_start.pos_lnum - prev.Warnings.loc_end.pos_lnum > 1
4476+
then
4477+
let expr = print_expr expr in
4478+
let acc = Doc.concat [Doc.hard_line; expr] :: acc in
4479+
acc
4480+
else
4481+
let expr = print_expr expr in
4482+
let acc = expr :: acc in
4483+
acc
4484+
in
4485+
loop current_loc docs tails
4486+
in
4487+
let docs = loop loc [] children in
4488+
Doc.group (Doc.join ~sep docs)
44784489
| _ ->
44794490
let leading_line_comment_present =
44804491
has_leading_line_comment cmt_tbl children_expr.pexp_loc

0 commit comments

Comments
 (0)