Skip to content

Commit 8f9e0f1

Browse files
authored
Further improve JSX preserve output (rescript-lang#7445)
1 parent a87e656 commit 8f9e0f1

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

compiler/core/js_dump.ml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,13 +1127,23 @@ and print_jsx cxt ?(spread_props : J.expression option)
11271127
fields
11281128
in
11291129
let print_props cxt props =
1130+
let print_prop_value (x : J.expression) ctx =
1131+
let needs_braces =
1132+
match x.expression_desc with
1133+
| Str _ | Optional_block ({expression_desc = Str _}, _) -> false
1134+
| _ -> true
1135+
in
1136+
if needs_braces then P.string f "{";
1137+
let next_cxt = expression ~level:0 ctx f x in
1138+
if needs_braces then P.string f "}";
1139+
next_cxt
1140+
in
1141+
11301142
(* If a key is present, should be printed before the spread props,
11311143
This is to ensure tools like ESBuild use the automatic JSX runtime *)
11321144
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
1145+
P.string f "key=";
1146+
print_prop_value key cxt
11371147
in
11381148

11391149
let print_spread_props spread cxt =
@@ -1147,10 +1157,7 @@ and print_jsx cxt ?(spread_props : J.expression option)
11471157
let prop_name = Js_dump_property.property_key_string n in
11481158
P.string f prop_name;
11491159
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
1160+
print_prop_value x ctx
11541161
in
11551162
let printable_props =
11561163
(match key with

tests/tests/src/preserve_jsx_test.mjs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ let _single_element_fragment = <>
3434

3535
let _multiple_element_fragment = <>
3636
<input
37-
type={"text"}
37+
type="text"
3838
/>
3939
<input
40-
type={"number"}
40+
type="number"
4141
/>
4242
</>;
4343

4444
let _unary_element_with_props = <input
45-
className={"foo"}
46-
type={"text"}
45+
className="foo"
46+
type="text"
4747
/>;
4848

4949
let _container_element_with_props_and_children = <div
50-
className={"foo"}
51-
title={"foo"}
50+
className="foo"
51+
title="foo"
5252
>
5353
{"Hello, world!"}
5454
</div>;
@@ -62,19 +62,19 @@ let newrecord = {...baseProps};
6262

6363
let _unary_element_with_spread_props = <input
6464
{...newrecord}
65-
type={"text"}
65+
type="text"
6666
/>;
6767

6868
let newrecord$1 = {...baseProps};
6969

7070
let _container_with_spread_props = <div
7171
{...newrecord$1}
72-
title={"barry"}
73-
className={"barry"}
72+
title="barry"
73+
className="barry"
7474
>
7575
{"Hello, world!"}
7676
<input
77-
type={"text"}
77+
type="text"
7878
/>
7979
</div>;
8080

@@ -88,8 +88,8 @@ let baseChildren = [
8888
];
8989

9090
let _container_with_spread_children = <div
91-
className={"barry"}
92-
title={"barry"}
91+
className="barry"
92+
title="barry"
9393
>
9494
{baseChildren}
9595
</div>;
@@ -98,31 +98,31 @@ let newrecord$2 = {...baseProps};
9898

9999
let _container_with_spread_props_and_children = <div
100100
{...newrecord$2}
101-
title={"barry"}
102-
className={"barry"}
101+
title="barry"
102+
className="barry"
103103
>
104104
{baseChildren}
105105
</div>;
106106

107107
let newrecord$3 = {...baseProps};
108108

109109
let _unary_element_with_spread_props_keyed = <input
110-
key={"barry-key"}
110+
key="barry-key"
111111
{...newrecord$3}
112-
type={"text"}
112+
type="text"
113113
/>;
114114

115115
let newrecord$4 = {...baseProps};
116116

117117
let _container_with_spread_props_keyed = <div
118-
key={"barry-key"}
118+
key="barry-key"
119119
{...newrecord$4}
120-
title={"barry"}
121-
className={"barry"}
120+
title="barry"
121+
className="barry"
122122
>
123123
{"Hello, world!"}
124124
<input
125-
type={"text"}
125+
type="text"
126126
/>
127127
</div>;
128128

@@ -161,29 +161,29 @@ let MyWeirdComponent = {
161161
};
162162

163163
let _escaped_jsx_prop = <Preserve_jsx_test$MyWeirdComponent
164-
MyWeirdProp={"bar"}
164+
MyWeirdProp="bar"
165165
/>;
166166

167167
let _large_component = <div
168-
className={"bar"}
168+
className="bar"
169169
tabIndex={1}
170-
title={"foo"}
170+
title="foo"
171171
onClick={param => {}}
172172
onMouseDown={param => {}}
173173
>
174174
<p
175-
className={"bar"}
175+
className="bar"
176176
tabIndex={1}
177-
title={"foo"}
177+
title="foo"
178178
onClick={param => {}}
179179
onMouseDown={param => {}}
180180
>
181181
{"Hello, world!"}
182182
</p>
183183
<strong
184-
className={"bar"}
184+
className="bar"
185185
tabIndex={1}
186-
title={"foo"}
186+
title="foo"
187187
onClick={param => {}}
188188
onMouseDown={param => {}}
189189
>

0 commit comments

Comments
 (0)