Skip to content

Commit

Permalink
browser_only don't break on multiple args
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Jul 25, 2024
1 parent 088f9ce commit eb7eea7
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/browser-ppx/ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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); _ ])
Expand Down
49 changes: 49 additions & 0 deletions packages/browser-ppx/tests/use_effect.t
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,52 @@ Without -js flag, we add the browser_only transformation and browser_only applie
Runtime.fail_impossible_action_in_ssr "<unkwnown>");
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),
> );
>
> <div />;
> };
> 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 "<unkwnown>")
(focusedEntryText, delayInMs);
div ~children:[] () [@JSX]
[@@react.component]

0 comments on commit eb7eea7

Please sign in to comment.