Skip to content

Commit c0fef48

Browse files
committed
Improve consistency of prop printing
1 parent 331765c commit c0fef48

File tree

2 files changed

+62
-49
lines changed

2 files changed

+62
-49
lines changed

compiler/core/js_dump.ml

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,53 +1129,58 @@ and print_jsx cxt ?(spread_props : J.expression option)
11291129
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 *)
1132-
let cxt =
1133-
match key with
1134-
| None -> cxt
1135-
| Some k ->
1136-
P.string f " key={";
1137-
let cxt_k = expression ~level:0 cxt f k in
1138-
P.string f "} ";
1139-
cxt_k
1132+
let print_key key cxt =
1133+
P.string f "key={";
1134+
let cxt_k = expression ~level:0 cxt f key in
1135+
P.string f "} ";
1136+
cxt_k
11401137
in
11411138

1142-
let cxt =
1143-
match spread_props with
1144-
| None -> cxt
1145-
| Some spread ->
1146-
P.string f " {...";
1147-
let cxt = expression ~level:0 cxt f spread in
1148-
P.string f "} ";
1149-
cxt
1139+
let print_spread_props spread cxt =
1140+
P.string f "{...";
1141+
let cxt = expression ~level:0 cxt f spread in
1142+
P.string f "} ";
1143+
cxt
1144+
in
1145+
1146+
let print_prop n x ctx =
1147+
let prop_name = Js_dump_property.property_key_string n in
1148+
P.string f prop_name;
1149+
P.string f "=";
1150+
P.string f "{";
1151+
let next_cxt = expression ~level:0 ctx f x in
1152+
P.string f "}";
1153+
next_cxt
1154+
in
1155+
let printable_props =
1156+
(match key with
1157+
| None -> []
1158+
| Some k -> [print_key k])
1159+
@ (match spread_props with
1160+
| None -> []
1161+
| Some spread -> [print_spread_props spread])
1162+
@ List.map (fun (n, x) -> print_prop n x) props
11501163
in
1151-
if List.length props = 0 then cxt
1164+
if List.length printable_props = 0 then (
1165+
match children_opt with
1166+
| Some _ -> cxt
1167+
| None ->
1168+
(* Put a space the tag name and /> *)
1169+
P.space f;
1170+
cxt)
11521171
else
11531172
P.group f 1 (fun () ->
11541173
P.newline f;
1155-
let rec process_remaining_props acc_cxt props_to_process =
1156-
match props_to_process with
1174+
let rec process_remaining_props acc_cxt printable_props =
1175+
match printable_props with
11571176
| [] -> 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 "}";
1177+
| print_prop :: [] -> print_prop acc_cxt
1178+
| print_prop :: tail ->
1179+
let next_cxt = print_prop acc_cxt in
11751180
P.newline f;
1176-
process_remaining_props cxt_after_current_prop_value tail
1181+
process_remaining_props next_cxt tail
11771182
in
1178-
process_remaining_props cxt props)
1183+
process_remaining_props cxt printable_props)
11791184
in
11801185

11811186
let print_one_child expr_level_for_child current_cxt_for_child f_format

tests/tests/src/preserve_jsx_test.mjs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let React = {};
88
let ReactDOM = {};
99

1010
function Preserve_jsx_test$Icon(props) {
11-
return <strong/>;
11+
return <strong />;
1212
}
1313

1414
let Icon = {
@@ -25,11 +25,11 @@ let _multiple_element_children = <div>
2525
<h1>
2626
{"Hello, world!"}
2727
</h1>
28-
<Preserve_jsx_test$Icon/>
28+
<Preserve_jsx_test$Icon />
2929
</div>;
3030

3131
let _single_element_fragment = <>
32-
{Primitive_option.some(<input/>)}
32+
{Primitive_option.some(<input />)}
3333
</>;
3434

3535
let _multiple_element_fragment = <>
@@ -60,13 +60,15 @@ let baseProps = {
6060

6161
let newrecord = {...baseProps};
6262

63-
let _unary_element_with_spread_props = <input {...newrecord}
63+
let _unary_element_with_spread_props = <input
64+
{...newrecord}
6465
type={"text"}
6566
/>;
6667

6768
let newrecord$1 = {...baseProps};
6869

69-
let _container_with_spread_props = <div {...newrecord$1}
70+
let _container_with_spread_props = <div
71+
{...newrecord$1}
7072
title={"barry"}
7173
className={"barry"}
7274
>
@@ -94,7 +96,8 @@ let _container_with_spread_children = <div
9496

9597
let newrecord$2 = {...baseProps};
9698

97-
let _container_with_spread_props_and_children = <div {...newrecord$2}
99+
let _container_with_spread_props_and_children = <div
100+
{...newrecord$2}
98101
title={"barry"}
99102
className={"barry"}
100103
>
@@ -103,13 +106,17 @@ let _container_with_spread_props_and_children = <div {...newrecord$2}
103106

104107
let newrecord$3 = {...baseProps};
105108

106-
let _unary_element_with_spread_props_keyed = <input key={"barry-key"} {...newrecord$3}
109+
let _unary_element_with_spread_props_keyed = <input
110+
key={"barry-key"}
111+
{...newrecord$3}
107112
type={"text"}
108113
/>;
109114

110115
let newrecord$4 = {...baseProps};
111116

112-
let _container_with_spread_props_keyed = <div key={"barry-key"} {...newrecord$4}
117+
let _container_with_spread_props_keyed = <div
118+
key={"barry-key"}
119+
{...newrecord$4}
113120
title={"barry"}
114121
className={"barry"}
115122
>
@@ -119,7 +126,8 @@ let _container_with_spread_props_keyed = <div key={"barry-key"} {...newrecord$4
119126
/>
120127
</div>;
121128

122-
let _unary_element_with_only_spread_props = <input {...baseProps} />;
129+
let _unary_element_with_only_spread_props = <input
130+
{...baseProps} />;
123131

124132
function QueryClientProvider(props) { return props.children }
125133
;
@@ -137,8 +145,8 @@ let B = {
137145
};
138146

139147
let _external_component_with_children = <QueryClientProvider>
140-
<strong/>
141-
<Preserve_jsx_test$B/>
148+
<strong />
149+
<Preserve_jsx_test$B />
142150
</QueryClientProvider>;
143151

144152
function Preserve_jsx_test$MyWeirdComponent(props) {

0 commit comments

Comments
 (0)