Skip to content

Escape prop name in preserve jsx #7429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,9 @@ and print_jsx cxt ?(spread_props : J.expression option)
else
(List.fold_left (fun acc (n, x) ->
P.space f;
P.string f n;
let prop_name = Js_dump_property.property_key_string n in

P.string f prop_name;
P.string f "=";
P.string f "{";
let next = expression ~level:0 acc f x in
Expand Down
8 changes: 5 additions & 3 deletions compiler/core/js_dump_property.ml
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,11 @@ let property_access f s =
| _ -> Js_dump_string.pp_string f s
| exception _ -> Js_dump_string.pp_string f s)

let property_key_string (s : string) : string =
let s = Ext_ident.unwrap_uppercase_exotic s in
if obj_property_no_need_quot s then s else Js_dump_string.escape_to_string s

let property_key (s : J.property_name) : string =
match s with
| Lit s ->
let s = Ext_ident.unwrap_uppercase_exotic s in
if obj_property_no_need_quot s then s else Js_dump_string.escape_to_string s
| Lit s -> property_key_string s
| Symbol_name -> {|[Symbol.for("name")]|}
2 changes: 2 additions & 0 deletions compiler/core/js_dump_property.mli
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,5 @@
val property_access : Ext_pp.t -> string -> unit

val property_key : J.property_name -> string

val property_key_string : string -> string
15 changes: 15 additions & 0 deletions tests/tests/src/preserve_jsx_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,19 @@ let _external_component_with_children = <QueryClientProvider>
<Preserve_jsx_test$B/>
</QueryClientProvider>;

function make(props) {
return <p>
{"foo"}
{props["\\\"MyWeirdProp\""]}
</p>;
}

let MyWeirdComponent = {
make: make
};

let _escaped_jsx_prop = <make MyWeirdProp={"bar"}/>;

export {
React,
ReactDOM,
Expand All @@ -133,5 +146,7 @@ export {
A,
B,
_external_component_with_children,
MyWeirdComponent,
_escaped_jsx_prop,
}
/* _single_element_child Not a pure module */
12 changes: 12 additions & 0 deletions tests/tests/src/preserve_jsx_test.res
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,15 @@ let _external_component_with_children =
<strong />
<B />
</A>

module MyWeirdComponent = {
type props = {\"MyWeirdProp": string}

let make = props =>
<p>
{React.string("foo")}
{React.string(props.\"MyWeirdProp")}
</p>
}

let _escaped_jsx_prop = <MyWeirdComponent \"MyWeirdProp"="bar" />
Loading