Skip to content

Commit

Permalink
browser_only: don't convert to runtime errors on identifiers or funct…
Browse files Browse the repository at this point in the history
…ion application (#138)
  • Loading branch information
jchavarri authored Jun 19, 2024
1 parent 40f33e1 commit 8b900ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
20 changes: 3 additions & 17 deletions packages/browser-ppx/ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ module Browser_only = struct
let error_only_works_on ~loc =
[%expr
[%ocaml.error
"[browser_ppx] browser_only works on function definitions or values. \
If there's another case where it can be helpful, feel free to open an \
issue in https://github.com/ml-in-barcelona/server-reason-react."]]
"[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."]]

let remove_alert_browser_only ~loc =
Builder.attribute ~loc ~name:{ txt = "alert"; loc }
Expand Down Expand Up @@ -135,14 +135,6 @@ module Browser_only = struct
in
let vb = Builder.value_binding ~loc ~pat:pattern ~expr in
{ vb with pvb_attributes = [ remove_alert_browser_only ~loc ] }
| Pexp_ident { txt = longident; loc } ->
let stringified = Ppxlib.Longident.name longident in
let message = Builder.estring ~loc stringified in
let vb =
Builder.value_binding ~loc ~pat:pattern
~expr:[%expr Runtime.fail_impossible_action_in_ssr [%e message]]
in
{ vb with pvb_attributes = [ remove_alert_browser_only ~loc ] }
| _ ->
Builder.value_binding ~loc ~pat:pattern
~expr:(error_only_works_on ~loc))
Expand All @@ -155,12 +147,6 @@ module Browser_only = struct
| Js -> payload
| Native -> (
match payload.pexp_desc with
| Pexp_apply (expression, _) ->
let stringified =
Ppxlib.Pprintast.string_of_expression expression
in
let message = Builder.estring ~loc stringified in
[%expr Runtime.fail_impossible_action_in_ssr [%e message]]
| Pexp_constraint
( {
pexp_desc =
Expand Down
14 changes: 10 additions & 4 deletions packages/browser-ppx/tests/pexp_apply.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,21 @@ With -js flag everything keeps as it is and browser_only extension disappears
let make () =
let pstr_value_binding_2 = Webapi.Dom.getElementById "foo" 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 pstr_value_binding =
Runtime.fail_impossible_action_in_ssr "Webapi.Dom.getElementById"
[%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."]

let make () =
let pstr_value_binding_2 =
[%ocaml.error
"[browser_ppx] browser_only works on function definitions or values. If \
there's another case where it can be helpful, feel free to open an \
issue in https://github.com/ml-in-barcelona/server-reason-react."]
"[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
()
8 changes: 5 additions & 3 deletions packages/browser-ppx/tests/pexp_ident.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ With -js flag everything keeps as it is and browser_only extension disappears
let pexp_ident = Webapi__Dom__Element.asHtmlElement in
()

Without -js flag, the compilation to native replaces the expression with `Runtime.fail_impossible_action_in_ssr`
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 make () =
let pexp_ident =
Runtime.fail_impossible_action_in_ssr "Webapi__Dom__Element.asHtmlElement"
[@@alert "-browser_only"]
[%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 8b900ef

Please sign in to comment.