@@ -1126,19 +1126,19 @@ and print_jsx cxt ?(spread_props : J.expression option)
11261126 else None )
11271127 fields
11281128 in
1129- let print_props cxt =
1129+ let print_props cxt props =
11301130 (* If a key is present, should be printed before the spread props,
11311131 This is to ensure tools like ESBuild use the automatic JSX runtime *)
11321132 let cxt =
11331133 match key with
11341134 | None -> cxt
1135- | Some key ->
1135+ | Some k ->
11361136 P. string f " key={" ;
1137- let cxt = expression ~level: 0 cxt f key in
1137+ let cxt_k = expression ~level: 0 cxt f k in
11381138 P. string f " } " ;
1139- cxt
1139+ cxt_k
11401140 in
1141- let props = List. filter ( fun ( n , _ ) -> n <> " children " ) fields in
1141+
11421142 let cxt =
11431143 match spread_props with
11441144 | None -> cxt
@@ -1150,17 +1150,32 @@ and print_jsx cxt ?(spread_props : J.expression option)
11501150 in
11511151 if List. length props = 0 then cxt
11521152 else
1153- (List. fold_left (fun acc (n , x ) ->
1154- P. space f;
1155- let prop_name = Js_dump_property. property_key_string n in
1156-
1157- P. string f prop_name;
1158- P. string f " =" ;
1159- P. string f " {" ;
1160- let next = expression ~level: 0 acc f x in
1161- P. string f " }" ;
1162- next))
1163- cxt props
1153+ P. group f 1 (fun () ->
1154+ P. newline f;
1155+ let rec process_remaining_props acc_cxt props_to_process =
1156+ match props_to_process with
1157+ | [] -> acc_cxt
1158+ | (n , x ) :: [] ->
1159+ let prop_name = Js_dump_property. property_key_string n in
1160+ P. string f prop_name;
1161+ P. string f " =" ;
1162+ P. string f " {" ;
1163+ let next_cxt = expression ~level: 0 acc_cxt f x in
1164+ P. string f " }" ;
1165+ next_cxt
1166+ | (n , x ) :: tail ->
1167+ let prop_name = Js_dump_property. property_key_string n in
1168+ P. string f prop_name;
1169+ P. string f " =" ;
1170+ P. string f " {" ;
1171+ let cxt_after_current_prop_value =
1172+ expression ~level: 0 acc_cxt f x
1173+ in
1174+ P. string f " }" ;
1175+ P. newline f;
1176+ process_remaining_props cxt_after_current_prop_value tail
1177+ in
1178+ process_remaining_props cxt props)
11641179 in
11651180
11661181 let print_one_child expr_level_for_child current_cxt_for_child f_format
@@ -1179,15 +1194,26 @@ and print_jsx cxt ?(spread_props : J.expression option)
11791194 next_cxt
11801195 in
11811196
1197+ let props = List. filter (fun (n , _ ) -> n <> " children" ) fields in
1198+
1199+ (* Actual printing of JSX element starts here *)
1200+ P. string f " <" ;
1201+ let cxt = print_tag cxt in
1202+ let cxt = print_props cxt props in
1203+ (* print_props handles its own block and updates cxt *)
1204+
1205+ let has_multiple_props = List. length props > 0 in
1206+
11821207 match children_opt with
11831208 | None ->
1184- P. string f " < " ;
1185- let cxt = cxt |> print_tag |> print_props in
1209+ (* Self-closing tag *)
1210+ if has_multiple_props then P. newline f;
11861211 P. string f " />" ;
11871212 cxt
11881213 | Some children ->
1189- P. string f " <" ;
1190- let cxt = cxt |> print_tag |> print_props in
1214+ (* Tag with children *)
1215+ let has_children = List. length children > 0 in
1216+ if has_multiple_props || has_children then P. newline f;
11911217 P. string f " >" ;
11921218
11931219 let cxt_after_children =
@@ -1196,8 +1222,7 @@ and print_jsx cxt ?(spread_props : J.expression option)
11961222 let cxt = cxt_after_children in
11971223
11981224 P. newline f;
1199-
1200- (* Newline before the closing tag, uses parent's indent level *)
1225+ (* For closing </tag> *)
12011226 P. string f " </" ;
12021227 let cxt = print_tag cxt in
12031228 P. string f " >" ;
0 commit comments