Skip to content

Commit

Permalink
Transform Pexp_function with browser_only (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx authored Jul 19, 2024
1 parent 38373ea commit 702a5cd
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/browser-ppx/ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,17 @@ module Browser_only = struct
in
let item = { fn with pexp_attributes = expr.pexp_attributes } in
make_vb_with_browser_only ~loc pattern item
| Pexp_function _cases ->
(* Because pexp_function doesn't have a pattern, neither a label, we construct an empty pattern and use it to generate the vb *)
let original_function_name = get_function_name pattern.ppat_desc in
let fn =
Builder.pexp_fun ~loc Nolabel None
[%pat? _]
(last_expr_to_raise_impossible ~loc original_function_name
expression)
in
let item = { fn with pexp_attributes = expression.pexp_attributes } in
make_vb_with_browser_only ~loc pattern item
| Pexp_ident { txt = _longident; loc } ->
let item = [%expr Obj.magic ()] in
make_vb_with_browser_only ~loc pattern item
Expand Down
46 changes: 46 additions & 0 deletions packages/browser-ppx/tests/pexp_function.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
$ cat > input.re << EOF
> let%browser_only foo = fun
> | x when x < 0. => None
> | x => Some("bar");
>
> let make = () => {
> let%browser_only foo = fun
> | x when x < 0. => None
> | x => Some("bar");
> ();
> };
> EOF

$ refmt --parse re --print ml input.re > input.ml

With -js flag everything keeps as it is and browser_only extension disappears

$ ./standalone.exe -impl input.ml -js | ocamlformat - --enable-outside-detected-project --impl
let foo = function x when x < 0. -> None | x -> Some "bar" [@explicit_arity]

let make () =
let foo = function
| x when x < 0. -> None
| x -> Some "bar" [@explicit_arity]
in
()

Without -js flag, the compilation to native errors out indicating that a function must be used

$ ./standalone.exe -impl input.ml | ocamlformat - --enable-outside-detected-project --impl
let (foo
[@alert
browser_only
"This expression is marked to only run on the browser where JavaScript \
can run. You can only use it inside a let%browser_only function."]) =
fun [@alert "-browser_only"] _ -> Runtime.fail_impossible_action_in_ssr "foo"
[@@warning "-27-32"]

let make () =
let foo =
[%ocaml.error
"[browser_ppx] browser_only works on function definitions. For other \
cases, use switch%platform or feel free to open an issue in \
https://github.com/ml-in-barcelona/server-reason-react."]
in
()

0 comments on commit 702a5cd

Please sign in to comment.