@@ -1600,56 +1600,39 @@ module V4 = struct
1600
1600
|> List. filter_map (fun (_isOptional , label , _ , interiorType ) ->
1601
1601
if label = " key" || label = " ref" then None else Some interiorType)
1602
1602
1603
- (* type props<'id, 'name, ...> = { @optional key: string, @optional id: 'id, ... } *)
1604
- let makePropsRecordType propsName loc namedTypeList =
1605
- let labelDeclList =
1606
- namedTypeList
1607
- |> List. map (fun (isOptional , label , _ , _interiorType ) ->
1608
- if label = " key" then
1609
- Type. field ~loc ~attrs: optionalAttr {txt = label; loc}
1610
- (keyType Location. none)
1611
- else if label = " ref" then
1612
- Type. field ~loc
1613
- ~attrs: (if isOptional then optionalAttr else [] )
1614
- {txt = label; loc} (refType Location. none)
1615
- else if isOptional then
1616
- Type. field ~loc ~attrs: optionalAttr {txt = label; loc}
1617
- (Typ. var label)
1618
- else Type. field ~loc {txt = label; loc} (Typ. var label))
1619
- in
1603
+ let makeLabelDecls ~loc namedTypeList =
1604
+ namedTypeList
1605
+ |> List. map (fun (isOptional , label , _ , interiorType ) ->
1606
+ if label = " key" then
1607
+ Type. field ~loc ~attrs: optionalAttr {txt = label; loc} interiorType
1608
+ else if label = " ref" then
1609
+ Type. field ~loc
1610
+ ~attrs: (if isOptional then optionalAttr else [] )
1611
+ {txt = label; loc} interiorType
1612
+ else if isOptional then
1613
+ Type. field ~loc ~attrs: optionalAttr {txt = label; loc}
1614
+ (Typ. var label)
1615
+ else Type. field ~loc {txt = label; loc} (Typ. var label))
1616
+
1617
+ let makeTypeDecls propsName loc namedTypeList =
1618
+ let labelDeclList = makeLabelDecls ~loc namedTypeList in
1620
1619
(* 'id, 'className, ... *)
1621
1620
let params =
1622
1621
makePropsTypeParamsTvar namedTypeList
1623
1622
|> List. map (fun coreType -> (coreType, Invariant ))
1624
1623
in
1625
- Str. type_ Nonrecursive
1626
- [
1627
- Type. mk ~loc ~params {txt = propsName; loc}
1628
- ~kind: (Ptype_record labelDeclList);
1629
- ]
1624
+ [
1625
+ Type. mk ~loc ~params {txt = propsName; loc}
1626
+ ~kind: (Ptype_record labelDeclList);
1627
+ ]
1628
+
1629
+ (* type props<'id, 'name, ...> = { @optional key: string, @optional id: 'id, ... } *)
1630
+ let makePropsRecordType propsName loc namedTypeList =
1631
+ Str. type_ Nonrecursive (makeTypeDecls propsName loc namedTypeList)
1630
1632
1631
1633
(* type props<'id, 'name, ...> = { @optional key: string, @optional id: 'id, ... } *)
1632
1634
let makePropsRecordTypeSig propsName loc namedTypeList =
1633
- let labelDeclList =
1634
- namedTypeList
1635
- |> List. map (fun (isOptional , label , _ , _interiorType ) ->
1636
- if label = " key" then
1637
- Type. field ~loc ~attrs: optionalAttr {txt = label; loc}
1638
- (keyType Location. none)
1639
- else if isOptional then
1640
- Type. field ~loc ~attrs: optionalAttr {txt = label; loc}
1641
- (Typ. var label)
1642
- else Type. field ~loc {txt = label; loc} (Typ. var label))
1643
- in
1644
- let params =
1645
- makePropsTypeParamsTvar namedTypeList
1646
- |> List. map (fun coreType -> (coreType, Invariant ))
1647
- in
1648
- Sig. type_ Nonrecursive
1649
- [
1650
- Type. mk ~loc ~params {txt = propsName; loc}
1651
- ~kind: (Ptype_record labelDeclList);
1652
- ]
1635
+ Sig. type_ Nonrecursive (makeTypeDecls propsName loc namedTypeList)
1653
1636
1654
1637
let transformUppercaseCall3 ~config modulePath mapper loc attrs callArguments
1655
1638
=
@@ -2279,10 +2262,11 @@ module V4 = struct
2279
2262
(* type props = { ... } *)
2280
2263
let propsRecordType =
2281
2264
makePropsRecordType " props" emptyLoc
2282
- (((true , " key" , [] , keyType emptyLoc) :: namedTypeList)
2283
- @
2284
- if hasForwardRef then [(true , " ref" , [] , refType Location. none)]
2285
- else [] )
2265
+ ([(true , " key" , [] , keyType emptyLoc)]
2266
+ @ (if hasForwardRef then
2267
+ [(true , " ref" , [] , refType Location. none)]
2268
+ else [] )
2269
+ @ namedTypeList)
2286
2270
in
2287
2271
let innerExpression =
2288
2272
Exp. apply
@@ -2459,12 +2443,20 @@ module V4 = struct
2459
2443
match List. filter hasAttr pval_attributes with
2460
2444
| [] -> [item]
2461
2445
| [_] ->
2446
+ let hasForwardRef = ref false in
2462
2447
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType ) =
2463
2448
match ptyp_desc with
2464
2449
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
2465
2450
when isOptional name || isLabelled name ->
2466
2451
getPropTypes ((name, ptyp_loc, type_) :: types) rest
2467
- | Ptyp_arrow (Nolabel, _type , rest ) -> getPropTypes types rest
2452
+ | Ptyp_arrow
2453
+ ( Nolabel ,
2454
+ {ptyp_desc = Ptyp_constr ({txt = Lident " unit" }, _)},
2455
+ rest ) ->
2456
+ getPropTypes types rest
2457
+ | Ptyp_arrow (Nolabel, _type , rest ) ->
2458
+ hasForwardRef := true ;
2459
+ getPropTypes types rest
2468
2460
| Ptyp_arrow (name, type_, returnValue)
2469
2461
when isOptional name || isLabelled name ->
2470
2462
(returnValue, (name, returnValue.ptyp_loc, type_) :: types)
@@ -2479,7 +2471,11 @@ module V4 = struct
2479
2471
in
2480
2472
let propsRecordType =
2481
2473
makePropsRecordTypeSig " props" Location. none
2482
- ((true , " key" , [] , keyType Location. none) :: namedTypeList)
2474
+ ([(true , " key" , [] , keyType Location. none)]
2475
+ (* If there is Nolabel arg, regard the type as ref in forwardRef *)
2476
+ @ (if ! hasForwardRef then [(true , " ref" , [] , refType Location. none)]
2477
+ else [] )
2478
+ @ namedTypeList)
2483
2479
in
2484
2480
(* can't be an arrow because it will defensively uncurry *)
2485
2481
let newExternalType =
0 commit comments