@@ -261670,9 +261670,10 @@ let otherAttrsPure (loc, _) = loc.txt <> "react.component"
261670
261670
let hasAttrOnBinding { pvb_attributes } = find_opt hasAttr pvb_attributes <> None
261671
261671
261672
261672
(* Finds the name of the variable the binding is assigned to, otherwise raises Invalid_argument *)
261673
- let getFnName binding =
261673
+ let rec getFnName binding =
261674
261674
match binding with
261675
- | { pvb_pat = { ppat_desc = Ppat_var { txt } } } -> txt
261675
+ | { ppat_desc = Ppat_var { txt } } -> txt
261676
+ | { ppat_desc = Ppat_constraint (pat, _) } -> getFnName pat
261676
261677
| _ -> raise (Invalid_argument "react.component calls cannot be destructured.")
261677
261678
[@@raises Invalid_argument]
261678
261679
@@ -261809,6 +261810,17 @@ let makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList =
261809
261810
(makePropsType ~loc namedTypeList)
261810
261811
[@@raises Invalid_argument]
261811
261812
261813
+ let newtypeToVar newtype type_ =
261814
+ let var_desc = Ptyp_var ("type-" ^ newtype) in
261815
+ let typ (mapper : Ast_mapper.mapper) typ =
261816
+ match typ.ptyp_desc with
261817
+ | Ptyp_constr ({txt = Lident name}, _) when name = newtype ->
261818
+ {typ with ptyp_desc = var_desc}
261819
+ | _ -> Ast_mapper.default_mapper.typ mapper typ
261820
+ in
261821
+ let mapper = {Ast_mapper.default_mapper with typ} in
261822
+ mapper.typ mapper type_
261823
+
261812
261824
(* TODO: some line number might still be wrong *)
261813
261825
let jsxMapper () =
261814
261826
let jsxVersion = ref None in
@@ -261911,7 +261923,7 @@ let jsxMapper () =
261911
261923
[@@raises Invalid_argument]
261912
261924
in
261913
261925
261914
- let rec recursivelyTransformNamedArgsForMake mapper expr list =
261926
+ let rec recursivelyTransformNamedArgsForMake mapper expr args newtypes =
261915
261927
let expr = mapper.expr mapper expr in
261916
261928
match expr.pexp_desc with
261917
261929
(* TODO: make this show up with a loc. *)
@@ -261952,19 +261964,23 @@ let jsxMapper () =
261952
261964
let type_ = match pattern with { ppat_desc = Ppat_constraint (_, type_) } -> Some type_ | _ -> None in
261953
261965
261954
261966
recursivelyTransformNamedArgsForMake mapper expression
261955
- ((arg, default, pattern, alias, pattern.ppat_loc, type_) :: list)
261967
+ ((arg, default, pattern, alias, pattern.ppat_loc, type_) :: args) newtypes
261956
261968
| Pexp_fun (Nolabel, _, { ppat_desc = Ppat_construct ({ txt = Lident "()" }, _) | Ppat_any }, _expression) ->
261957
- (list , None)
261969
+ (args, newtypes , None)
261958
261970
| Pexp_fun
261959
261971
( Nolabel,
261960
261972
_,
261961
261973
{ ppat_desc = Ppat_var { txt } | Ppat_constraint ({ ppat_desc = Ppat_var { txt } }, _) },
261962
261974
_expression ) ->
261963
- (list , Some txt)
261975
+ (args, newtypes , Some txt)
261964
261976
| Pexp_fun (Nolabel, _, pattern, _expression) ->
261965
261977
Location.raise_errorf ~loc:pattern.ppat_loc
261966
261978
"React: react.component refs only support plain arguments and type annotations."
261967
- | _ -> (list, None)
261979
+ | Pexp_newtype (label, expression) ->
261980
+ recursivelyTransformNamedArgsForMake mapper expression args (label :: newtypes)
261981
+ | Pexp_constraint (expression, _typ) ->
261982
+ recursivelyTransformNamedArgsForMake mapper expression args newtypes
261983
+ | _ -> (args, newtypes, None)
261968
261984
[@@raises Invalid_argument]
261969
261985
in
261970
261986
@@ -262064,7 +262080,7 @@ let jsxMapper () =
262064
262080
let bindingLoc = binding.pvb_loc in
262065
262081
let bindingPatLoc = binding.pvb_pat.ppat_loc in
262066
262082
let binding = { binding with pvb_pat = { binding.pvb_pat with ppat_loc = emptyLoc }; pvb_loc = emptyLoc } in
262067
- let fnName = getFnName binding in
262083
+ let fnName = getFnName binding.pvb_pat in
262068
262084
let internalFnName = fnName ^ "$Internal" in
262069
262085
let fullModuleName = makeModuleName fileName !nestedModules fnName in
262070
262086
let modifiedBindingOld binding =
@@ -262073,7 +262089,8 @@ let jsxMapper () =
262073
262089
let rec spelunkForFunExpression expression =
262074
262090
match expression with
262075
262091
(* let make = (~prop) => ... *)
262076
- | { pexp_desc = Pexp_fun _ } -> expression
262092
+ | { pexp_desc = Pexp_fun _ }
262093
+ | { pexp_desc = Pexp_newtype _ } -> expression
262077
262094
(* let make = {let foo = bar in (~prop) => ...} *)
262078
262095
| { pexp_desc = Pexp_let (_recursive, _vbs, returnExpression) } ->
262079
262096
(* here's where we spelunk! *)
@@ -262083,6 +262100,8 @@ let jsxMapper () =
262083
262100
spelunkForFunExpression innerFunctionExpression
262084
262101
| { pexp_desc = Pexp_sequence (_wrapperExpression, innerFunctionExpression) } ->
262085
262102
spelunkForFunExpression innerFunctionExpression
262103
+ | { pexp_desc = Pexp_constraint (innerFunctionExpression, _typ) } ->
262104
+ spelunkForFunExpression innerFunctionExpression
262086
262105
| _ ->
262087
262106
raise
262088
262107
(Invalid_argument
@@ -262171,8 +262190,8 @@ let jsxMapper () =
262171
262190
in
262172
262191
let props = getPropsAttr payload in
262173
262192
(* do stuff here! *)
262174
- let namedArgList, forwardRef =
262175
- recursivelyTransformNamedArgsForMake mapper (modifiedBindingOld binding) []
262193
+ let namedArgList, newtypes, forwardRef =
262194
+ recursivelyTransformNamedArgsForMake mapper (modifiedBindingOld binding) [] []
262176
262195
in
262177
262196
let namedArgListWithKeyAndRef =
262178
262197
(optional "key", None, Pat.var { txt = "key"; loc = emptyLoc }, "key", emptyLoc, Some (keyType emptyLoc))
@@ -262207,7 +262226,25 @@ let jsxMapper () =
262207
262226
in
262208
262227
let namedTypeList = List.fold_left argToType [] namedArgList in
262209
262228
let loc = emptyLoc in
262210
- let externalDecl = makeExternalDecl fnName loc namedArgListWithKeyAndRef namedTypeList in
262229
+ let externalArgs = (* translate newtypes to type variables *)
262230
+ List.fold_left
262231
+ (fun args newtype ->
262232
+ List.map (fun (a, b, c, d, e, maybeTyp) ->
262233
+ match maybeTyp with
262234
+ | Some typ -> (a, b, c, d, e, Some (newtypeToVar newtype.txt typ))
262235
+ | None -> (a, b, c, d, e, None))
262236
+ args)
262237
+ namedArgListWithKeyAndRef
262238
+ newtypes
262239
+ in
262240
+ let externalTypes = (* translate newtypes to type variables *)
262241
+ List.fold_left
262242
+ (fun args newtype ->
262243
+ List.map (fun (a, b, typ) -> (a, b, newtypeToVar newtype.txt typ)) args)
262244
+ namedTypeList
262245
+ newtypes
262246
+ in
262247
+ let externalDecl = makeExternalDecl fnName loc externalArgs externalTypes in
262211
262248
let innerExpressionArgs =
262212
262249
List.map pluckArg namedArgListWithKeyAndRefForNew
262213
262250
@ if hasUnit then [ (Nolabel, Exp.construct { loc; txt = Lident "()" } None) ] else []
@@ -262237,7 +262274,7 @@ let jsxMapper () =
262237
262274
{
262238
262275
ppat_desc =
262239
262276
Ppat_constraint
262240
- (makePropsName ~loc:emptyLoc props.propsName, makePropsType ~loc:emptyLoc namedTypeList );
262277
+ (makePropsName ~loc:emptyLoc props.propsName, makePropsType ~loc:emptyLoc externalTypes );
262241
262278
ppat_loc = emptyLoc;
262242
262279
ppat_attributes = [];
262243
262280
}
0 commit comments