Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow memoCustomCompareProps on ppx #766

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion ppx/src/reactjs_jsx_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -795,6 +795,16 @@ let jsxMapper =
(_wrapperExpression, [ (Nolabel, innerFunctionExpression) ]);
} ->
spelunkForFunExpression innerFunctionExpression
(* let make = React.memoCustomCompareProps(
(~prop) => ...,
(prevProps, nextProps) => false
) *)
| {
pexp_desc =
Pexp_apply
(_wrapperExpression, [ (Nolabel, innerFunctionExpression); _ ]);
} ->
spelunkForFunExpression innerFunctionExpression
| {
pexp_desc =
Pexp_sequence (_wrapperExpression, innerFunctionExpression);
Expand All @@ -805,7 +815,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
Expand Down
32 changes: 16 additions & 16 deletions ppx/test/input.re
Original file line number Diff line number Diff line change
Expand Up @@ -108,22 +108,22 @@ module Upper_case_with_fragment_as_root = {
</>;
};

/* module Using_React_memo = {
[@react.component]
let make =
React.memo((~a) =>
<div> {Printf.sprintf("`a` is %s", a) |> React.string} </div>
);
}; */

/* module Using_memo_custom_compare_Props = {
[@react.component]
let make =
React.memo(
(~a) => <div> {Printf.sprintf("`a` is %d", a) |> React.string} </div>,
(prevPros, nextProps) => false,
);
}; */
module Using_React_memo = {
[@react.component]
let make =
React.memo((~a) =>
<div> {Printf.sprintf("`a` is %s", a) |> React.string} </div>
);
};

module Using_memo_custom_compare_Props = {
[@react.component]
let make =
React.memoCustomCompareProps(
(~a) => <div> {Printf.sprintf("`a` is %d", a) |> React.string} </div>,
(prevPros, nextProps) => false,
);
};

module Forward_Ref = {
[@react.component]
Expand Down
32 changes: 32 additions & 0 deletions ppx/test/output.expected
Original file line number Diff line number Diff line change
Expand Up @@ -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.memoCustomCompareProps
(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 :
Expand Down
Loading