@@ -311,13 +311,30 @@ let makeTypeDecls propsName loc namedTypeList =
311
311
~kind: (Ptype_record labelDeclList);
312
312
]
313
313
314
+ let makeTypeDeclsWithCoreType propsName loc coreType typVars =
315
+ [
316
+ Type. mk ~loc {txt = propsName; loc} ~kind: Ptype_abstract
317
+ ~params: (typVars |> List. map (fun v -> (v, Invariant )))
318
+ ~manifest: coreType;
319
+ ]
320
+
314
321
(* type props<'x, 'y, ...> = { x: 'x, y?: 'y, ... } *)
315
- let makePropsRecordType propsName loc namedTypeList =
316
- Str. type_ Nonrecursive (makeTypeDecls propsName loc namedTypeList)
322
+ let makePropsRecordType ~coreTypeOfAttr ~typVarsOfCoreType propsName loc
323
+ namedTypeList =
324
+ Str. type_ Nonrecursive
325
+ (match coreTypeOfAttr with
326
+ | None -> makeTypeDecls propsName loc namedTypeList
327
+ | Some coreType ->
328
+ makeTypeDeclsWithCoreType propsName loc coreType typVarsOfCoreType)
317
329
318
330
(* type props<'x, 'y, ...> = { x: 'x, y?: 'y, ... } *)
319
- let makePropsRecordTypeSig propsName loc namedTypeList =
320
- Sig. type_ Nonrecursive (makeTypeDecls propsName loc namedTypeList)
331
+ let makePropsRecordTypeSig ~coreTypeOfAttr ~typVarsOfCoreType propsName loc
332
+ namedTypeList =
333
+ Sig. type_ Nonrecursive
334
+ (match coreTypeOfAttr with
335
+ | None -> makeTypeDecls propsName loc namedTypeList
336
+ | Some coreType ->
337
+ makeTypeDeclsWithCoreType propsName loc coreType typVarsOfCoreType)
321
338
322
339
let transformUppercaseCall3 ~config modulePath mapper jsxExprLoc callExprLoc
323
340
attrs callArguments =
@@ -733,6 +750,12 @@ let transformStructureItem ~config mapper item =
733
750
config.hasReactComponent < - true ;
734
751
check_string_int_attribute_iter.structure_item
735
752
check_string_int_attribute_iter item;
753
+ let coreTypeOfAttr = React_jsx_common. coreTypeOfAttrs pval_attributes in
754
+ let typVarsOfCoreType =
755
+ coreTypeOfAttr
756
+ |> Option. map React_jsx_common. typVarsOfCoreType
757
+ |> Option. value ~default: []
758
+ in
736
759
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType ) =
737
760
match ptyp_desc with
738
761
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
@@ -749,11 +772,14 @@ let transformStructureItem ~config mapper item =
749
772
let retPropsType =
750
773
Typ. constr ~loc: pstr_loc
751
774
(Location. mkloc (Lident " props" ) pstr_loc)
752
- (makePropsTypeParams namedTypeList)
775
+ (match coreTypeOfAttr with
776
+ | None -> makePropsTypeParams namedTypeList
777
+ | Some _ -> typVarsOfCoreType)
753
778
in
754
779
(* type props<'x, 'y> = { x: 'x, y?: 'y, ... } *)
755
780
let propsRecordType =
756
- makePropsRecordType " props" pstr_loc namedTypeList
781
+ makePropsRecordType ~core TypeOfAttr ~typ VarsOfCoreType " props"
782
+ pstr_loc namedTypeList
757
783
in
758
784
(* can't be an arrow because it will defensively uncurry *)
759
785
let newExternalType =
@@ -787,6 +813,14 @@ let transformStructureItem ~config mapper item =
787
813
React_jsx_common. raiseErrorMultipleReactComponent ~loc: pstr_loc
788
814
else (
789
815
config.hasReactComponent < - true ;
816
+ let coreTypeOfAttr =
817
+ React_jsx_common. coreTypeOfAttrs binding.pvb_attributes
818
+ in
819
+ let typVarsOfCoreType =
820
+ coreTypeOfAttr
821
+ |> Option. map React_jsx_common. typVarsOfCoreType
822
+ |> Option. value ~default: []
823
+ in
790
824
let bindingLoc = binding.pvb_loc in
791
825
let bindingPatLoc = binding.pvb_pat.ppat_loc in
792
826
let binding =
@@ -977,7 +1011,8 @@ let transformStructureItem ~config mapper item =
977
1011
let vbMatchList = List. map vbMatch namedArgWithDefaultValueList in
978
1012
(* type props = { ... } *)
979
1013
let propsRecordType =
980
- makePropsRecordType " props" pstr_loc namedTypeList
1014
+ makePropsRecordType ~core TypeOfAttr ~typ VarsOfCoreType " props"
1015
+ pstr_loc namedTypeList
981
1016
in
982
1017
let innerExpression =
983
1018
Exp. apply
@@ -989,19 +1024,23 @@ let transformStructureItem ~config mapper item =
989
1024
[(Nolabel , Exp. ident (Location. mknoloc @@ Lident " ref" ))]
990
1025
| false -> [] )
991
1026
in
1027
+ let makePropsPattern = function
1028
+ | [] -> Pat. var @@ Location. mknoloc " props"
1029
+ | _ ->
1030
+ Pat. constraint_
1031
+ (Pat. var @@ Location. mknoloc " props" )
1032
+ (Typ. constr (Location. mknoloc @@ Lident " props" ) [Typ. any () ])
1033
+ in
992
1034
let fullExpression =
993
1035
(* React component name should start with uppercase letter *)
994
1036
(* let make = { let \"App" = props => make(props); \"App" } *)
995
1037
(* let make = React.forwardRef({
996
1038
let \"App" = (props, ref) => make({...props, ref: @optional (Js.Nullabel.toOption(ref))})
997
1039
})*)
998
1040
Exp. fun_ nolabel None
999
- (match namedTypeList with
1000
- | [] -> Pat. var @@ Location. mknoloc " props"
1001
- | _ ->
1002
- Pat. constraint_
1003
- (Pat. var @@ Location. mknoloc " props" )
1004
- (Typ. constr (Location. mknoloc @@ Lident " props" ) [Typ. any () ]))
1041
+ (match coreTypeOfAttr with
1042
+ | None -> makePropsPattern namedTypeList
1043
+ | Some _ -> makePropsPattern typVarsOfCoreType)
1005
1044
(if hasForwardRef then
1006
1045
Exp. fun_ nolabel None
1007
1046
(Pat. var @@ Location. mknoloc " ref" )
@@ -1105,8 +1144,12 @@ let transformStructureItem ~config mapper item =
1105
1144
(Pat. constraint_ recordPattern
1106
1145
(Typ. constr ~loc: emptyLoc
1107
1146
{txt = Lident " props" ; loc = emptyLoc}
1108
- (makePropsTypeParams ~strip ExplicitOption:true
1109
- ~strip ExplicitJsNullableOfRef:hasForwardRef namedTypeList)))
1147
+ (match coreTypeOfAttr with
1148
+ | None ->
1149
+ makePropsTypeParams ~strip ExplicitOption:true
1150
+ ~strip ExplicitJsNullableOfRef:hasForwardRef
1151
+ namedTypeList
1152
+ | Some _ -> typVarsOfCoreType)))
1110
1153
expression
1111
1154
in
1112
1155
(* let make = ({id, name, ...}: props<'id, 'name, ...>) => { ... } *)
@@ -1182,6 +1225,12 @@ let transformSignatureItem ~config _mapper item =
1182
1225
check_string_int_attribute_iter.signature_item
1183
1226
check_string_int_attribute_iter item;
1184
1227
let hasForwardRef = ref false in
1228
+ let coreTypeOfAttr = React_jsx_common. coreTypeOfAttrs pval_attributes in
1229
+ let typVarsOfCoreType =
1230
+ coreTypeOfAttr
1231
+ |> Option. map React_jsx_common. typVarsOfCoreType
1232
+ |> Option. value ~default: []
1233
+ in
1185
1234
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType ) =
1186
1235
match ptyp_desc with
1187
1236
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
@@ -1204,10 +1253,13 @@ let transformSignatureItem ~config _mapper item =
1204
1253
let retPropsType =
1205
1254
Typ. constr
1206
1255
(Location. mkloc (Lident " props" ) psig_loc)
1207
- (makePropsTypeParams namedTypeList)
1256
+ (match coreTypeOfAttr with
1257
+ | None -> makePropsTypeParams namedTypeList
1258
+ | Some _ -> typVarsOfCoreType)
1208
1259
in
1209
1260
let propsRecordType =
1210
- makePropsRecordTypeSig " props" psig_loc
1261
+ makePropsRecordTypeSig ~core TypeOfAttr ~typ VarsOfCoreType " props"
1262
+ psig_loc
1211
1263
((* If there is Nolabel arg, regard the type as ref in forwardRef *)
1212
1264
(if ! hasForwardRef then [(true , " ref" , [] , refType Location. none)]
1213
1265
else [] )
0 commit comments