Skip to content

Commit 5e060ea

Browse files
committed
Rough print_jsx_container_tag
1 parent 02e9fc9 commit 5e060ea

File tree

1 file changed

+70
-3
lines changed

1 file changed

+70
-3
lines changed

compiler/syntax/src/res_printer.ml

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2799,7 +2799,13 @@ and print_expression ~state (e : Parsetree.expression) cmt_tbl =
27992799
{jsx_unary_element_tag_name = tag_name; jsx_unary_element_props = props}
28002800
->
28012801
print_jsx_unary_tag ~state tag_name props cmt_tbl
2802-
| Pexp_jsx_container_element _ -> failwith "Pexp_jsx_container_element 4"
2802+
| Pexp_jsx_container_element
2803+
{
2804+
jsx_container_element_tag_name_start = tag_name;
2805+
jsx_container_element_props = props;
2806+
jsx_container_element_children = children;
2807+
} ->
2808+
print_jsx_container_tag ~state tag_name props children cmt_tbl
28032809
| Pexp_construct ({txt = Longident.Lident "()"}, _) -> Doc.text "()"
28042810
| Pexp_construct ({txt = Longident.Lident "[]"}, _) ->
28052811
Doc.concat
@@ -4361,7 +4367,7 @@ and print_jsx_expression ~state lident args cmt_tbl =
43614367
Doc.line;
43624368
(match children with
43634369
| Some children_expression ->
4364-
print_jsx_children ~state children_expression ~sep:line_sep
4370+
print_jsx_children_old ~state children_expression ~sep:line_sep
43654371
cmt_tbl
43664372
| None -> Doc.nil);
43674373
]);
@@ -4438,6 +4444,54 @@ and print_jsx_unary_tag ~state tag_name props cmt_tbl =
44384444
Doc.nil;
44394445
])
44404446

4447+
and print_jsx_container_tag ~state tag_name props
4448+
(children : Parsetree.jsx_children) cmt_tbl =
4449+
let name = print_jsx_name tag_name in
4450+
let formatted_props = print_jsx_props ~state props cmt_tbl in
4451+
(* <div className="test" /> *)
4452+
let has_children =
4453+
match children with
4454+
| JSXChildrenSpreading _ | JSXChildrenItems (_ :: _) -> true
4455+
| JSXChildrenItems [] -> false
4456+
in
4457+
let line_sep = Doc.line in
4458+
let print_children children =
4459+
print_jsx_children ~sep:line_sep ~state children cmt_tbl
4460+
|> Doc.group |> Doc.indent
4461+
in
4462+
4463+
Doc.group
4464+
(Doc.concat
4465+
[
4466+
Doc.group
4467+
(Doc.concat
4468+
[
4469+
print_comments
4470+
(Doc.concat [Doc.less_than; name])
4471+
cmt_tbl tag_name.Asttypes.loc;
4472+
Doc.space;
4473+
(* todo: might not be needed if no props?*)
4474+
Doc.join formatted_props ~sep:Doc.space;
4475+
(* if tag A has trailing comments then put > on the next line
4476+
<A
4477+
// comments
4478+
>
4479+
</A>
4480+
*)
4481+
(if has_trailing_comments cmt_tbl tag_name.Asttypes.loc then
4482+
Doc.concat [Doc.soft_line; Doc.greater_than]
4483+
else Doc.greater_than);
4484+
]);
4485+
Doc.concat
4486+
[
4487+
(if has_children then Doc.line else Doc.nil);
4488+
(if has_children then print_children children else Doc.nil);
4489+
Doc.text "</";
4490+
name;
4491+
Doc.greater_than;
4492+
];
4493+
])
4494+
44414495
and print_jsx_fragment ~state (opening_greater_than : Lexing.position)
44424496
(children : Parsetree.expression list)
44434497
(closing_lesser_than : Lexing.position) (fragment_loc : Warnings.loc)
@@ -4498,7 +4552,7 @@ and print_jsx_child ~state (expr : Parsetree.expression) cmt_tbl =
44984552
| Braced braces_loc ->
44994553
print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc
45004554

4501-
and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep
4555+
and print_jsx_children_old ~state (children_expr : Parsetree.expression) ~sep
45024556
cmt_tbl =
45034557
match children_expr.pexp_desc with
45044558
| Pexp_construct ({txt = Longident.Lident "::"}, _) ->
@@ -4569,6 +4623,19 @@ and print_jsx_children ~state (children_expr : Parsetree.expression) ~sep
45694623
| Nothing -> expr_doc);
45704624
]
45714625

4626+
and print_jsx_children ~state (children_expr : Parsetree.jsx_children) ~sep
4627+
cmt_tbl =
4628+
let open Parsetree in
4629+
match children_expr with
4630+
| JSXChildrenSpreading child ->
4631+
Doc.concat
4632+
[Doc.dotdotdot; print_expression_with_comments ~state child cmt_tbl]
4633+
| JSXChildrenItems children ->
4634+
children
4635+
|> List.map (fun child ->
4636+
print_expression_with_comments ~state child cmt_tbl)
4637+
|> Doc.join ~sep
4638+
45724639
and print_jsx_props_old ~state args cmt_tbl :
45734640
Doc.t * Parsetree.expression option =
45744641
(* This function was introduced because we have different formatting behavior for self-closing tags and other tags

0 commit comments

Comments
 (0)