Skip to content

Commit de5862f

Browse files
committed
fix JSX4 classic fragment with variadic expression
1 parent bf2eff8 commit de5862f

File tree

4 files changed

+57
-48
lines changed

4 files changed

+57
-48
lines changed

jscomp/syntax/src/reactjs_jsx_v4.ml

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1367,23 +1367,34 @@ let expr ~config mapper expression =
13671367
{txt = Ldot (Lident "React", "array"); loc = Location.none})
13681368
[(Nolabel, expr)]
13691369
in
1370-
let args =
1371-
[
1372-
(nolabel, fragment);
1373-
( nolabel,
1374-
match childrenExpr with
1375-
| {pexp_desc = Pexp_array children} -> (
1376-
match children with
1377-
| [] -> emptyRecord ~loc:Location.none
1378-
| [child] -> recordOfChildren child
1379-
| _ -> recordOfChildren @@ applyReactArray childrenExpr)
1380-
| _ -> recordOfChildren @@ applyReactArray childrenExpr );
1381-
]
1382-
in
13831370
let countOfChildren = function
13841371
| {pexp_desc = Pexp_array children} -> List.length children
13851372
| _ -> 0
13861373
in
1374+
let transformChildrenToProps childrenExpr =
1375+
match childrenExpr with
1376+
| {pexp_desc = Pexp_array children} -> (
1377+
match children with
1378+
| [] -> emptyRecord ~loc:Location.none
1379+
| [child] -> recordOfChildren child
1380+
| _ -> (
1381+
match config.mode with
1382+
| "automatic" -> recordOfChildren @@ applyReactArray childrenExpr
1383+
| "classic" | _ -> emptyRecord ~loc:Location.none))
1384+
| _ -> (
1385+
match config.mode with
1386+
| "automatic" -> recordOfChildren @@ applyReactArray childrenExpr
1387+
| "classic" | _ -> emptyRecord ~loc:Location.none)
1388+
in
1389+
let args =
1390+
(nolabel, fragment)
1391+
:: (nolabel, transformChildrenToProps childrenExpr)
1392+
::
1393+
(match config.mode with
1394+
| "classic" when countOfChildren childrenExpr > 1 ->
1395+
[(nolabel, childrenExpr)]
1396+
| _ -> [])
1397+
in
13871398
Exp.apply
13881399
~loc (* throw away the [@JSX] attribute and keep the others, if any *)
13891400
~attrs:nonJSXAttributes
@@ -1394,7 +1405,11 @@ let expr ~config mapper expression =
13941405
Exp.ident ~loc {loc; txt = Ldot (Lident "React", "jsxs")}
13951406
else Exp.ident ~loc {loc; txt = Ldot (Lident "React", "jsx")}
13961407
| "classic" | _ ->
1397-
Exp.ident ~loc {loc; txt = Ldot (Lident "React", "createElement")})
1408+
if countOfChildren childrenExpr > 1 then
1409+
Exp.ident ~loc
1410+
{loc; txt = Ldot (Lident "React", "createElementVariadic")}
1411+
else
1412+
Exp.ident ~loc {loc; txt = Ldot (Lident "React", "createElement")})
13981413
args)
13991414
(* Delegate to the default mapper, a deep identity traversal *)
14001415
| e -> default_mapper.expr mapper e

jscomp/syntax/tests/ppx/react/expected/fragment.res.txt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,10 @@ let _ = React.createElement(
55
React.fragment,
66
{children: ReactDOM.createDOMElementVariadic("div", [])},
77
)
8-
let _ = React.createElement(
8+
let _ = React.createElementVariadic(
99
React.fragment,
10-
{
11-
children: React.array([
12-
ReactDOM.createDOMElementVariadic("div", []),
13-
ReactDOM.createDOMElementVariadic("div", []),
14-
]),
15-
},
10+
{},
11+
[ReactDOM.createDOMElementVariadic("div", []), ReactDOM.createDOMElementVariadic("div", [])],
1612
)
1713
let _ = React.createElement(React.fragment, {children: React.createElement(React.fragment, {})})
1814
let _ = React.createElement(Z.make, {})

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ module V4C = {
2222
type props = {}
2323

2424
let make = (_: props) =>
25-
React.createElement(
25+
React.createElementVariadic(
2626
React.fragment,
27-
{
28-
children: React.array([
29-
JsxPPXReactSupport.createElementWithKey(~key="k", V4CA.make, {}),
30-
JsxPPXReactSupport.createElementWithKey(~key="k", V4CB.make, {}),
31-
]),
32-
},
27+
{},
28+
[
29+
JsxPPXReactSupport.createElementWithKey(~key="k", V4CA.make, {}),
30+
JsxPPXReactSupport.createElementWithKey(~key="k", V4CB.make, {}),
31+
],
3332
)
3433
let make = {
3534
let \"NoPropsWithKey$V4C" = props => make(props)

jscomp/syntax/tests/ppx/react/expected/removedKeyProp.res.txt

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,26 @@ module HasChildren = {
2424
type props = {}
2525

2626
let make = (_: props) =>
27-
React.createElement(
27+
React.createElementVariadic(
2828
React.fragment,
29-
{
30-
children: React.array([
31-
JsxPPXReactSupport.createElementWithKey(~key="k", Foo.make, {x: "x", y: "y"}),
32-
React.createElement(Foo.make, {x: "x", y: "y"}),
33-
JsxPPXReactSupport.createElementWithKey(
34-
~key="k",
35-
HasChildren.make,
36-
{
37-
children: React.createElement(Foo.make, {x: "x", y: "y"}),
38-
},
39-
),
40-
React.createElement(
41-
HasChildren.make,
42-
{
43-
children: React.createElement(Foo.make, {x: "x", y: "y"}),
44-
},
45-
),
46-
]),
47-
},
29+
{},
30+
[
31+
JsxPPXReactSupport.createElementWithKey(~key="k", Foo.make, {x: "x", y: "y"}),
32+
React.createElement(Foo.make, {x: "x", y: "y"}),
33+
JsxPPXReactSupport.createElementWithKey(
34+
~key="k",
35+
HasChildren.make,
36+
{
37+
children: React.createElement(Foo.make, {x: "x", y: "y"}),
38+
},
39+
),
40+
React.createElement(
41+
HasChildren.make,
42+
{
43+
children: React.createElement(Foo.make, {x: "x", y: "y"}),
44+
},
45+
),
46+
],
4847
)
4948
let make = {
5049
let \"RemovedKeyProp" = props => make(props)

0 commit comments

Comments
 (0)