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

Commit 86f1e93

Browse files
mununkicristianoc
authored andcommitted
spread props errors
- first in order - multiple use not allowed
1 parent 56a6d12 commit 86f1e93

File tree

3 files changed

+26
-13
lines changed

3 files changed

+26
-13
lines changed

cli/reactjs_jsx_v4.ml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,16 @@ let recordFromProps ~loc ~removeKey callArguments =
183183
| (Nolabel, {pexp_loc}) :: _rest ->
184184
React_jsx_common.raiseError ~loc:pexp_loc
185185
"JSX: found non-labelled argument before the last position"
186-
| prop :: rest -> removeLastPositionUnitAux rest (prop :: acc)
186+
| ((Labelled txt, {pexp_loc}) as prop) :: rest
187+
| ((Optional txt, {pexp_loc}) as prop) :: rest ->
188+
if txt = "_spreadProps" then
189+
match acc with
190+
| [] -> removeLastPositionUnitAux rest (prop :: acc)
191+
| _ ->
192+
React_jsx_common.raiseError ~loc:pexp_loc
193+
"JSX: spread props should be first in order than other props\n\
194+
\ and multiple spread props are not allowed."
195+
else removeLastPositionUnitAux rest (prop :: acc)
187196
in
188197
let props, propsToSpread =
189198
removeLastPositionUnitAux callArguments []

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
@@jsxConfig({version: 4, mode: "classic"})
2-
let c0 = React.createElement(A.make, {...p, x: "x"})
2+
// Error: spreadProps should be first in order than other props
3+
// let c0 = <A x="x" {...p} />
34

4-
// ignore second one
5-
let c0 = React.createElement(A.make, {...p0, x: "x"})
5+
// Error: multiple spreadProps not allowed
6+
// let c0 = <A x="x" {...p0} {...p1} />
67

78
// only spread props
89
let c1 = React.createElement(A.make, p)
@@ -11,10 +12,11 @@ let c1 = React.createElement(A.make, p)
1112
let c2 = React.createElement(A.make, {...p, x: "x"})
1213

1314
@@jsxConfig({version: 4, mode: "automatic"})
14-
let c0 = React.jsx(A.make, {...p, x: "x"})
15+
// Error: spreadProps should be first in order than other props
16+
// let c0 = <A x="x" {...p} />
1517

16-
// ignore second one
17-
let c0 = React.jsx(A.make, {...p0, x: "x"})
18+
// Error: multiple spreadProps not allowed
19+
// let c0 = <A x="x" {...p0} {...p1} />
1820

1921
// only spread props
2022
let c1 = React.jsx(A.make, p)

tests/ppx/react/spreadProps.res

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
@@jsxConfig({version:4, mode: "classic"})
2-
let c0 = <A x="x" {...p} />
2+
// Error: spreadProps should be first in order than other props
3+
// let c0 = <A x="x" {...p} />
34

4-
// ignore second one
5-
let c0 = <A x="x" {...p0} {...p1} />
5+
// Error: multiple spreadProps not allowed
6+
// let c0 = <A x="x" {...p0} {...p1} />
67

78
// only spread props
89
let c1 = <A {...p} />
@@ -11,10 +12,11 @@ let c1 = <A {...p} />
1112
let c2 = <A {...p} x="x" />
1213

1314
@@jsxConfig({version:4, mode: "automatic"})
14-
let c0 = <A x="x" {...p} />
15+
// Error: spreadProps should be first in order than other props
16+
// let c0 = <A x="x" {...p} />
1517

16-
// ignore second one
17-
let c0 = <A x="x" {...p0} {...p1} />
18+
// Error: multiple spreadProps not allowed
19+
// let c0 = <A x="x" {...p0} {...p1} />
1820

1921
// only spread props
2022
let c1 = <A {...p} />

0 commit comments

Comments
 (0)