Skip to content

Commit

Permalink
Merge pull request #178 from ml-in-barcelona/Prefix-option-array-with…
Browse files Browse the repository at this point in the history
…-stdlib
  • Loading branch information
davesnx authored Mar 29, 2023
2 parents afccc69 + 7480416 commit ffec0a8
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 162 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ $(opam_file): dune-project ## Update the package dependencies when new deps are

init: ## Create a local opam switch and setups githooks
git config core.hooksPath .githooks
opam switch create . --deps-only --with-test
opam switch create . 4.12.1 --deps-only --with-test
7 changes: 3 additions & 4 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
(dune (and (>= 2) (< 4)))
(ocaml (and (>= 4.12.0) (< 5.0.0)))

(js_of_ocaml (and (>= 4.0.0) (<= 5.0.0)))
(gen_js_api (and (>= 1.0.8) (< 1.1.0)))
(js_of_ocaml (and (>= 4.0.0) (< 5.2.0)))
(gen_js_api (and (>= 1.0.8) (< 1.2.0)))
(ppxlib (>= 0.23.0))

;; Test dependencies
Expand All @@ -35,10 +35,9 @@

;; Dev dependencies, using with-test so that consumers don't install them (until package is released in opam)
(ocamlformat (and (= 0.21.0) :with-test))
(reason :with-test)
(reason (and (= 3.8.2) :with-test))

;; Example dependencies, using with-test so that consumers don't install them (until package is released in opam)
(ppx_blob :with-test)
(js_of_ocaml-lwt :with-test)

))
2 changes: 1 addition & 1 deletion jsoo-react.opam
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ depends: [
"js_of_ocaml-ppx" {with-test}
"conf-npm" {with-test}
"ocamlformat" {= "0.21.0" & with-test}
"reason" {with-test}
"reason" {= "3.8.2" & with-test}
"ppx_blob" {with-test}
"js_of_ocaml-lwt" {with-test}
"odoc" {with-doc}
Expand Down
13 changes: 8 additions & 5 deletions ppx/ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ let rec make_funs_for_make_props_body list args =
let makeAttributeValue ~loc ~isOptional (type_ : Html.attributeType) value =
match (type_, isOptional) with
| String, true ->
[%expr Option.map Js_of_ocaml.Js.string ([%e value] : string option)]
[%expr
Stdlib.Option.map Js_of_ocaml.Js.string ([%e value] : string option)]
| String, false -> [%expr Js_of_ocaml.Js.string ([%e value] : string)]
| Int, false -> [%expr ([%e value] : int)]
| Int, true -> [%expr ([%e value] : int option)]
Expand Down Expand Up @@ -340,7 +341,7 @@ let make_js_props_obj ~loc named_arg_list_with_key_and_ref =
[%e Exp.constant ~loc (Const.string label_str)]
, inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string [%e id]))]
(Stdlib.Option.map Js_of_ocaml.Js.string [%e id]))]
| "ref" ->
[%expr
[%e Exp.constant ~loc (Const.string label_str)]
Expand All @@ -359,14 +360,16 @@ let make_js_props_obj ~loc named_arg_list_with_key_and_ref =
let rec get_wrap_fn_for_type ~loc typ =
match typ with
| Some [%type: React.element list] ->
Some [%expr fun v -> Js_of_ocaml.Js.array (Array.of_list v)]
Some [%expr fun v -> Js_of_ocaml.Js.array (Stdlib.Array.of_list v)]
| Some [%type: string] -> Some [%expr Js_of_ocaml.Js.string]
| Some [%type: bool] -> Some [%expr Js_of_ocaml.Js.bool]
| Some [%type: [%t? inner_typ] array] ->
Some
(match get_wrap_fn_for_type ~loc (Some inner_typ) with
| Some inner_wrapf ->
[%expr fun v -> Js_of_ocaml.Js.array (Array.map [%e inner_wrapf] v)]
[%expr
fun v ->
Js_of_ocaml.Js.array (Stdlib.Array.map [%e inner_wrapf] v)]
| None -> [%expr Js_of_ocaml.Js.array])
| _ -> None

Expand All @@ -388,7 +391,7 @@ let make_external_js_props_obj ~loc named_arg_list =
[%e Exp.constant ~loc (Const.string l)]
, inject
(Js_of_ocaml.Js.Optdef.option
(Option.map [%e wrapf] [%e label_ident]))]
(Stdlib.Option.map [%e wrapf] [%e label_ident]))]
| Optional l, None ->
[%expr
[%e Exp.constant ~loc (Const.string l)]
Expand Down
89 changes: 28 additions & 61 deletions ppx/test/pp_ocaml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("name",
(
inject
name))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("name", (inject name))|][@@merlin.hide ] in
let make =
((fun ?(name= "") ->
(div [||] [React.string ("Hello " ^ name)] : React.element))
Expand Down Expand Up @@ -59,11 +56,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("children",
(
inject
children))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("children", (inject children))|][@@merlin.hide ] in
let make =
((fun ~children:(first, second) ->
(div [||] [first; second] : React.element))
Expand Down Expand Up @@ -105,11 +99,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("children",
(
inject
children))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("children", (inject children))|][@@merlin.hide ] in
let make = ((fun ~children:kids -> (div [||] kids : React.element))
[@warning "-16"]) in
((let make
Expand Down Expand Up @@ -149,11 +140,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("children",
(
inject
children))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("children", (inject children))|][@@merlin.hide ] in
let make =
((fun ~children:(first, second) ->
fun () -> (div [||] [first; second] : React.element))
Expand Down Expand Up @@ -195,11 +183,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("name",
(
inject
name))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("name", (inject name))|][@@merlin.hide ] in
let make = ((fun ?(name= "") -> (div [||] [name] : React.element))
[@warning "-16"]) in
((let make
Expand Down Expand Up @@ -229,8 +214,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))))|][@@merlin.hide
] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))))|]
[@@merlin.hide ] in
let make () = (div [||] [] : React.element) in
((let make (Props : < > Js_of_ocaml.Js.t) = make () in
fun ?key -> fun () -> React.create_element make (make_props ?key ()))
Expand All @@ -249,11 +234,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("foo",
(
inject
foo))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("foo", (inject foo))|][@@merlin.hide ] in
let make (type a) ~foo:(foo : a) = (div [||] [] : _) in
((let make
(Props : < foo: a Js_of_ocaml.Js.readonly_prop > Js_of_ocaml.Js.t) =
Expand Down Expand Up @@ -285,11 +267,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("foo",
(
inject
foo))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("foo", (inject foo))|][@@merlin.hide ] in
let make (type a) = (fun ~foo:_ -> div [||] [] : foo:a -> _) in
((let make
(Props :
Expand Down Expand Up @@ -324,11 +303,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("bar",
(
inject
bar))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("bar", (inject bar))|][@@merlin.hide ] in
let make =
((fun ~bar:(bar : int option) ->
(div [||]
Expand Down Expand Up @@ -368,11 +344,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("name",
(
inject
name))|]
[@@merlin.hide ] in
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("name", (inject name))|][@@merlin.hide ] in
fun ~name ->
fun ?key ->
fun () ->
Expand All @@ -396,11 +369,8 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("name",
(
inject
(Js_of_ocaml.Js.Optdef.option
name)))|]
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("name", (inject (Js_of_ocaml.Js.Optdef.option name)))|]
[@@merlin.hide ] in
fun ?name ->
fun ?key ->
Expand All @@ -425,15 +395,12 @@ let make =
[|("key",
(inject
(Js_of_ocaml.Js.Optdef.option
(Option.map Js_of_ocaml.Js.string key))));("names",
(
inject
((fun v
->
Js_of_ocaml.Js.array
(Array.map
Js_of_ocaml.Js.string
v)) names)))|]
(Stdlib.Option.map Js_of_ocaml.Js.string key))));
("names",
(inject
((fun v ->
Js_of_ocaml.Js.array
(Stdlib.Array.map Js_of_ocaml.Js.string v)) names)))|]
[@@merlin.hide ] in
fun ~names ->
fun ?key ->
Expand Down
Loading

0 comments on commit ffec0a8

Please sign in to comment.