Skip to content

Commit 6761625

Browse files
committed
Cleanup/unify component cases in CreateInterface.
1 parent ed39859 commit 6761625

File tree

1 file changed

+38
-61
lines changed

1 file changed

+38
-61
lines changed

analysis/src/CreateInterface.ml

Lines changed: 38 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -86,73 +86,50 @@ let printSignature ~extractor ~signature =
8686

8787
let buf = Buffer.create 10 in
8888

89+
let getComponentType (typ : Types.type_expr) =
90+
let reactElement =
91+
Ctype.newconstr (Pdot (Pident (Ident.create "React"), "element", 0)) []
92+
in
93+
match typ.desc with
94+
| Tarrow (_, {desc = Tobject (tObj, _)}, retType, _) -> Some (tObj, retType)
95+
| Tconstr
96+
( Pdot (Pident {name = "React"}, "component", _),
97+
[{desc = Tobject (tObj, _)}],
98+
_ ) ->
99+
Some (tObj, reactElement)
100+
| Tconstr
101+
( Pdot (Pident {name = "React"}, "componentLike", _),
102+
[{desc = Tobject (tObj, _)}; retType],
103+
_ ) ->
104+
Some (tObj, retType)
105+
| _ -> None
106+
in
107+
89108
let rec processSignature ~indent (signature : Types.signature) : unit =
90109
match signature with
91110
| Sig_value
92-
(id1 (* makeProps *), {val_loc = makePropsLoc; val_type = makePropsType})
93-
:: Sig_value
94-
( id2 (* make *),
95-
({
96-
val_type = {desc = Tarrow (_, {desc = Tobject (tObj, _)}, t2, _)};
97-
} as vd2) )
98-
:: rest
99-
when Ident.name id1 = Ident.name id2 ^ "Props"
100-
&& (* from implementation *) makePropsLoc.loc_ghost ->
101-
(* {"name": string} => React.element ~~> (~name:string) => React.element *)
102-
let funType = tObj |> objectPropsToFun ~rhs:t2 ~makePropsType in
103-
let newItemStr =
104-
sigItemToString
105-
(Printtyp.tree_of_value_description id2 {vd2 with val_type = funType})
111+
( makePropsId (* makeProps *),
112+
{val_loc = makePropsLoc; val_type = makePropsType} )
113+
:: Sig_value (makeId (* make *), makeValueDesc) :: rest
114+
when Ident.name makePropsId = Ident.name makeId ^ "Props"
115+
&& ((* from implementation *) makePropsLoc.loc_ghost
116+
|| (* from interface *) makePropsLoc = makeValueDesc.val_loc)
117+
&& getComponentType makeValueDesc.val_type <> None ->
118+
(*
119+
{"name": string} => retType ~~> (~name:string) => retType
120+
React.component<{"name": string}> ~~> (~name:string) => React.element
121+
React.componentLike<{"name": string}, retType> ~~> (~name:string) => retType
122+
*)
123+
let tObj, retType =
124+
match getComponentType makeValueDesc.val_type with
125+
| None -> assert false
126+
| Some (tObj, retType) -> (tObj, retType)
106127
in
107-
Buffer.add_string buf (indent ^ "@react.component\n");
108-
Buffer.add_string buf (indent ^ newItemStr ^ "\n");
109-
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
139-
| Sig_value
140-
(id1 (* makeProps *), {val_loc = makePropsLoc; val_type = makePropsType})
141-
:: Sig_value
142-
( id2 (* make *),
143-
({
144-
val_type =
145-
{desc = Tconstr (_, [{desc = Tobject (tObj, _)}; t2], _)};
146-
} as vd2) )
147-
:: rest
148-
when Ident.name id1 = Ident.name id2 ^ "Props"
149-
&& (* from interface *) makePropsLoc = vd2.val_loc ->
150-
(* React.componentLike<{"name": string}, React.element> ~~>
151-
(~name:string) => React.element *)
152-
let funType = tObj |> objectPropsToFun ~rhs:t2 ~makePropsType in
128+
let funType = tObj |> objectPropsToFun ~rhs:retType ~makePropsType in
153129
let newItemStr =
154130
sigItemToString
155-
(Printtyp.tree_of_value_description id2 {vd2 with val_type = funType})
131+
(Printtyp.tree_of_value_description makeId
132+
{makeValueDesc with val_type = funType})
156133
in
157134
Buffer.add_string buf (indent ^ "@react.component\n");
158135
Buffer.add_string buf (indent ^ newItemStr ^ "\n");

0 commit comments

Comments
 (0)