Skip to content

Commit

Permalink
Add browser_only
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Aug 4, 2023
1 parent c0cd394 commit 10afb92
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 6 deletions.
17 changes: 15 additions & 2 deletions packages/ppx/browser.ml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let effect_rule =
match expression.pexp_desc with
| Pexp_apply
( {
(* Ldot (Lident "Prop", str) *)
pexp_desc = Pexp_ident { txt = Ldot (Lident _, "useEffect"); _ };
pexp_loc_stack;
},
Expand All @@ -23,4 +22,18 @@ let effect_rule =
(Extension.V3.declare "effect" Extension.Context.expression extractor
handler)

let () = Driver.V2.register_transformation "browser_ppx" ~rules:[ effect_rule ]
let browser_only_rule =
let extractor = Ast_pattern.(__') in
let handler ~ctxt:_ ({ txt = payload; loc } : Ppxlib.Parsetree.payload loc) =
match payload with
| PStr [ { pstr_desc = Pstr_eval (expression, _); _ } ] ->
[%expr fun () -> raise Browser_only]
| _ -> [%expr [%ocaml.error "effect only accepts a useEffect expression"]]
in
Context_free.Rule.extension
(Extension.V3.declare "browser_only" Extension.Context.expression extractor
handler)

let () =
Driver.V2.register_transformation "browser_ppx"
~rules:[ effect_rule; browser_only_rule ]
1 change: 1 addition & 0 deletions packages/ppx/dune
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
(public_name server-reason-react.browser_ppx)
(flags :standard -w -9)
(libraries ppxlib)
(ppx_runtime_libraries runtime)
(preprocess
(pps ppxlib.metaquot))
(kind ppx_rewriter))
1 change: 1 addition & 0 deletions packages/ppx/runtime.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exception Browser_only
9 changes: 6 additions & 3 deletions packages/ppx/test_snapshot/input.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,15 @@ let flags_global_multiline_insensitive = [%re "/foo/gim"]
let scape_digis_with_global = [%re "/(\\d+)/g"]
let payload_should_be_a_string = [%re apply];;

(* browser *)
(* effect *)

[%effect
React.useEffect
(Js.log "ok";
(None, [||]))]
;;

[%effect None]
let _ = [%effect None]

(* browser_only *)
let _ = [%browser_only Webapi.Dom.getElementById "foo"]
let _ = [%browser_only fun () -> ()]
4 changes: 3 additions & 1 deletion packages/ppx/test_snapshot/output.expected
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,6 @@ let scape_digis_with_global = Js.Re.fromStringWithFlags ~flags:"g" "(\\d+)"
let payload_should_be_a_string =
[%ocaml.error "payload should be a string literal"]
;;React.useEffect (fun () -> None) [||]
;;[%ocaml.error "effect only accepts a useEffect expression"]
let _ = [%ocaml.error "effect only accepts a useEffect expression"]
let _ = fun () -> raise Browser_only
let _ = fun () -> raise Browser_only

0 comments on commit 10afb92

Please sign in to comment.