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

Commit 1165667

Browse files
committed
clean up transforming forwardRef
1 parent 44e0d10 commit 1165667

File tree

2 files changed

+36
-77
lines changed

2 files changed

+36
-77
lines changed

cli/reactjs_jsx_ppx.ml

Lines changed: 32 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,20 +2256,6 @@ module V4 = struct
22562256
(argToType ~newtypes ~typeConstraints)
22572257
[] namedArgList
22582258
in
2259-
(* let _ = ref *)
2260-
let vbIgnoreUnusedRef =
2261-
Vb.mk (Pat.any ()) (Exp.ident (Location.mknoloc (Lident "ref")))
2262-
in
2263-
(* let ref = ref->Js.Nullable.fromOption *)
2264-
let vbRefFromOption =
2265-
Vb.mk
2266-
(Pat.var @@ Location.mknoloc "ref")
2267-
(Exp.apply
2268-
(Exp.ident
2269-
(Location.mknoloc
2270-
(Ldot (Ldot (Lident "Js", "Nullable"), "fromOption"))))
2271-
[(Nolabel, Exp.ident (Location.mknoloc @@ Lident "ref"))])
2272-
in
22732259
let namedArgWithDefaultValueList =
22742260
List.filter_map argWithDefaultValue namedArgList
22752261
in
@@ -2299,30 +2285,14 @@ module V4 = struct
22992285
else [])
23002286
in
23012287
let innerExpression =
2302-
if hasForwardRef then
2303-
Exp.apply
2304-
(Exp.ident @@ Location.mknoloc @@ Lident "make")
2305-
[
2306-
( Nolabel,
2307-
Exp.record
2308-
[
2309-
( Location.mknoloc @@ Lident "ref",
2310-
Exp.apply ~attrs:optionalAttr
2311-
(Exp.ident
2312-
(Location.mknoloc
2313-
(Ldot
2314-
(Ldot (Lident "Js", "Nullable"), "toOption"))))
2315-
[
2316-
( Nolabel,
2317-
Exp.ident (Location.mknoloc @@ Lident "ref") );
2318-
] );
2319-
]
2320-
(Some (Exp.ident (Location.mknoloc @@ Lident "props"))) );
2321-
]
2322-
else
2323-
Exp.apply
2324-
(Exp.ident (Location.mknoloc @@ Lident "make"))
2325-
[(Nolabel, Exp.ident (Location.mknoloc @@ Lident "props"))]
2288+
Exp.apply
2289+
(Exp.ident (Location.mknoloc @@ Lident "make"))
2290+
([(Nolabel, Exp.ident (Location.mknoloc @@ Lident "props"))]
2291+
@
2292+
match hasForwardRef with
2293+
| true ->
2294+
[(Nolabel, Exp.ident (Location.mknoloc @@ Lident "ref"))]
2295+
| false -> [])
23262296
in
23272297
let fullExpression =
23282298
(* React component name should start with uppercase letter *)
@@ -2355,10 +2325,13 @@ module V4 = struct
23552325
]
23562326
(Exp.ident ~loc:emptyLoc {loc = emptyLoc; txt = Lident txt})
23572327
in
2358-
let rec returnedExpression patterns ({pexp_desc} as expr) =
2328+
let rec returnedExpression patternsWithLabel patternsWithNolabel
2329+
({pexp_desc} as expr) =
23592330
match pexp_desc with
2360-
| Pexp_newtype (_, expr) -> returnedExpression patterns expr
2361-
| Pexp_constraint (expr, _) -> returnedExpression patterns expr
2331+
| Pexp_newtype (_, expr) ->
2332+
returnedExpression patternsWithLabel patternsWithNolabel expr
2333+
| Pexp_constraint (expr, _) ->
2334+
returnedExpression patternsWithLabel patternsWithNolabel expr
23622335
| Pexp_fun
23632336
( _arg_label,
23642337
_default,
@@ -2367,7 +2340,7 @@ module V4 = struct
23672340
Ppat_construct ({txt = Lident "()"}, _) | Ppat_any;
23682341
},
23692342
expr ) ->
2370-
(patterns, expr)
2343+
(patternsWithLabel, patternsWithNolabel, expr)
23712344
| Pexp_fun (arg_label, _default, {ppat_loc; ppat_desc}, expr) -> (
23722345
if isLabelled arg_label || isOptional arg_label then
23732346
returnedExpression
@@ -2376,43 +2349,39 @@ module V4 = struct
23762349
~attrs:
23772350
(if isOptional arg_label then optionalAttr else [])
23782351
{txt = getLabel arg_label; loc = ppat_loc} )
2379-
:: patterns)
2380-
expr
2352+
:: patternsWithLabel)
2353+
patternsWithNolabel expr
23812354
else
23822355
(* Special case of nolabel arg "ref" in forwardRef fn *)
23832356
(* let make = React.forwardRef(ref => body) *)
23842357
match ppat_desc with
2385-
| Ppat_var {txt}
2386-
| Ppat_constraint ({ppat_desc = Ppat_var {txt}}, _)
2387-
when txt = "ref" ->
2388-
returnedExpression
2358+
| Ppat_var {txt} ->
2359+
returnedExpression patternsWithLabel
23892360
(( {loc = ppat_loc; txt = Lident txt},
23902361
Pat.var ~attrs:optionalAttr {txt; loc = ppat_loc} )
2391-
:: patterns)
2362+
:: patternsWithNolabel)
23922363
expr
2393-
| _ -> returnedExpression patterns expr)
2394-
| _ -> (patterns, expr)
2364+
| _ ->
2365+
returnedExpression patternsWithLabel patternsWithNolabel expr)
2366+
| _ -> (patternsWithLabel, patternsWithNolabel, expr)
2367+
in
2368+
let patternsWithLabel, patternsWithNolabel, expression =
2369+
returnedExpression [] [] expression
23952370
in
2396-
let patternsWithLid, expression = returnedExpression [] expression in
23972371
let pattern =
2398-
match patternsWithLid with
2372+
match patternsWithLabel with
23992373
| [] -> Pat.any ()
2400-
| _ -> Pat.record (List.rev patternsWithLid) Open
2374+
| _ -> Pat.record (List.rev patternsWithLabel) Open
24012375
in
2402-
(* add patttern matching for optional prop value *)
2376+
(* add pattern matching for optional prop value *)
24032377
let expression =
24042378
if List.length vbMatchList = 0 then expression
24052379
else Exp.let_ Nonrecursive vbMatchList expression
24062380
in
2407-
(* add let _ = ref to ignore unused warning *)
24082381
let expression =
2409-
match hasForwardRef with
2410-
| true ->
2411-
let expression =
2412-
Exp.let_ Nonrecursive [vbIgnoreUnusedRef] expression
2413-
in
2414-
Exp.let_ Nonrecursive [vbRefFromOption] expression
2415-
| false -> expression
2382+
List.fold_left
2383+
(fun expr (_, pattern) -> Exp.fun_ Nolabel None pattern expr)
2384+
expression patternsWithNolabel
24162385
in
24172386
let expression =
24182387
Exp.fun_ Nolabel None

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

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ module FancyInput = {
7171
}
7272

7373
@react.component
74-
let make = ({?className, children, ?ref, _}: props<'className, 'children>) => {
75-
let ref = Js.Nullable.fromOption(ref)
76-
let _ = ref
77-
74+
let make = ({?className, children, _}: props<'className, 'children>, ref) =>
7875
ReactDOMRe.createDOMElementVariadic(
7976
"div",
8077
[
@@ -91,10 +88,8 @@ module FancyInput = {
9188
children,
9289
],
9390
)
94-
}
9591
let make = React.forwardRef({
96-
let \"ForwardRef$FancyInput" = (props: props<_>, ref) =>
97-
make({...props, ref: ?Js.Nullable.toOption(ref)})
92+
let \"ForwardRef$FancyInput" = (props: props<_>, ref) => make(props, ref)
9893
\"ForwardRef$FancyInput"
9994
})
10095
}
@@ -130,10 +125,7 @@ module FancyInput = {
130125
}
131126

132127
@react.component
133-
let make = ({?className, children, ?ref, _}: props<'className, 'children>) => {
134-
let ref = Js.Nullable.fromOption(ref)
135-
let _ = ref
136-
128+
let make = ({?className, children, _}: props<'className, 'children>, ref) =>
137129
ReactDOM.jsxs(
138130
"div",
139131
{
@@ -150,10 +142,8 @@ module FancyInput = {
150142
]),
151143
},
152144
)
153-
}
154145
let make = React.forwardRef({
155-
let \"ForwardRef$FancyInput" = (props: props<_>, ref) =>
156-
make({...props, ref: ?Js.Nullable.toOption(ref)})
146+
let \"ForwardRef$FancyInput" = (props: props<_>, ref) => make(props, ref)
157147
\"ForwardRef$FancyInput"
158148
})
159149
}

0 commit comments

Comments
 (0)