From 8f12df72662d097ec62b38e2e3493ff4f05b3818 Mon Sep 17 00:00:00 2001 From: David Sancho Moreno Date: Thu, 25 Jul 2024 17:40:07 +0200 Subject: [PATCH] Allow 2nd arg --- packages/browser-ppx/ppx.ml | 9 ++++- packages/browser-ppx/tests/use_effect.t | 47 +++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/packages/browser-ppx/ppx.ml b/packages/browser-ppx/ppx.ml index 0133c7781..4dfc9dc6d 100644 --- a/packages/browser-ppx/ppx.ml +++ b/packages/browser-ppx/ppx.ml @@ -292,7 +292,14 @@ module Browser_only = struct | Pexp_apply (_, [ (Nolabel, effect_body); _ ]) when has_browser_only_attribute effect_body -> None - | Pexp_apply (apply_expr, [ (Nolabel, effect_body); _ ]) + | Pexp_apply (apply_expr, [ (Nolabel, effect_body); second_arg ]) -> + let loc = expr.pexp_loc in + let new_effect_body = [%expr [%browser_only [%e effect_body]]] in + let new_effect_fun = + Builder.pexp_apply ~loc apply_expr + [ (Nolabel, new_effect_body); second_arg ] + in + Some new_effect_fun | Pexp_apply (apply_expr, [ (Nolabel, effect_body) ]) -> let loc = expr.pexp_loc in let new_effect_body = [%expr [%browser_only [%e effect_body]]] in diff --git a/packages/browser-ppx/tests/use_effect.t b/packages/browser-ppx/tests/use_effect.t index 2148a6d05..679be1eab 100644 --- a/packages/browser-ppx/tests/use_effect.t +++ b/packages/browser-ppx/tests/use_effect.t @@ -70,8 +70,9 @@ Without -js flag, we add the browser_only transformation and browser_only applie $ ./standalone.exe -impl input.ml | ocamlformat - --enable-outside-detected-project --impl let make () = - React.useEffect2 (fun () -> - Runtime.fail_impossible_action_in_ssr ""); + React.useEffect2 + (fun () -> Runtime.fail_impossible_action_in_ssr "") + (uiState, newPassword); div ~children:[] () [@JSX] [@@react.component] @@ -123,3 +124,45 @@ Without -js flag, we add the browser_only transformation and browser_only applie (focusedEntryText, delayInMs); div ~children:[] () [@JSX] [@@react.component] + $ cat > input.re << EOF + > [@react.component] + > let make = () => { + > let (state, dispatch) = React.useReducer(reducer, initialState); + > + > React.useEffect1( + > () => { + > isFocused ? onFocusedItemChange(domRef) : (); + > None; + > }, + > [|isFocused|], + > ); + > + >
; + > }; + > EOF + + $ refmt --parse re --print ml input.re > input.ml + +With -js flag everything keeps as it is + + $ ./standalone.exe -impl input.ml -js | ocamlformat - --enable-outside-detected-project --impl + let make () = + let state, dispatch = React.useReducer reducer initialState in + React.useEffect1 + (fun () -> + (match isFocused with true -> onFocusedItemChange domRef | false -> ()); + None) + [| isFocused |]; + div ~children:[] () [@JSX] + [@@react.component] + +Without -js flag, we add the browser_only transformation and browser_only applies the transformation to fail_impossible_action_in_ssr + + $ ./standalone.exe -impl input.ml | ocamlformat - --enable-outside-detected-project --impl + let make () = + let state, dispatch = React.useReducer reducer initialState in + React.useEffect1 + (fun () -> Runtime.fail_impossible_action_in_ssr "") + [| isFocused |]; + div ~children:[] () [@JSX] + [@@react.component]