@@ -1075,7 +1075,30 @@ and print_jsx cxt ~(level : int) f (fnName : string) (tag : J.expression)
1075
1075
1076
1076
(* TODO: clean up the code , a lot of code is duplicated *)
1077
1077
and print_jsx_prop_spreading cxt ~level f fnName tag props =
1078
- (* TODO: the children as somewhere present in the props Seq *)
1078
+ (* The spreading expression is going to look something like:
1079
+ (newrecord.type = "text", newrecord.tabIndex = 0, newrecord.children = 5, newrecord)
1080
+ Where there are some assignments to the props object and then the props object is returned.
1081
+ We want to extract the assignments and turn them into props.
1082
+ And so capture the object we need to spread.
1083
+ *)
1084
+ let fields, spread =
1085
+ let rec visit acc e =
1086
+ match e.J. expression_desc with
1087
+ | J. Seq
1088
+ ( {
1089
+ J. expression_desc =
1090
+ J. Bin
1091
+ ( Js_op. Eq ,
1092
+ {J. expression_desc = J. Static_index (_, name, _)},
1093
+ value );
1094
+ },
1095
+ rest ) ->
1096
+ visit ((name, value) :: acc) rest
1097
+ | _ -> (List. rev acc, e)
1098
+ in
1099
+ visit [] props
1100
+ in
1101
+
1079
1102
let print_tag () =
1080
1103
match tag.expression_desc with
1081
1104
| J. Str {txt} -> P. string f txt
@@ -1085,7 +1108,7 @@ and print_jsx_prop_spreading cxt ~level f fnName tag props =
1085
1108
let _ = expression ~level cxt f tag in
1086
1109
()
1087
1110
in
1088
- (* let children_opt =
1111
+ let children_opt =
1089
1112
List. find_map
1090
1113
(fun (n , e ) ->
1091
1114
if n = " children" then
@@ -1097,17 +1120,32 @@ and print_jsx_prop_spreading cxt ~level f fnName tag props =
1097
1120
else Some [e]
1098
1121
else None )
1099
1122
fields
1100
- in *)
1101
- let print_props () =
1102
- P. string f " {...(" ;
1103
- let _ = expression ~level: 0 cxt f props in
1104
- P. string f " )}"
1105
1123
in
1106
- (match None with
1124
+ let print_props fields =
1125
+ let props = List. filter (fun (n , _ ) -> n <> " children" ) fields in
1126
+ if List. length props > 0 then
1127
+ (List. iter (fun (n , x ) ->
1128
+ P. space f;
1129
+ P. string f n;
1130
+ P. string f " =" ;
1131
+ P. string f " {" ;
1132
+ let _ = expression ~level: 0 cxt f x in
1133
+ P. string f " }" ))
1134
+ props
1135
+ in
1136
+ let print_spreaded_props () =
1137
+ (* Spread the object first, as that is what happens in ReScript *)
1138
+ P. string f " {..." ;
1139
+ let _ = expression ~level: 0 cxt f spread in
1140
+ P. string f " } " ;
1141
+ (* Then print the rest of the props *)
1142
+ print_props fields
1143
+ in
1144
+ (match children_opt with
1107
1145
| None ->
1108
1146
P. string f " <" ;
1109
1147
print_tag () ;
1110
- print_props () ;
1148
+ print_spreaded_props () ;
1111
1149
P. string f " />"
1112
1150
| Some children ->
1113
1151
let child_is_jsx child =
@@ -1118,7 +1156,7 @@ and print_jsx_prop_spreading cxt ~level f fnName tag props =
1118
1156
1119
1157
P. string f " <" ;
1120
1158
print_tag () ;
1121
- print_props () ;
1159
+ print_spreaded_props () ;
1122
1160
P. string f " >" ;
1123
1161
1124
1162
let _ =
@@ -1128,6 +1166,8 @@ and print_jsx_prop_spreading cxt ~level f fnName tag props =
1128
1166
if not (child_is_jsx e) then P. string f " {" ;
1129
1167
let next = expression ~level acc f e in
1130
1168
if not (child_is_jsx e) then P. string f " }" ;
1169
+ (* Can we some indent this? *)
1170
+ P. newline f;
1131
1171
next)
1132
1172
cxt
1133
1173
in
0 commit comments