Skip to content

Commit 362a1da

Browse files
authored
Escape prop name in preserve jsx (#7429)
1 parent 54d8015 commit 362a1da

File tree

5 files changed

+37
-4
lines changed

5 files changed

+37
-4
lines changed

compiler/core/js_dump.ml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1124,7 +1124,9 @@ and print_jsx cxt ?(spread_props : J.expression option)
11241124
else
11251125
(List.fold_left (fun acc (n, x) ->
11261126
P.space f;
1127-
P.string f n;
1127+
let prop_name = Js_dump_property.property_key_string n in
1128+
1129+
P.string f prop_name;
11281130
P.string f "=";
11291131
P.string f "{";
11301132
let next = expression ~level:0 acc f x in

compiler/core/js_dump_property.ml

+5-3
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,11 @@ let property_access f s =
8181
| _ -> Js_dump_string.pp_string f s
8282
| exception _ -> Js_dump_string.pp_string f s)
8383

84+
let property_key_string (s : string) : string =
85+
let s = Ext_ident.unwrap_uppercase_exotic s in
86+
if obj_property_no_need_quot s then s else Js_dump_string.escape_to_string s
87+
8488
let property_key (s : J.property_name) : string =
8589
match s with
86-
| Lit s ->
87-
let s = Ext_ident.unwrap_uppercase_exotic s in
88-
if obj_property_no_need_quot s then s else Js_dump_string.escape_to_string s
90+
| Lit s -> property_key_string s
8991
| Symbol_name -> {|[Symbol.for("name")]|}

compiler/core/js_dump_property.mli

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@
2525
val property_access : Ext_pp.t -> string -> unit
2626

2727
val property_key : J.property_name -> string
28+
29+
val property_key_string : string -> string

tests/tests/src/preserve_jsx_test.mjs

+15
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,19 @@ let _external_component_with_children = <QueryClientProvider>
111111
<Preserve_jsx_test$B/>
112112
</QueryClientProvider>;
113113

114+
function make(props) {
115+
return <p>
116+
{"foo"}
117+
{props["\\\"MyWeirdProp\""]}
118+
</p>;
119+
}
120+
121+
let MyWeirdComponent = {
122+
make: make
123+
};
124+
125+
let _escaped_jsx_prop = <make MyWeirdProp={"bar"}/>;
126+
114127
export {
115128
React,
116129
ReactDOM,
@@ -133,5 +146,7 @@ export {
133146
A,
134147
B,
135148
_external_component_with_children,
149+
MyWeirdComponent,
150+
_escaped_jsx_prop,
136151
}
137152
/* _single_element_child Not a pure module */

tests/tests/src/preserve_jsx_test.res

+12
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,15 @@ let _external_component_with_children =
155155
<strong />
156156
<B />
157157
</A>
158+
159+
module MyWeirdComponent = {
160+
type props = {\"MyWeirdProp": string}
161+
162+
let make = props =>
163+
<p>
164+
{React.string("foo")}
165+
{React.string(props.\"MyWeirdProp")}
166+
</p>
167+
}
168+
169+
let _escaped_jsx_prop = <MyWeirdComponent \"MyWeirdProp"="bar" />

0 commit comments

Comments
 (0)