From 16fac6cfec3c711e06006ef6efbd23e1fbbdfacd Mon Sep 17 00:00:00 2001 From: David Sancho Moreno Date: Wed, 2 Aug 2023 17:51:10 +0200 Subject: [PATCH] Fix ppx for uppercase components --- packages/server-reason-react-ppx/ppx.ml | 8 ++- .../test_snapshot/reason.expected | 68 ++++++++++++------- 2 files changed, 50 insertions(+), 26 deletions(-) diff --git a/packages/server-reason-react-ppx/ppx.ml b/packages/server-reason-react-ppx/ppx.ml index 106d4e8ce..bcbcf3198 100644 --- a/packages/server-reason-react-ppx/ppx.ml +++ b/packages/server-reason-react-ppx/ppx.ml @@ -1059,7 +1059,13 @@ let jsxMapper () = (Invalid_argument "JSX name can't be the result of function applications") in - Exp.apply ~attrs ~loc (Exp.ident ~loc { loc; txt = makeFnIdentifier }) args + [%expr + React.Upper_case_component + (fun () -> + [%e + Exp.apply ~attrs ~loc + (Exp.ident ~loc { loc; txt = makeFnIdentifier }) + args])] in let transformLowercaseCall ~loc mapper attrs callArguments id callLoc = let children, nonChildrenProps = diff --git a/packages/server-reason-react-ppx/test_snapshot/reason.expected b/packages/server-reason-react-ppx/test_snapshot/reason.expected index 57e99226b..cf59986e4 100644 --- a/packages/server-reason-react-ppx/test_snapshot/reason.expected +++ b/packages/server-reason-react-ppx/test_snapshot/reason.expected @@ -65,7 +65,7 @@ let lower_children_multiple foo bar = let lower_child_with_upper_as_children = React.createElement "div" ((([||] |> Array.to_list) |> (List.filter_map (fun a -> a))) |> - Array.of_list) [App.make ()] + Array.of_list) [React.Upper_case_component ((fun () -> App.make ()))] let lower_children_nested = React.createElement "div" ((([|(Some @@ -146,31 +146,45 @@ let fragment_as_a_child = ((([||] |> Array.to_list) |> (List.filter_map (fun a -> a))) |> Array.of_list) []]) ()] -let upper = Upper.make () -let upper_prop = Upper.make ~count () -let upper_children_single foo = Upper.make ~children:foo () +let upper = React.Upper_case_component (fun () -> Upper.make ()) +let upper_prop = React.Upper_case_component (fun () -> Upper.make ~count ()) +let upper_children_single foo = + React.Upper_case_component (fun () -> Upper.make ~children:foo ()) let upper_children_multiple foo bar = - Upper.make ~children:(React.list [foo; bar]) () + React.Upper_case_component + (fun () -> Upper.make ~children:(React.list [foo; bar]) ()) let upper_children = - Page.make ~moreProps:"hgalo" - ~children:(React.createElement "h1" - ((([||] |> Array.to_list) |> (List.filter_map (fun a -> a))) - |> Array.of_list) [React.string "Yep"]) () -let upper_nested_module = Foo.Bar.make ~a:1 ~b:"1" () -let upper_child_expr = Div.make ~children:(React.int 1) () -let upper_child_ident = Div.make ~children:lola () + React.Upper_case_component + (fun () -> + Page.make ~moreProps:"hgalo" + ~children:(React.createElement "h1" + ((([||] |> Array.to_list) |> + (List.filter_map (fun a -> a))) + |> Array.of_list) [React.string "Yep"]) ()) +let upper_nested_module = + React.Upper_case_component (fun () -> Foo.Bar.make ~a:1 ~b:"1" ()) +let upper_child_expr = + React.Upper_case_component (fun () -> Div.make ~children:(React.int 1) ()) +let upper_child_ident = + React.Upper_case_component (fun () -> Div.make ~children:lola ()) let upper_all_kinds_of_props = - MyComponent.make ~booleanAttribute:true ~stringAttribute:"string" - ~intAttribute:1 ?forcedOptional:((Some "hello")[@explicit_arity ]) - ~onClick:(send handleClick) - ~children:(React.createElement "div" - ((([||] |> Array.to_list) |> (List.filter_map (fun a -> a))) - |> Array.of_list) ["hello"]) () + React.Upper_case_component + (fun () -> + MyComponent.make ~booleanAttribute:true ~stringAttribute:"string" + ~intAttribute:1 ?forcedOptional:((Some "hello")[@explicit_arity ]) + ~onClick:(send handleClick) + ~children:(React.createElement "div" + ((([||] |> Array.to_list) |> + (List.filter_map (fun a -> a))) + |> Array.of_list) ["hello"]) ()) let upper_ref_with_children = - FancyButton.make ~ref:buttonRef - ~children:(React.createElement "div" - ((([||] |> Array.to_list) |> (List.filter_map (fun a -> a))) - |> Array.of_list) []) () + React.Upper_case_component + (fun () -> + FancyButton.make ~ref:buttonRef + ~children:(React.createElement "div" + ((([||] |> Array.to_list) |> + (List.filter_map (fun a -> a))) + |> Array.of_list) []) ()) let lower_ref_with_children = React.createElement "button" ((([|(Some (React.Attribute.Ref ref));(Some @@ -242,7 +256,8 @@ module React_component_with_props = [@warning "-16"][@warning "-16"]) end let react_component_with_props = - React_component_with_props.make ~lola:"flores" () + React.Upper_case_component + (fun () -> React_component_with_props.make ~lola:"flores" ()) module Upper_case_with_fragment_as_root = struct let make ?key = @@ -255,8 +270,11 @@ module Upper_case_with_fragment_as_root = (List.filter_map (fun a -> a))) |> Array.of_list) [React.string ("First " ^ name)]; - Hello.make ~one:"1" - ~children:(React.string ("2nd " ^ name)) ()]) ()) + React.Upper_case_component + ((fun () -> + Hello.make ~one:"1" + ~children:(React.string ("2nd " ^ name)) + ()))]) ()) [@warning "-16"][@warning "-16"]) end module Using_React_memo =