Skip to content

Commit

Permalink
Allow 2nd arg
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Jul 25, 2024
1 parent eb7eea7 commit 8f12df7
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 3 deletions.
9 changes: 8 additions & 1 deletion packages/browser-ppx/ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 45 additions & 2 deletions packages/browser-ppx/tests/use_effect.t
Original file line number Diff line number Diff line change
Expand Up @@ -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 "<unkwnown>");
React.useEffect2
(fun () -> Runtime.fail_impossible_action_in_ssr "<unkwnown>")
(uiState, newPassword);
div ~children:[] () [@JSX]
[@@react.component]

Expand Down Expand Up @@ -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|],
> );
>
> <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.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 "<unkwnown>")
[| isFocused |];
div ~children:[] () [@JSX]
[@@react.component]

0 comments on commit 8f12df7

Please sign in to comment.