diff --git a/packages/browser-ppx/ppx.ml b/packages/browser-ppx/ppx.ml index ab78a58f0..0133c7781 100644 --- a/packages/browser-ppx/ppx.ml +++ b/packages/browser-ppx/ppx.ml @@ -289,6 +289,7 @@ module Browser_only = struct let add_browser_only_extension expr = match expr.pexp_desc with | Pexp_apply (_, [ (Nolabel, effect_body) ]) + | Pexp_apply (_, [ (Nolabel, effect_body); _ ]) when has_browser_only_attribute effect_body -> None | Pexp_apply (apply_expr, [ (Nolabel, effect_body); _ ]) diff --git a/packages/browser-ppx/tests/use_effect.t b/packages/browser-ppx/tests/use_effect.t index c842cb897..2148a6d05 100644 --- a/packages/browser-ppx/tests/use_effect.t +++ b/packages/browser-ppx/tests/use_effect.t @@ -74,3 +74,52 @@ Without -js flag, we add the browser_only transformation and browser_only applie Runtime.fail_impossible_action_in_ssr ""); div ~children:[] () [@JSX] [@@react.component] + + $ cat > input.re << EOF + > [@react.component] + > let make = () => { + > let (state, dispatch) = React.useReducer(reducer, initialState); + > + > React.useEffect2( + > [%browser_only + > () => { + > let handler = Js.Global.setTimeout(~f=_ => setDebouncedValue(focusedEntryText), delayInMs); + > Some(_ => Js.Global.clearTimeout(handler)); + > } + > ], + > (focusedEntryText, delayInMs), + > ); + > + >
; + > }; + > 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.useEffect2 + (fun () -> + let handler = + Js.Global.setTimeout + ~f:(fun _ -> setDebouncedValue focusedEntryText) + delayInMs + in + (Some (fun _ -> Js.Global.clearTimeout handler) [@explicit_arity])) + (focusedEntryText, delayInMs); + 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.useEffect2 + (fun () -> Runtime.fail_impossible_action_in_ssr "") + (focusedEntryText, delayInMs); + div ~children:[] () [@JSX] + [@@react.component]