From 97ba407989c7b2542d53dfbf5495969001f8bcf1 Mon Sep 17 00:00:00 2001 From: David Sancho Moreno Date: Mon, 4 Sep 2023 11:43:41 -0700 Subject: [PATCH 1/2] Allow memoCustomCompareProps on ppx --- ppx/src/reactjs_jsx_ppx.ml | 9 ++++++++- ppx/test/input.re | 32 ++++++++++++++++---------------- ppx/test/output.expected | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 17 deletions(-) diff --git a/ppx/src/reactjs_jsx_ppx.ml b/ppx/src/reactjs_jsx_ppx.ml index 206f8982a..c90cfb585 100644 --- a/ppx/src/reactjs_jsx_ppx.ml +++ b/ppx/src/reactjs_jsx_ppx.ml @@ -795,6 +795,13 @@ let jsxMapper = (_wrapperExpression, [ (Nolabel, innerFunctionExpression) ]); } -> spelunkForFunExpression innerFunctionExpression + (* let make = React.memoCustomCompareProps((~prop) => ...) *) + | { + pexp_desc = + Pexp_apply + (_wrapperExpression, [ (Nolabel, innerFunctionExpression); _ ]); + } -> + spelunkForFunExpression innerFunctionExpression | { pexp_desc = Pexp_sequence (_wrapperExpression, innerFunctionExpression); @@ -805,7 +812,7 @@ let jsxMapper = (Invalid_argument "react.component calls can only be on function \ definitions or component wrappers (forwardRef, \ - memo).") + memo or memoCustomCompareProps).") [@@raises Invalid_argument] in spelunkForFunExpression expression diff --git a/ppx/test/input.re b/ppx/test/input.re index da53f594e..cceb39123 100644 --- a/ppx/test/input.re +++ b/ppx/test/input.re @@ -108,22 +108,22 @@ module Upper_case_with_fragment_as_root = { ; }; -/* module Using_React_memo = { - [@react.component] - let make = - React.memo((~a) => -
{Printf.sprintf("`a` is %s", a) |> React.string}
- ); - }; */ - -/* module Using_memo_custom_compare_Props = { - [@react.component] - let make = - React.memo( - (~a) =>
{Printf.sprintf("`a` is %d", a) |> React.string}
, - (prevPros, nextProps) => false, - ); - }; */ +module Using_React_memo = { + [@react.component] + let make = + React.memo((~a) => +
{Printf.sprintf("`a` is %s", a) |> React.string}
+ ); +}; + +module Using_memo_custom_compare_Props = { + [@react.component] + let make = + React.memo( + (~a) =>
{Printf.sprintf("`a` is %d", a) |> React.string}
, + (prevPros, nextProps) => false, + ); +}; module Forward_Ref = { [@react.component] diff --git a/ppx/test/output.expected b/ppx/test/output.expected index ff730bf2e..276806d76 100644 --- a/ppx/test/output.expected +++ b/ppx/test/output.expected @@ -202,6 +202,38 @@ module Upper_case_with_fragment_as_root = (Props : < name: 'name option > Js.t) = make ?name:(Props ## name) in Generated$Upper_case_with_fragment_as_root end +module Using_React_memo = + struct + external makeProps : + a:'a -> ?key:string -> unit -> < a: 'a > Js.t = ""[@@bs.obj ] + let make = + ((fun ~a -> + ReactDOM.jsx "div" + (((ReactDOM.domProps)[@merlin.hide ]) + ~children:((Printf.sprintf "`a` is %s" a) |> React.string) ())) + [@warning "-16"]) + let make = + React.memo + (let Generated$Using_React_memo (Props : < a: 'a > Js.t) = + make ~a:(Props ## a) in + Generated$Using_React_memo) + end +module Using_memo_custom_compare_Props = + struct + external makeProps : + a:'a -> ?key:string -> unit -> < a: 'a > Js.t = ""[@@bs.obj ] + let make = + React.memo + (fun ~a -> + ReactDOM.jsx "div" + (((ReactDOM.domProps)[@merlin.hide ]) + ~children:((Printf.sprintf "`a` is %d" a) |> React.string) ())) + (fun prevPros -> fun nextProps -> false) + let make = + let Generated$Using_memo_custom_compare_Props + (Props : < a: 'a > Js.t) = make ~a:(Props ## a) in + Generated$Using_memo_custom_compare_Props + end module Forward_Ref = struct external makeProps : From 89b90f12a071f6276b40793245b638a12ae7b46c Mon Sep 17 00:00:00 2001 From: David Sancho Moreno Date: Mon, 4 Sep 2023 15:31:26 -0700 Subject: [PATCH 2/2] Enable test with React.memoCustomCompareProps --- ppx/src/reactjs_jsx_ppx.ml | 5 ++++- ppx/test/input.re | 2 +- ppx/test/output.expected | 2 +- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/ppx/src/reactjs_jsx_ppx.ml b/ppx/src/reactjs_jsx_ppx.ml index c90cfb585..de1700cfd 100644 --- a/ppx/src/reactjs_jsx_ppx.ml +++ b/ppx/src/reactjs_jsx_ppx.ml @@ -795,7 +795,10 @@ let jsxMapper = (_wrapperExpression, [ (Nolabel, innerFunctionExpression) ]); } -> spelunkForFunExpression innerFunctionExpression - (* let make = React.memoCustomCompareProps((~prop) => ...) *) + (* let make = React.memoCustomCompareProps( + (~prop) => ..., + (prevProps, nextProps) => false + ) *) | { pexp_desc = Pexp_apply diff --git a/ppx/test/input.re b/ppx/test/input.re index cceb39123..5a293ca3f 100644 --- a/ppx/test/input.re +++ b/ppx/test/input.re @@ -119,7 +119,7 @@ module Using_React_memo = { module Using_memo_custom_compare_Props = { [@react.component] let make = - React.memo( + React.memoCustomCompareProps( (~a) =>
{Printf.sprintf("`a` is %d", a) |> React.string}
, (prevPros, nextProps) => false, ); diff --git a/ppx/test/output.expected b/ppx/test/output.expected index 276806d76..a746abb4b 100644 --- a/ppx/test/output.expected +++ b/ppx/test/output.expected @@ -223,7 +223,7 @@ module Using_memo_custom_compare_Props = external makeProps : a:'a -> ?key:string -> unit -> < a: 'a > Js.t = ""[@@bs.obj ] let make = - React.memo + React.memoCustomCompareProps (fun ~a -> ReactDOM.jsx "div" (((ReactDOM.domProps)[@merlin.hide ])