Skip to content

Commit ed39859

Browse files
committed
CreateInterface: handle the case when make has type React.component<...>.
E.g. when make is wrapped into React.memo instead of coming directly from the ppx. Fixes #382
1 parent 22953eb commit ed39859

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

analysis/src/CreateInterface.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,35 @@ let printSignature ~extractor ~signature =
107107
Buffer.add_string buf (indent ^ "@react.component\n");
108108
Buffer.add_string buf (indent ^ newItemStr ^ "\n");
109109
processSignature ~indent rest
110+
| Sig_value
111+
(id1 (* makeProps *), {val_loc = makePropsLoc; val_type = makePropsType})
112+
:: Sig_value
113+
( id2 (* make *),
114+
({
115+
val_type =
116+
{
117+
desc =
118+
Tconstr
119+
( Pdot (Pident {name = "React"}, "component", _),
120+
[{desc = Tobject (tObj, _)}],
121+
_ );
122+
};
123+
} as vd2) )
124+
:: rest
125+
when Ident.name id1 = Ident.name id2 ^ "Props"
126+
&& (* from implementation *) makePropsLoc.loc_ghost ->
127+
(* React.component<{"name": string}> ~~> (~name:string) => React.element *)
128+
let reactElement =
129+
Ctype.newconstr (Pdot (Pident (Ident.create "React"), "element", 0)) []
130+
in
131+
let funType = tObj |> objectPropsToFun ~rhs:reactElement ~makePropsType in
132+
let newItemStr =
133+
sigItemToString
134+
(Printtyp.tree_of_value_description id2 {vd2 with val_type = funType})
135+
in
136+
Buffer.add_string buf (indent ^ "@react.component\n");
137+
Buffer.add_string buf (indent ^ newItemStr ^ "\n");
138+
processSignature ~indent rest
110139
| Sig_value
111140
(id1 (* makeProps *), {val_loc = makePropsLoc; val_type = makePropsType})
112141
:: Sig_value

analysis/tests/src/CreateInterface.res

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ module Opt = {
111111

112112
module Opt2: OptT = Opt
113113
module Opt3 = Opt
114+
115+
module Memo = {
116+
@react.component
117+
let make = (~name) => React.string(name)
118+
119+
let make = React.memo(make)
120+
}

analysis/tests/src/expected/CreateInterface.res.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,8 @@ module Opt: {
7979
}
8080
module Opt2: OptT
8181
module Opt3 = Opt
82+
module Memo: {
83+
@react.component
84+
let make: (~name: string) => React.element
85+
}
8286

0 commit comments

Comments
 (0)