Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

Commit 8c62db0

Browse files
committed
change order of fields in props type record
more clean up for forwardRef
1 parent f4cb119 commit 8c62db0

File tree

2 files changed

+46
-50
lines changed

2 files changed

+46
-50
lines changed

cli/reactjs_jsx_ppx.ml

Lines changed: 44 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1600,56 +1600,39 @@ module V4 = struct
16001600
|> List.filter_map (fun (_isOptional, label, _, interiorType) ->
16011601
if label = "key" || label = "ref" then None else Some interiorType)
16021602

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
16201619
(* 'id, 'className, ... *)
16211620
let params =
16221621
makePropsTypeParamsTvar namedTypeList
16231622
|> List.map (fun coreType -> (coreType, Invariant))
16241623
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)
16301632

16311633
(* type props<'id, 'name, ...> = { @optional key: string, @optional id: 'id, ... } *)
16321634
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)
16531636

16541637
let transformUppercaseCall3 ~config modulePath mapper loc attrs callArguments
16551638
=
@@ -2279,10 +2262,11 @@ module V4 = struct
22792262
(* type props = { ... } *)
22802263
let propsRecordType =
22812264
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)
22862270
in
22872271
let innerExpression =
22882272
Exp.apply
@@ -2459,12 +2443,20 @@ module V4 = struct
24592443
match List.filter hasAttr pval_attributes with
24602444
| [] -> [item]
24612445
| [_] ->
2446+
let hasForwardRef = ref false in
24622447
let rec getPropTypes types ({ptyp_loc; ptyp_desc} as fullType) =
24632448
match ptyp_desc with
24642449
| Ptyp_arrow (name, type_, ({ptyp_desc = Ptyp_arrow _} as rest))
24652450
when isOptional name || isLabelled name ->
24662451
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
24682460
| Ptyp_arrow (name, type_, returnValue)
24692461
when isOptional name || isLabelled name ->
24702462
(returnValue, (name, returnValue.ptyp_loc, type_) :: types)
@@ -2479,7 +2471,11 @@ module V4 = struct
24792471
in
24802472
let propsRecordType =
24812473
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)
24832479
in
24842480
(* can't be an arrow because it will defensively uncurry *)
24852481
let newExternalType =

tests/ppx/react/expected/forwardRef.res.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ let make = {
6565
module FancyInput = {
6666
type props<'className, 'children> = {
6767
key?: string,
68+
ref?: ReactDOM.Ref.currentDomRef,
6869
className?: 'className,
6970
children: 'children,
70-
ref?: ReactDOM.Ref.currentDomRef,
7171
}
7272

7373
@react.component
@@ -119,9 +119,9 @@ let make = {
119119
module FancyInput = {
120120
type props<'className, 'children> = {
121121
key?: string,
122+
ref?: ReactDOM.Ref.currentDomRef,
122123
className?: 'className,
123124
children: 'children,
124-
ref?: ReactDOM.Ref.currentDomRef,
125125
}
126126

127127
@react.component

0 commit comments

Comments
 (0)