Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit bca7852

Browse files
committedDec 5, 2024·
fix ignoring react component arguments when aliasing props type
1 parent c1f9e01 commit bca7852

23 files changed

+348
-75
lines changed
 

‎compiler/syntax/src/jsx_v4.ml

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,61 @@ let vb_match_expr named_arg_list expr =
920920
in
921921
aux (List.rev named_arg_list)
922922

923+
let vb_type_annotation ~expr (name, default, pattern, alias, loc, core_type) =
924+
let label = get_label name in
925+
let pattern_name =
926+
match default with
927+
| Some _ -> "__" ^ alias
928+
| None -> alias
929+
in
930+
let value_binding =
931+
Vb.mk ~loc
932+
(match pattern with
933+
| {
934+
ppat_desc =
935+
Ppat_constraint (pattern, ({ptyp_desc = Ptyp_package _} as type_));
936+
} ->
937+
(* Handle pattern: ~comp as module(Comp: Comp) *)
938+
Pat.record
939+
[
940+
( {txt = Lident label; loc = Location.none},
941+
Pat.constraint_ pattern type_ );
942+
]
943+
Closed
944+
| _ -> (
945+
(* For other cases, use regular variable pattern with type constraint *)
946+
let type_ =
947+
match pattern with
948+
| {ppat_desc = Ppat_constraint (_, type_)} -> Some type_
949+
| _ -> core_type
950+
in
951+
match type_ with
952+
| Some type_ ->
953+
Pat.constraint_ (Pat.var (Location.mkloc pattern_name loc)) type_
954+
| None -> Pat.var (Location.mkloc pattern_name loc)))
955+
(match pattern with
956+
| {ppat_desc = Ppat_constraint (_, {ptyp_desc = Ptyp_package _})} ->
957+
(* For module types, use props directly *)
958+
Exp.ident {txt = Lident "props"; loc = Location.none}
959+
| _ ->
960+
(* For other cases, use props.x form *)
961+
Exp.field
962+
(Exp.ident {txt = Lident "props"; loc = Location.none})
963+
{txt = Lident label; loc = Location.none})
964+
in
965+
Exp.let_ Nonrecursive [value_binding] expr
966+
967+
let vb_type_annotations_expr named_arg_list expr =
968+
let rec aux named_arg_list =
969+
match named_arg_list with
970+
| [] -> expr
971+
| ((name, _, _, _, _, _) as named_arg) :: rest ->
972+
let label = get_label name in
973+
if label = "ref" then aux rest
974+
else vb_type_annotation named_arg ~expr:(aux rest)
975+
in
976+
aux (List.rev named_arg_list)
977+
923978
let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
924979
if Jsx_common.has_attr_on_binding binding then (
925980
check_multiple_components ~config ~loc:pstr_loc;
@@ -1105,6 +1160,8 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
11051160
vb_match_expr named_arg_list expression
11061161
else expression
11071162
in
1163+
(* add pattern matching for optional prop value and type annotations *)
1164+
let expression = vb_type_annotations_expr named_arg_list expression in
11081165
(* (ref) => expr *)
11091166
let expression =
11101167
List.fold_left
@@ -1118,11 +1175,11 @@ let map_binding ~config ~empty_loc ~pstr_loc ~file_name ~rec_flag binding =
11181175
Exp.fun_ Nolabel None pattern expr)
11191176
expression patterns_with_nolabel
11201177
in
1121-
(* ({a, b, _}: props<'a, 'b>) *)
1178+
(* (props: props<'a, 'b>) *)
11221179
let record_pattern =
11231180
match patterns_with_label with
1124-
| [] -> Pat.any ()
1125-
| _ -> Pat.record (List.rev patterns_with_label) Open
1181+
| [] -> Pat.any () (* (_: props<'a, 'b>)*)
1182+
| _ -> Pat.var @@ Location.mknoloc "props"
11261183
in
11271184
let expression =
11281185
Exp.fun_ Nolabel None

‎tests/build_tests/react_ppx/src/recursive_component_test.bs.js

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/syntax_tests/data/ppx/react/expected/aliasProps.res.txt

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module C0 = {
44
@res.jsxComponentProps
55
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
66

7-
let make = ({priority: _, text: ?__text, _}: props<_, _>) => {
7+
let make = (props: props<_, _>) => {
8+
let _ = props.priority
9+
let __text = props.text
810
let text = switch __text {
911
| Some(text) => text
1012
| None => "Test"
@@ -23,7 +25,9 @@ module C1 = {
2325
@res.jsxComponentProps
2426
type props<'priority, 'text> = {priority: 'priority, text?: 'text}
2527

26-
let make = ({priority: p, text: ?__text, _}: props<_, _>) => {
28+
let make = (props: props<_, _>) => {
29+
let p = props.priority
30+
let __text = props.text
2731
let text = switch __text {
2832
| Some(text) => text
2933
| None => "Test"
@@ -42,7 +46,8 @@ module C2 = {
4246
@res.jsxComponentProps
4347
type props<'foo> = {foo?: 'foo}
4448

45-
let make = ({foo: ?__bar, _}: props<_>) => {
49+
let make = (props: props<_>) => {
50+
let __bar = props.foo
4651
let bar = switch __bar {
4752
| Some(foo) => foo
4853
| None => ""
@@ -61,7 +66,10 @@ module C3 = {
6166
@res.jsxComponentProps
6267
type props<'foo, 'a, 'b> = {foo?: 'foo, a?: 'a, b: 'b}
6368

64-
let make = ({foo: ?__bar, a: ?__a, b, _}: props<_, _, _>) => {
69+
let make = (props: props<_, _, _>) => {
70+
let __bar = props.foo
71+
let __a = props.a
72+
let b = props.b
6573
let bar = switch __bar {
6674
| Some(foo) => foo
6775
| None => ""
@@ -86,7 +94,9 @@ module C4 = {
8694
@res.jsxComponentProps
8795
type props<'a, 'x> = {a: 'a, x?: 'x}
8896

89-
let make = ({a: b, x: ?__x, _}: props<_, _>) => {
97+
let make = (props: props<_, _>) => {
98+
let b = props.a
99+
let __x = props.x
90100
let x = switch __x {
91101
| Some(x) => x
92102
| None => true
@@ -105,7 +115,9 @@ module C5 = {
105115
@res.jsxComponentProps
106116
type props<'a, 'z> = {a: 'a, z?: 'z}
107117

108-
let make = ({a: (x, y), z: ?__z, _}: props<_, _>) => {
118+
let make = (props: props<_, _>) => {
119+
let a = props.a
120+
let __z = props.z
109121
let z = switch __z {
110122
| Some(z) => z
111123
| None => 3
@@ -130,7 +142,11 @@ module C6 = {
130142
@res.jsxComponentProps
131143
type props<'comp, 'x> = {comp: 'comp, x: 'x}
132144

133-
let make = ({comp: module(Comp: Comp), x: (a, b), _}: props<_, _>) => React.jsx(Comp.make, {})
145+
let make = (props: props<_, _>) => {
146+
let {comp: module(Comp: Comp)} = props
147+
let x = props.x
148+
React.jsx(Comp.make, {})
149+
}
134150
let make = {
135151
let \"AliasProps$C6" = (props: props<_>) => make(props)
136152

‎tests/syntax_tests/data/ppx/react/expected/asyncAwait.res.txt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ module C0 = {
44
@res.jsxComponentProps
55
type props<'a> = {a: 'a}
66

7-
let make = async ({a, _}: props<_>) => {
7+
let make = async (props: props<_>) => {
8+
let a = props.a
89
let a = await f(a)
910
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({React.int(a)})})
1011
}
@@ -19,10 +20,13 @@ module C1 = {
1920
@res.jsxComponentProps
2021
type props<'status> = {status: 'status}
2122

22-
let make = async ({status, _}: props<_>) => {
23-
switch status {
24-
| #on => React.string("on")
25-
| #off => React.string("off")
23+
let make = async (props: props<_>) => {
24+
let status = props.status
25+
{
26+
switch status {
27+
| #on => React.string("on")
28+
| #off => React.string("off")
29+
}
2630
}
2731
}
2832
let make = {

‎tests/syntax_tests/data/ppx/react/expected/commentAtTop.res.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
@res.jsxComponentProps
22
type props<'msg> = {msg: 'msg} // test React JSX file
33

4-
let make = ({msg, _}: props<_>) => {
5-
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
4+
let make = (props: props<_>) => {
5+
let msg = props.msg
6+
{
7+
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
8+
}
69
}
710
let make = {
811
let \"CommentAtTop" = (props: props<_>) => make(props)

‎tests/syntax_tests/data/ppx/react/expected/defaultValueProp.res.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
module C0 = {
22
@res.jsxComponentProps
33
type props<'a, 'b> = {a?: 'a, b?: 'b}
4-
let make = ({a: ?__a, b: ?__b, _}: props<_, _>) => {
4+
let make = (props: props<_, _>) => {
5+
let __a = props.a
6+
let __b = props.b
57
let a = switch __a {
68
| Some(a) => a
79
| None => 2
@@ -23,7 +25,9 @@ module C1 = {
2325
@res.jsxComponentProps
2426
type props<'a, 'b> = {a?: 'a, b: 'b}
2527

26-
let make = ({a: ?__a, b, _}: props<_, _>) => {
28+
let make = (props: props<_, _>) => {
29+
let __a = props.a
30+
let b = props.b
2731
let a = switch __a {
2832
| Some(a) => a
2933
| None => 2
@@ -43,7 +47,8 @@ module C2 = {
4347
@res.jsxComponentProps
4448
type props<'a> = {a?: 'a}
4549

46-
let make = ({a: ?__a, _}: props<_>) => {
50+
let make = (props: props<_>) => {
51+
let __a = props.a
4752
let a = switch __a {
4853
| Some(a) => a
4954
| None => a
@@ -62,7 +67,8 @@ module C3 = {
6267
@res.jsxComponentProps
6368
type props<'disabled> = {disabled?: 'disabled}
6469

65-
let make = ({disabled: ?__everythingDisabled, _}: props<bool>) => {
70+
let make = (props: props<bool>) => {
71+
let __everythingDisabled: bool = props.disabled
6672
let everythingDisabled = switch __everythingDisabled {
6773
| Some(disabled) => disabled
6874
| None => false

‎tests/syntax_tests/data/ppx/react/expected/fileLevelConfig.res.txt

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ module V4C = {
44
@res.jsxComponentProps
55
type props<'msg> = {msg: 'msg}
66

7-
let make = ({msg, _}: props<_>) => {
8-
ReactDOM.createDOMElementVariadic("div", [{msg->React.string}])
7+
let make = (props: props<_>) => {
8+
let msg = props.msg
9+
{
10+
ReactDOM.createDOMElementVariadic("div", [{msg->React.string}])
11+
}
912
}
1013
let make = {
1114
let \"FileLevelConfig$V4C" = (props: props<_>) => make(props)
@@ -20,8 +23,11 @@ module V4A = {
2023
@res.jsxComponentProps
2124
type props<'msg> = {msg: 'msg}
2225

23-
let make = ({msg, _}: props<_>) => {
24-
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
26+
let make = (props: props<_>) => {
27+
let msg = props.msg
28+
{
29+
ReactDOM.jsx("div", {children: ?ReactDOM.someElement({msg->React.string})})
30+
}
2531
}
2632
let make = {
2733
let \"FileLevelConfig$V4A" = (props: props<_>) => make(props)

‎tests/syntax_tests/data/ppx/react/expected/firstClassModules.res.txt

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ module Select = {
1313
items: 'items,
1414
}
1515

16-
let make = (
17-
type a key,
18-
{model: module(T: T with type t = a and type key = key), selected, onChange, items, _}: props<
19-
_,
20-
option<key>,
21-
option<key> => unit,
22-
array<a>,
23-
>,
24-
) => {
16+
let make = (type a key, props: props<_, option<key>, option<key> => unit, array<a>>) => {
17+
let {model: module(T: T with type t = a and type key = key)} = props
18+
let selected: option<key> = props.selected
19+
let onChange: option<key> => unit = props.onChange
20+
let items: array<a> = props.items
21+
2522
let _ = (model, selected, onChange, items)
2623
ReactDOM.createDOMElementVariadic("div", [])
2724
}
@@ -32,6 +29,29 @@ module Select = {
3229
}
3330
}
3431

32+
module C6 = {
33+
module type Comp = {
34+
let xx: int
35+
@res.jsxComponentProps
36+
type props = {}
37+
38+
let make: React.componentLike<props, React.element>
39+
}
40+
@res.jsxComponentProps
41+
type props<'comp, 'x> = {comp: 'comp, x: 'x}
42+
43+
let make = (props: props<_, _>) => {
44+
let {comp: module(Comp: Comp)} = props
45+
let x = props.x
46+
Comp.xx
47+
}
48+
let make = {
49+
let \"FirstClassModules$C6" = (props: props<_>) => make(props)
50+
51+
\"FirstClassModules$C6"
52+
}
53+
}
54+
3555
module External = {
3656
module type T = {
3757
type key

‎tests/syntax_tests/data/ppx/react/expected/forwardRef.res.txt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ module V4C = {
1010
}
1111

1212
let make = (
13-
{?className, children, _}: props<_, _, ReactRef.currentDomRef>,
13+
props: props<_, _, ReactRef.currentDomRef>,
1414
ref: Js.Nullable.t<ReactRef.currentDomRef>,
15-
) =>
15+
) => {
16+
let className = props.className
17+
let children = props.children
1618
ReactDOM.createDOMElementVariadic(
1719
"div",
1820
[
@@ -28,6 +30,7 @@ module V4C = {
2830
children,
2931
],
3032
)
33+
}
3134
let make = React.forwardRef({
3235
let \"ForwardRef$V4C$FancyInput" = (props: props<_>, ref) => make(props, ref)
3336

@@ -67,9 +70,11 @@ module V4CUncurried = {
6770
}
6871

6972
let make = (
70-
{?className, children, _}: props<_, _, ReactRef.currentDomRef>,
73+
props: props<_, _, ReactRef.currentDomRef>,
7174
ref: Js.Nullable.t<ReactRef.currentDomRef>,
72-
) =>
75+
) => {
76+
let className = props.className
77+
let children = props.children
7378
ReactDOM.createDOMElementVariadic(
7479
"div",
7580
[
@@ -85,6 +90,7 @@ module V4CUncurried = {
8590
children,
8691
],
8792
)
93+
}
8894
let make = React.forwardRef({
8995
let \"ForwardRef$V4CUncurried$FancyInput" = (props: props<_>, ref) => make(props, ref)
9096

@@ -125,7 +131,9 @@ module V4A = {
125131
ref?: 'ref,
126132
}
127133

128-
let make = ({?className, children, _}: props<_, _, 'ref>, ref: Js.Nullable.t<'ref>) =>
134+
let make = (props: props<_, _, 'ref>, ref: Js.Nullable.t<'ref>) => {
135+
let className = props.className
136+
let children = props.children
129137
ReactDOM.jsxs(
130138
"div",
131139
{
@@ -142,6 +150,7 @@ module V4A = {
142150
]),
143151
},
144152
)
153+
}
145154
let make = React.forwardRef({
146155
let \"ForwardRef$V4A$FancyInput" = (props: props<_>, ref) => make(props, ref)
147156

@@ -179,7 +188,9 @@ module V4AUncurried = {
179188
ref?: 'ref,
180189
}
181190

182-
let make = ({?className, children, _}: props<_, _, 'ref>, ref: Js.Nullable.t<'ref>) =>
191+
let make = (props: props<_, _, 'ref>, ref: Js.Nullable.t<'ref>) => {
192+
let className = props.className
193+
let children = props.children
183194
ReactDOM.jsxs(
184195
"div",
185196
{
@@ -196,6 +207,7 @@ module V4AUncurried = {
196207
]),
197208
},
198209
)
210+
}
199211
let make = React.forwardRef({
200212
let \"ForwardRef$V4AUncurried$FancyInput" = (props: props<_>, ref) => make(props, ref)
201213

‎tests/syntax_tests/data/ppx/react/expected/interface.res.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
module A = {
22
@res.jsxComponentProps
33
type props<'x> = {x: 'x}
4-
let make = ({x, _}: props<_>) => React.string(x)
4+
let make = (props: props<_>) => {
5+
let x = props.x
6+
React.string(x)
7+
}
58
let make = {
69
let \"Interface$A" = (props: props<_>) => make(props)
710
\"Interface$A"

‎tests/syntax_tests/data/ppx/react/expected/interfaceWithRef.res.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
@res.jsxComponentProps type props<'x, 'ref> = {x: 'x, ref?: 'ref}
22
let make = (
3-
{x, _}: props<string, ReactDOM.Ref.currentDomRef>,
3+
props: props<string, ReactDOM.Ref.currentDomRef>,
44
ref: Js.Nullable.t<ReactDOM.Ref.currentDomRef>,
55
) => {
6+
let x: string = props.x
67
let _ = ref->Js.Nullable.toOption->Belt.Option.map(ReactDOM.Ref.domRef)
78
React.string(x)
89
}

‎tests/syntax_tests/data/ppx/react/expected/mangleKeyword.res.txt

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module C4C0 = {
44
@res.jsxComponentProps
55
type props<'T_open, 'T_type> = {@as("open") _open: 'T_open, @as("type") _type: 'T_type}
66

7-
let make = ({@as("open") _open, @as("type") _type, _}: props<_, string>) => React.string(_open)
7+
let make = (props: props<_, string>) => {
8+
let _open = props._open
9+
let _type: string = props._type
10+
React.string(_open)
11+
}
812
let make = {
913
let \"MangleKeyword$C4C0" = (props: props<_>) => make(props)
1014

@@ -27,7 +31,11 @@ module C4A0 = {
2731
@res.jsxComponentProps
2832
type props<'T_open, 'T_type> = {@as("open") _open: 'T_open, @as("type") _type: 'T_type}
2933

30-
let make = ({@as("open") _open, @as("type") _type, _}: props<_, string>) => React.string(_open)
34+
let make = (props: props<_, string>) => {
35+
let _open = props._open
36+
let _type: string = props._type
37+
React.string(_open)
38+
}
3139
let make = {
3240
let \"MangleKeyword$C4A0" = (props: props<_>) => make(props)
3341

‎tests/syntax_tests/data/ppx/react/expected/newtype.res.txt

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@ module V4C = {
44
@res.jsxComponentProps
55
type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
66

7-
let make = (type a, {a, b, c, _}: props<a, array<option<[#Foo(a)]>>, 'a>) =>
7+
let make = (type a, props: props<a, array<option<[#Foo(a)]>>, 'a>) => {
8+
let a: a = props.a
9+
let b: array<option<[#Foo(a)]>> = props.b
10+
let c: 'a = props.c
811
ReactDOM.createDOMElementVariadic("div", [])
12+
}
913
let make = {
1014
let \"Newtype$V4C" = (props: props<_>) => make(props)
1115

@@ -19,8 +23,12 @@ module V4A = {
1923
@res.jsxComponentProps
2024
type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
2125

22-
let make = (type a, {a, b, c, _}: props<a, array<option<[#Foo(a)]>>, 'a>) =>
26+
let make = (type a, props: props<a, array<option<[#Foo(a)]>>, 'a>) => {
27+
let a: a = props.a
28+
let b: array<option<[#Foo(a)]>> = props.b
29+
let c: 'a = props.c
2330
ReactDOM.jsx("div", {})
31+
}
2432
let make = {
2533
let \"Newtype$V4A" = (props: props<_>) => make(props)
2634

@@ -32,7 +40,12 @@ module V4A1 = {
3240
@res.jsxComponentProps
3341
type props<'a, 'b, 'c> = {a: 'a, b: 'b, c: 'c}
3442

35-
let make = (type x y, {a, b, c, _}: props<x, array<y>, 'a>) => ReactDOM.jsx("div", {})
43+
let make = (type x y, props: props<x, array<y>, 'a>) => {
44+
let a: x = props.a
45+
let b: array<y> = props.b
46+
let c: 'a = props.c
47+
ReactDOM.jsx("div", {})
48+
}
3649
let make = {
3750
let \"Newtype$V4A1" = (props: props<_>) => make(props)
3851

@@ -48,7 +61,8 @@ module V4A2 = {
4861
@res.jsxComponentProps
4962
type props<'foo> = {foo: 'foo}
5063

51-
let make = (type a, {foo: (foo: module(T with type t = a)), _}: props<_>) => {
64+
let make = (type a, props: props<_>) => {
65+
let {foo: (foo: module(T with type t = a))} = props
5266
module T = unpack(foo)
5367
ReactDOM.jsx("div", {})
5468
}
@@ -63,7 +77,8 @@ module V4A3 = {
6377
@res.jsxComponentProps
6478
type props<'foo> = {foo: 'foo}
6579

66-
let make = (type a, {foo, _}: props<_>) => {
80+
let make = (type a, props: props<_>) => {
81+
let foo = props.foo
6782
module T = unpack(foo: T with type t = a)
6883
foo
6984
}
@@ -76,7 +91,11 @@ module V4A3 = {
7691
@res.jsxComponentProps
7792
type props<'x, 'q> = {x: 'x, q: 'q}
7893

79-
let make = ({x, q, _}: props<('a, 'b), 'a>) => [fst(x), q]
94+
let make = (props: props<('a, 'b), 'a>) => {
95+
let x: ('a, 'b) = props.x
96+
let q: 'a = props.q
97+
[fst(x), q]
98+
}
8099
let make = {
81100
let \"Newtype" = (props: props<_>) => make(props)
82101

@@ -89,7 +108,10 @@ module Uncurried = {
89108
@res.jsxComponentProps
90109
type props<'foo> = {foo?: 'foo}
91110

92-
let make = (type a, {?foo, _}: props<_>) => React.null
111+
let make = (type a, props: props<_>) => {
112+
let foo = props.foo
113+
React.null
114+
}
93115
let make = {
94116
let \"Newtype$Uncurried" = (props: props<_>) => make(props)
95117

‎tests/syntax_tests/data/ppx/react/expected/optimizeAutomaticMode.res.txt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,14 @@ module User = {
77
@res.jsxComponentProps
88
type props<'doctor> = {doctor: 'doctor}
99

10-
let make = ({doctor, _}: props<_>) => {
11-
ReactDOM.jsx("h1", {id: "h1", children: ?ReactDOM.someElement({React.string(format(doctor))})})
10+
let make = (props: props<_>) => {
11+
let doctor = props.doctor
12+
{
13+
ReactDOM.jsx(
14+
"h1",
15+
{id: "h1", children: ?ReactDOM.someElement({React.string(format(doctor))})},
16+
)
17+
}
1218
}
1319
let make = {
1420
let \"OptimizeAutomaticMode$User" = (props: props<_>) => make(props)

‎tests/syntax_tests/data/ppx/react/expected/removedKeyProp.res.txt

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ module Foo = {
44
@res.jsxComponentProps
55
type props<'x, 'y> = {x: 'x, y: 'y}
66

7-
let make = ({x, y, _}: props<_, _>) => React.string(x ++ y)
7+
let make = (props: props<_, _>) => {
8+
let x = props.x
9+
let y = props.y
10+
React.string(x ++ y)
11+
}
812
let make = {
913
let \"RemovedKeyProp$Foo" = (props: props<_>) => make(props)
1014

@@ -16,7 +20,10 @@ module HasChildren = {
1620
@res.jsxComponentProps
1721
type props<'children> = {children: 'children}
1822

19-
let make = ({children, _}: props<_>) => React.createElement(React.fragment, {children: children})
23+
let make = (props: props<_>) => {
24+
let children = props.children
25+
React.createElement(React.fragment, {children: children})
26+
}
2027
let make = {
2128
let \"RemovedKeyProp$HasChildren" = (props: props<_>) => make(props)
2229

‎tests/syntax_tests/data/ppx/react/expected/sharedProps.res.txt

Lines changed: 70 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
module V4C1 = {
44
type props = sharedProps<string>
55

6-
let make = ({x, y, _}: props) => React.string(x ++ y)
6+
let make = (props: props) => {
7+
let x = props.x
8+
let y = props.y
9+
React.string(x ++ y)
10+
}
711
let make = {
812
let \"SharedProps$V4C1" = props => make(props)
913

@@ -14,7 +18,11 @@ module V4C1 = {
1418
module V4C2 = {
1519
type props<'a> = sharedProps<'a>
1620

17-
let make = ({x, y, _}: props<_>) => React.string(x ++ y)
21+
let make = (props: props<_>) => {
22+
let x = props.x
23+
let y = props.y
24+
React.string(x ++ y)
25+
}
1826
let make = {
1927
let \"SharedProps$V4C2" = (props: props<_>) => make(props)
2028

@@ -25,7 +33,11 @@ module V4C2 = {
2533
module V4C3 = {
2634
type props<'a> = sharedProps<string, 'a>
2735

28-
let make = ({x, y, _}: props<_>) => React.string(x ++ y)
36+
let make = (props: props<_>) => {
37+
let x = props.x
38+
let y = props.y
39+
React.string(x ++ y)
40+
}
2941
let make = {
3042
let \"SharedProps$V4C3" = (props: props<_>) => make(props)
3143

@@ -36,7 +48,11 @@ module V4C3 = {
3648
module V4C4 = {
3749
type props = sharedProps
3850

39-
let make = ({x, y, _}: props) => React.string(x ++ y)
51+
let make = (props: props) => {
52+
let x = props.x
53+
let y = props.y
54+
React.string(x ++ y)
55+
}
4056
let make = {
4157
let \"SharedProps$V4C4" = props => make(props)
4258

@@ -68,12 +84,31 @@ module V4C8 = {
6884
external make: React.componentLike<props, React.element> = "default"
6985
}
7086

87+
module V4C9 = {
88+
type props = sharedProps
89+
90+
let make = (props: props) => {
91+
let x: string = props.x
92+
let y: int = props.y
93+
React.string(x ++ y)
94+
}
95+
let make = {
96+
let \"SharedProps$V4C9" = props => make(props)
97+
98+
\"SharedProps$V4C9"
99+
}
100+
}
101+
71102
@@jsxConfig({version: 4, mode: "automatic"})
72103

73104
module V4A1 = {
74105
type props = sharedProps<string>
75106

76-
let make = ({x, y, _}: props) => React.string(x ++ y)
107+
let make = (props: props) => {
108+
let x = props.x
109+
let y = props.y
110+
React.string(x ++ y)
111+
}
77112
let make = {
78113
let \"SharedProps$V4A1" = props => make(props)
79114

@@ -84,7 +119,11 @@ module V4A1 = {
84119
module V4A2 = {
85120
type props<'a> = sharedProps<'a>
86121

87-
let make = ({x, y, _}: props<_>) => React.string(x ++ y)
122+
let make = (props: props<_>) => {
123+
let x = props.x
124+
let y = props.y
125+
React.string(x ++ y)
126+
}
88127
let make = {
89128
let \"SharedProps$V4A2" = (props: props<_>) => make(props)
90129

@@ -95,7 +134,11 @@ module V4A2 = {
95134
module V4A3 = {
96135
type props<'a> = sharedProps<string, 'a>
97136

98-
let make = ({x, y, _}: props<_>) => React.string(x ++ y)
137+
let make = (props: props<_>) => {
138+
let x = props.x
139+
let y = props.y
140+
React.string(x ++ y)
141+
}
99142
let make = {
100143
let \"SharedProps$V4A3" = (props: props<_>) => make(props)
101144

@@ -106,7 +149,11 @@ module V4A3 = {
106149
module V4A4 = {
107150
type props = sharedProps
108151

109-
let make = ({x, y, _}: props) => React.string(x ++ y)
152+
let make = (props: props) => {
153+
let x = props.x
154+
let y = props.y
155+
React.string(x ++ y)
156+
}
110157
let make = {
111158
let \"SharedProps$V4A4" = props => make(props)
112159

@@ -137,3 +184,18 @@ module V4A8 = {
137184

138185
external make: React.componentLike<props, React.element> = "default"
139186
}
187+
188+
module V4A9 = {
189+
type props = sharedProps
190+
191+
let make = (props: props) => {
192+
let x: string = props.x
193+
let y: int = props.y
194+
React.string(x ++ y)
195+
}
196+
let make = {
197+
let \"SharedProps$V4A9" = props => make(props)
198+
199+
\"SharedProps$V4A9"
200+
}
201+
}

‎tests/syntax_tests/data/ppx/react/expected/topLevel.res.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ module V4C = {
44
@res.jsxComponentProps
55
type props<'a, 'b> = {a: 'a, b: 'b}
66

7-
let make = ({a, b, _}: props<_, _>) => {
7+
let make = (props: props<_, _>) => {
8+
let a = props.a
9+
let b = props.b
810
Js.log("This function should be named 'TopLevel.react'")
911
ReactDOM.createDOMElementVariadic("div", [])
1012
}
@@ -21,7 +23,9 @@ module V4A = {
2123
@res.jsxComponentProps
2224
type props<'a, 'b> = {a: 'a, b: 'b}
2325

24-
let make = ({a, b, _}: props<_, _>) => {
26+
let make = (props: props<_, _>) => {
27+
let a = props.a
28+
let b = props.b
2529
Js.log("This function should be named 'TopLevel.react'")
2630
ReactDOM.jsx("div", {})
2731
}

‎tests/syntax_tests/data/ppx/react/expected/uncurriedProps.res.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
@res.jsxComponentProps
33
type props<'a> = {a?: 'a}
44

5-
let make = ({a: ?__a, _}: props<unit => unit>) => {
5+
let make = (props: props<unit => unit>) => {
6+
let __a: unit => unit = props.a
67
let a = switch __a {
78
| Some(a) => a
89
| None => () => ()
@@ -30,7 +31,8 @@ module Foo = {
3031
@res.jsxComponentProps
3132
type props<'callback> = {callback?: 'callback}
3233

33-
let make = ({callback: ?__callback, _}: props<(string, bool, bool) => unit>) => {
34+
let make = (props: props<(string, bool, bool) => unit>) => {
35+
let __callback: (string, bool, bool) => unit = props.callback
3436
let callback = switch __callback {
3537
| Some(callback) => callback
3638
| None => (_, _, _) => ()

‎tests/syntax_tests/data/ppx/react/expected/v4.res.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
@res.jsxComponentProps
22
type props<'x, 'y> = {x: 'x, y: 'y} // Component with type constraint
3-
let make = ({x, y, _}: props<string, string>) => React.string(x ++ y)
3+
let make = (props: props<string, string>) => {
4+
let x: string = props.x
5+
let y: string = props.y
6+
React.string(x ++ y)
7+
}
48
let make = {
59
let \"V4" = (props: props<_>) => make(props)
610
\"V4"
@@ -11,7 +15,10 @@ module AnotherName = {
1115
type // Component with another name than "make"
1216
props<'x> = {x: 'x}
1317

14-
let anotherName = ({x, _}: props<_>) => React.string(x)
18+
let anotherName = (props: props<_>) => {
19+
let x = props.x
20+
React.string(x)
21+
}
1522
let anotherName = {
1623
let \"V4$AnotherName$anotherName" = (props: props<_>) => anotherName(props)
1724

@@ -23,7 +30,10 @@ module Uncurried = {
2330
@res.jsxComponentProps
2431
type props<'x> = {x: 'x}
2532

26-
let make = ({x, _}: props<_>) => React.string(x)
33+
let make = (props: props<_>) => {
34+
let x = props.x
35+
React.string(x)
36+
}
2737
let make = {
2838
let \"V4$Uncurried" = (props: props<_>) => make(props)
2939

‎tests/syntax_tests/data/ppx/react/firstClassModules.res

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,17 @@ module Select = {
1919
}
2020
}
2121

22+
module C6 = {
23+
module type Comp = {
24+
let xx: int
25+
@react.component
26+
let make: unit => React.element
27+
}
28+
29+
@react.component
30+
let make = (~comp as module(Comp: Comp), ~x as (a, b)) => Comp.xx
31+
}
32+
2233
module External = {
2334
module type T = {
2435
type key

‎tests/syntax_tests/data/ppx/react/sharedProps.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ module V4C8 = {
4040
external make: (~x: string, ~y: string) => React.element = "default"
4141
}
4242

43+
module V4C9 = {
44+
@react.component(:sharedProps)
45+
let make = (~x: string, ~y: int) => React.string(x ++ y)
46+
}
47+
4348
@@jsxConfig({version:4, mode: "automatic"})
4449

4550
module V4A1 = {
@@ -80,4 +85,9 @@ module V4A7 = {
8085
module V4A8 = {
8186
@react.component(:sharedProps)
8287
external make: (~x: string, ~y: string) => React.element = "default"
88+
}
89+
90+
module V4A9 = {
91+
@react.component(:sharedProps)
92+
let make = (~x: string, ~y: int) => React.string(x ++ y)
8393
}

‎tests/tests/src/alias_default_value_test.mjs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
function Alias_default_value_test$C0(props) {
5-
let __b = props.b;
65
let __a = props.a;
6+
let __b = props.b;
77
let a = __a !== undefined ? __a : 2;
88
let b = __b !== undefined ? __b : (a << 1);
99
return a + b | 0;
@@ -27,11 +27,12 @@ let C1 = {
2727
};
2828

2929
function Alias_default_value_test$C2(props) {
30-
let __a = props.a;
3130
let __bar = props.foo;
31+
let __a = props.a;
32+
let b = props.b;
3233
let bar = __bar !== undefined ? __bar : "";
3334
let a = __a !== undefined ? __a : bar;
34-
return bar + a + props.b;
35+
return bar + a + b;
3536
}
3637

3738
let C2 = {

‎tests/tests/src/recursive_react_component.mjs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import * as React from "react";
44

55
function make(props) {
6+
let foo = props.foo;
67
return React.createElement(make, {
7-
foo: props.foo
8+
foo: foo
89
});
910
}
1011

0 commit comments

Comments
 (0)
Please sign in to comment.