Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit e3aaffd

Browse files
authored
change calling jsxKeyed bindings (#694)
1 parent eb059d4 commit e3aaffd

File tree

4 files changed

+43
-28
lines changed

4 files changed

+43
-28
lines changed

cli/reactjs_jsx_v4.ml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ let constantString ~loc str =
3636
(* {} empty record *)
3737
let emptyRecord ~loc = Exp.record ~loc [] None
3838

39+
let unitExpr ~loc = Exp.construct ~loc (Location.mkloc (Lident "()") loc) None
40+
3941
let safeTypeFromValue valueStr =
4042
let valueStr = getLabel valueStr in
4143
if valueStr = "" || (valueStr.[0] [@doesNotRaise]) <> '_' then valueStr
@@ -385,21 +387,23 @@ let transformUppercaseCall3 ~config modulePath mapper jsxExprLoc callExprLoc
385387
match config.mode with
386388
(* The new jsx transform *)
387389
| "automatic" ->
388-
let jsxExpr, key =
390+
let jsxExpr, keyAndUnit =
389391
match (!childrenArg, keyProp) with
390392
| None, key :: _ ->
391-
( Exp.ident {loc = Location.none; txt = Ldot (Lident "React", "jsx")},
392-
[key] )
393+
( Exp.ident
394+
{loc = Location.none; txt = Ldot (Lident "React", "jsxKeyed")},
395+
[key; (nolabel, unitExpr ~loc:Location.none)] )
393396
| None, [] ->
394397
(Exp.ident {loc = Location.none; txt = Ldot (Lident "React", "jsx")}, [])
395398
| Some _, key :: _ ->
396-
( Exp.ident {loc = Location.none; txt = Ldot (Lident "React", "jsxs")},
397-
[key] )
399+
( Exp.ident
400+
{loc = Location.none; txt = Ldot (Lident "React", "jsxsKeyed")},
401+
[key; (nolabel, unitExpr ~loc:Location.none)] )
398402
| Some _, [] ->
399403
( Exp.ident {loc = Location.none; txt = Ldot (Lident "React", "jsxs")},
400404
[] )
401405
in
402-
Exp.apply ~attrs jsxExpr (key @ [(nolabel, makeID); (nolabel, props)])
406+
Exp.apply ~attrs jsxExpr ([(nolabel, makeID); (nolabel, props)] @ keyAndUnit)
403407
| _ -> (
404408
match (!childrenArg, keyProp) with
405409
| None, key :: _ ->
@@ -488,23 +492,25 @@ let transformLowercaseCall3 ~config mapper jsxExprLoc callExprLoc attrs
488492
let keyProp =
489493
args |> List.filter (fun (arg_label, _) -> "key" = getLabel arg_label)
490494
in
491-
let jsxExpr, key =
495+
let jsxExpr, keyAndUnit =
492496
match (!childrenArg, keyProp) with
493497
| None, key :: _ ->
494-
( Exp.ident {loc = Location.none; txt = Ldot (Lident "ReactDOM", "jsx")},
495-
[key] )
498+
( Exp.ident
499+
{loc = Location.none; txt = Ldot (Lident "ReactDOM", "jsxKeyed")},
500+
[key; (nolabel, unitExpr ~loc:Location.none)] )
496501
| None, [] ->
497502
( Exp.ident {loc = Location.none; txt = Ldot (Lident "ReactDOM", "jsx")},
498503
[] )
499504
| Some _, key :: _ ->
500-
( Exp.ident {loc = Location.none; txt = Ldot (Lident "ReactDOM", "jsxs")},
501-
[key] )
505+
( Exp.ident
506+
{loc = Location.none; txt = Ldot (Lident "ReactDOM", "jsxsKeyed")},
507+
[key; (nolabel, unitExpr ~loc:Location.none)] )
502508
| Some _, [] ->
503509
( Exp.ident {loc = Location.none; txt = Ldot (Lident "ReactDOM", "jsxs")},
504510
[] )
505511
in
506512
Exp.apply ~attrs jsxExpr
507-
(key @ [(nolabel, componentNameExpr); (nolabel, props)])
513+
([(nolabel, componentNameExpr); (nolabel, props)] @ keyAndUnit)
508514
| _ ->
509515
let children, nonChildrenProps =
510516
extractChildren ~loc:jsxExprLoc callArguments

tests/ppx/react/expected/noPropsWithKey.res.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,12 @@ module V4C = {
6464
let make = (_: props) =>
6565
React.jsxs(
6666
React.jsxFragment,
67-
{children: [React.jsx(~key="k", V4CA.make, {}), React.jsx(~key="k", V4CB.make, {})]},
67+
{
68+
children: [
69+
React.jsxKeyed(V4CA.make, {}, ~key="k", ()),
70+
React.jsxKeyed(V4CB.make, {}, ~key="k", ()),
71+
],
72+
},
6873
)
6974
let make = {
7075
let \"NoPropsWithKey$V4C" = props => make(props)

tests/ppx/react/expected/optionalKeyType.res.txt

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,24 +54,27 @@ let _ = ReactDOM.createDOMElementVariadic(
5454

5555
@@jsxConfig({version: 4, mode: "automatic"})
5656

57-
let _ = React.jsx(~key="k", C.make, {})
58-
let _ = React.jsx(~key=?Some("k"), C.make, {})
59-
let _ = React.jsx(~key?, C.make, {})
60-
let _ = ReactDOM.jsx(~key="k", "div", {})
61-
let _ = ReactDOM.jsx(~key=?Some("k"), "div", {})
62-
let _ = ReactDOM.jsx(~key?, "div", {})
63-
let _ = ReactDOM.jsxs(
64-
~key="k",
57+
let _ = React.jsxKeyed(C.make, {}, ~key="k", ())
58+
let _ = React.jsxKeyed(C.make, {}, ~key=?Some("k"), ())
59+
let _ = React.jsxKeyed(C.make, {}, ~key?, ())
60+
let _ = ReactDOM.jsxKeyed("div", {}, ~key="k", ())
61+
let _ = ReactDOM.jsxKeyed("div", {}, ~key=?Some("k"), ())
62+
let _ = ReactDOM.jsxKeyed("div", {}, ~key?, ())
63+
let _ = ReactDOM.jsxsKeyed(
6564
"div",
6665
{children: React.array([ReactDOM.jsx("br", {}), ReactDOM.jsx("br", {})])},
66+
~key="k",
67+
(),
6768
)
68-
let _ = ReactDOM.jsxs(
69-
~key=?Some("k"),
69+
let _ = ReactDOM.jsxsKeyed(
7070
"div",
7171
{children: React.array([ReactDOM.jsx("br", {}), ReactDOM.jsx("br", {})])},
72+
~key=?Some("k"),
73+
(),
7274
)
73-
let _ = ReactDOM.jsxs(
74-
~key?,
75+
let _ = ReactDOM.jsxsKeyed(
7576
"div",
7677
{children: React.array([ReactDOM.jsx("br", {}), ReactDOM.jsx("br", {})])},
78+
~key?,
79+
(),
7780
)

tests/ppx/react/expected/spreadProps.res.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ let c2 = React.jsx(A.make, {...p, x: "x"})
3636

3737
let c3 = ReactDOM.jsx("div", p)
3838

39-
let c4 = ReactDOM.jsx(~key="k", "div", {...p, x: "x"})
39+
let c4 = ReactDOM.jsxKeyed("div", {...p, x: "x"}, ~key="k", ())
4040

41-
let c5 = ReactDOM.jsxs(
42-
~key="k",
41+
let c5 = ReactDOM.jsxsKeyed(
4342
"div",
4443
{...p, children: React.array([ReactDOM.jsx("br", {}), ReactDOM.jsx("br", {})])},
44+
~key="k",
45+
(),
4546
)

0 commit comments

Comments
 (0)