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

Fix the usage of spreadProps without any other props #645

Merged
merged 5 commits into from
Sep 22, 2022
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
27 changes: 17 additions & 10 deletions cli/reactjs_jsx_v4.ml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ let makeModuleName fileName nestedModules fnName =

(* make record from props and spread props if exists *)
let recordFromProps ~loc ~removeKey callArguments =
let spreadPropsLabel = "_spreadProps" in
let rec removeLastPositionUnitAux props acc =
match props with
| [] -> acc
Expand All @@ -183,7 +184,16 @@ let recordFromProps ~loc ~removeKey callArguments =
| (Nolabel, {pexp_loc}) :: _rest ->
React_jsx_common.raiseError ~loc:pexp_loc
"JSX: found non-labelled argument before the last position"
| prop :: rest -> removeLastPositionUnitAux rest (prop :: acc)
| ((Labelled txt, {pexp_loc}) as prop) :: rest
| ((Optional txt, {pexp_loc}) as prop) :: rest ->
if txt = spreadPropsLabel then
match acc with
| [] -> removeLastPositionUnitAux rest (prop :: acc)
| _ ->
React_jsx_common.raiseError ~loc:pexp_loc
"JSX: use {...p} {x: v} not {x: v} {...p} \n\
\ multiple spreads {...p} {...p} not allowed."
else removeLastPositionUnitAux rest (prop :: acc)
in
let props, propsToSpread =
removeLastPositionUnitAux callArguments []
Expand All @@ -208,20 +218,17 @@ let recordFromProps ~loc ~removeKey callArguments =
let spreadFields =
propsToSpread |> List.map (fun (_, expression) -> expression)
in
match spreadFields with
| [] ->
match (fields, spreadFields) with
| [], [spreadProps] | [], spreadProps :: _ -> spreadProps
| _, [] ->
{
pexp_desc = Pexp_record (fields, None);
pexp_loc = loc;
pexp_attributes = [];
}
| [spreadProps] ->
{
pexp_desc = Pexp_record (fields, Some spreadProps);
pexp_loc = loc;
pexp_attributes = [];
}
| spreadProps :: _ ->
| _, [spreadProps]
(* take the first spreadProps only *)
| _, spreadProps :: _ ->
{
pexp_desc = Pexp_record (fields, Some spreadProps);
pexp_loc = loc;
Expand Down
25 changes: 25 additions & 0 deletions tests/ppx/react/expected/spreadProps.res.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@@jsxConfig({version: 4, mode: "classic"})
// Error: spreadProps should be first in order than other props
// let c0 = <A x="x" {...p} />

// Error: multiple spreadProps not allowed
// let c0 = <A x="x" {...p0} {...p1} />

// only spread props
let c1 = React.createElement(A.make, p)

// reversed order
let c2 = React.createElement(A.make, {...p, x: "x"})

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

// Error: multiple spreadProps not allowed
// let c0 = <A x="x" {...p0} {...p1} />

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

// reversed order
let c2 = React.jsx(A.make, {...p, x: "x"})
25 changes: 25 additions & 0 deletions tests/ppx/react/spreadProps.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@@jsxConfig({version:4, mode: "classic"})
// Error: spreadProps should be first in order than other props
// let c0 = <A x="x" {...p} />

// Error: multiple spreadProps not allowed
// let c0 = <A x="x" {...p0} {...p1} />

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

// reversed order
let c2 = <A {...p} x="x" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the generated output, perhaps this should also be the only order allowed, and the first example, in the other order, should be an error?

Copy link
Member Author

@mununki mununki Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to describe the spec of how the spread props will work compared to the js. In js, always the latter one has a priority which means order does matter. But in v4 spread props, independent props always have a priority.

How about allowing reversed order only? The semantic seems matching to the js output.

let c2 = <A {...p} x="x" />

Or, describing on the spec that each independent prop will be updated at the end regardless of order.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. Allowing reverse only seems natural. And avoids confusion if there's only one way.

Copy link
Member Author

@mununki mununki Sep 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One thing came to my mind, maybe I think I can extract the (label, expression) list from arguments and label declaration in spread prop. then I can reduce it to the single record expression. I’ll give it a try.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah..I confused. Never mind the reduce thing, p is a just Lident not Pexp_record.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


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

// Error: multiple spreadProps not allowed
// let c0 = <A x="x" {...p0} {...p1} />

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

// reversed order
let c2 = <A {...p} x="x" />