Skip to content

Commit aec0d8e

Browse files
committed
Restore braces in props and children
1 parent 251244b commit aec0d8e

File tree

1 file changed

+43
-13
lines changed

1 file changed

+43
-13
lines changed

compiler/syntax/src/res_printer.ml

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,16 +4628,30 @@ and print_jsx_children_old ~state (children_expr : Parsetree.expression) ~sep
46284628
and print_jsx_children ~state (children_expr : Parsetree.jsx_children) ~sep
46294629
cmt_tbl =
46304630
let open Parsetree in
4631+
let print_expr (expr : Parsetree.expression) =
4632+
let leading_line_comment_present =
4633+
has_leading_line_comment cmt_tbl expr.pexp_loc
4634+
in
4635+
let expr_doc = print_expression_with_comments ~state expr cmt_tbl in
4636+
let add_parens_or_braces expr_doc =
4637+
(* {(20: int)} make sure that we also protect the expression inside *)
4638+
let inner_doc =
4639+
if Parens.braced_expr expr then add_parens expr_doc else expr_doc
4640+
in
4641+
if leading_line_comment_present then add_braces inner_doc
4642+
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4643+
in
4644+
match Parens.jsx_child_expr expr with
4645+
| Nothing -> expr_doc
4646+
| Parenthesized -> add_parens_or_braces expr_doc
4647+
| Braced braces_loc ->
4648+
print_comments (add_parens_or_braces expr_doc) cmt_tbl braces_loc
4649+
in
46314650
match children_expr with
4632-
| JSXChildrenSpreading child ->
4633-
Doc.concat
4634-
[Doc.dotdotdot; print_expression_with_comments ~state child cmt_tbl]
4651+
| JSXChildrenSpreading child -> Doc.concat [Doc.dotdotdot; print_expr child]
46354652
| JSXChildrenItems [] -> Doc.nil
46364653
| JSXChildrenItems children ->
4637-
children
4638-
|> List.map (fun child ->
4639-
print_expression_with_comments ~state child cmt_tbl)
4640-
|> Doc.join ~sep
4654+
children |> List.map print_expr |> Doc.join ~sep
46414655

46424656
and print_jsx_props_old ~state args cmt_tbl :
46434657
Doc.t * Parsetree.expression option =
@@ -4780,15 +4794,31 @@ and print_jsx_prop ~state prop cmt_tbl =
47804794
if is_optional then Doc.concat [Doc.question; Doc.text name.txt]
47814795
else Doc.text name.txt
47824796
| JSXPropValue (name, is_optional, value) ->
4783-
let value =
4784-
if is_optional then
4785-
[Doc.question; print_expression_with_comments ~state value cmt_tbl]
4786-
else [print_expression_with_comments ~state value cmt_tbl]
4797+
let value_doc =
4798+
let v =
4799+
Doc.concat
4800+
[
4801+
(if is_optional then Doc.question else Doc.nil);
4802+
print_expression_with_comments ~state value cmt_tbl;
4803+
]
4804+
in
4805+
match Parens.jsx_prop_expr value with
4806+
| Parenthesized | Braced _ ->
4807+
let inner_doc = if Parens.braced_expr value then add_parens v else v in
4808+
if has_leading_line_comment cmt_tbl value.pexp_loc then
4809+
add_braces inner_doc
4810+
else Doc.concat [Doc.lbrace; inner_doc; Doc.rbrace]
4811+
| _ -> v
47874812
in
4788-
Doc.concat [Doc.text name.txt; Doc.equal; Doc.group (Doc.concat value)]
4813+
Doc.concat [Doc.text name.txt; Doc.equal; Doc.group value_doc]
47894814
| JSXPropSpreading (_, value) ->
47904815
Doc.concat
4791-
[Doc.dotdotdot; print_expression_with_comments ~state value cmt_tbl]
4816+
[
4817+
Doc.lbrace;
4818+
Doc.dotdotdot;
4819+
print_expression_with_comments ~state value cmt_tbl;
4820+
Doc.rbrace;
4821+
]
47924822

47934823
and print_jsx_props ~state props cmt_tbl : Doc.t list =
47944824
props |> List.map (fun prop -> print_jsx_prop ~state prop cmt_tbl)

0 commit comments

Comments
 (0)