Skip to content

Commit

Permalink
ppx: qualify usages of stdlib infix operators
Browse files Browse the repository at this point in the history
  • Loading branch information
jchavarri committed Aug 26, 2024
1 parent c2dc734 commit 8d4151c
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 70 deletions.
29 changes: 17 additions & 12 deletions ppx/browser/ppx_deriving_json_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ module Of_json = struct

let eis_json_object ~loc x =
[%expr
Js.typeof [%e x] = "object"
&& (not (Js.Array.isArray [%e x]))
&& not ((Obj.magic [%e x] : 'a Js.null) == Js.null)]
Stdlib.( && )
(Js.typeof [%e x] = "object")
(Stdlib.( && )
(not (Js.Array.isArray [%e x]))
(not (Stdlib.( == ) (Obj.magic [%e x] : 'a Js.null) Js.null)))]

let ensure_json_object ~loc x =
[%expr
Expand All @@ -67,7 +69,7 @@ module Of_json = struct

let ensure_json_array_len ~loc n len =
[%expr
if [%e len] <> [%e eint ~loc n] then
if Stdlib.( <> ) [%e len] [%e eint ~loc n] then
Ppx_deriving_json_runtime.of_json_error
[%e
estring ~loc (sprintf "expected a JSON array of length %i" n)]]
Expand All @@ -77,9 +79,11 @@ module Of_json = struct
let n = List.length t.tpl_types in
[%expr
if
Js.Array.isArray [%e x]
&& Js.Array.length (Obj.magic [%e x] : Js.Json.t array)
= [%e eint ~loc n]
Stdlib.( && )
(Js.Array.isArray [%e x])
(Stdlib.( = )
(Js.Array.length (Obj.magic [%e x] : Js.Json.t array))
[%e eint ~loc n])
then
let es = (Obj.magic [%e x] : Js.Json.t array) in
[%e build_tuple ~loc derive 0 t.tpl_types [%expr es]]
Expand Down Expand Up @@ -113,7 +117,7 @@ module Of_json = struct
if Js.Array.isArray [%e x] then
let array = (Obj.magic [%e x] : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
Expand All @@ -135,13 +139,14 @@ module Of_json = struct
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as ctx) in
[%expr
if tag = [%e estring ~loc:n.loc n.txt] then [%e make None]
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then
[%e make None]
else [%e next]]
| Vcs_record (n, r) ->
let loc = n.loc in
let n = Option.value ~default:n (vcs_attr_json_as r.rcd_ctx) in
[%expr
if tag = [%e estring ~loc:n.loc n.txt] then (
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then (
[%e ensure_json_array_len ~loc 2 [%expr len]];
let fs = Js.Array.unsafe_get array 1 in
[%e ensure_json_object ~loc [%expr fs]];
Expand All @@ -154,10 +159,10 @@ module Of_json = struct
let n = Option.value ~default:n (vcs_attr_json_as t.tpl_ctx) in
let arity = List.length t.tpl_types in
[%expr
if tag = [%e estring ~loc:n.loc n.txt] then (
if Stdlib.( = ) tag [%e estring ~loc:n.loc n.txt] then (
[%e ensure_json_array_len ~loc (arity + 1) [%expr len]];
[%e
if arity = 0 then make None
if Stdlib.( = ) arity 0 then make None
else
make
(Some
Expand Down
136 changes: 78 additions & 58 deletions ppx/test/ppx_deriving_json_js.t
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@
let rec tuple_of_json =
(fun x ->
if
Js.Array.isArray x
&& Js.Array.length (Obj.magic x : Js.Json.t array) = 2
Stdlib.( && ) (Js.Array.isArray x)
(Stdlib.( = )
(Js.Array.length (Obj.magic x : Js.Json.t array))
2)
then
let es = (Obj.magic x : Js.Json.t array) in
( int_of_json (Js.Array.unsafe_get es 0),
Expand Down Expand Up @@ -118,9 +120,11 @@
(fun x ->
if
not
(Js.typeof x = "object"
&& (not (Js.Array.isArray x))
&& not ((Obj.magic x : 'a Js.null) == Js.null))
(Stdlib.( && )
(Js.typeof x = "object")
(Stdlib.( && )
(not (Js.Array.isArray x))
(not (Stdlib.( == ) (Obj.magic x : 'a Js.null) Js.null))))
then
Ppx_deriving_json_runtime.of_json_error "expected a JSON object";
let fs =
Expand Down Expand Up @@ -180,9 +184,11 @@
(fun x ->
if
not
(Js.typeof x = "object"
&& (not (Js.Array.isArray x))
&& not ((Obj.magic x : 'a Js.null) == Js.null))
(Stdlib.( && )
(Js.typeof x = "object")
(Stdlib.( && )
(not (Js.Array.isArray x))
(not (Stdlib.( == ) (Obj.magic x : 'a Js.null) Js.null))))
then
Ppx_deriving_json_runtime.of_json_error "expected a JSON object";
let fs =
Expand Down Expand Up @@ -239,9 +245,11 @@
(fun x ->
if
not
(Js.typeof x = "object"
&& (not (Js.Array.isArray x))
&& not ((Obj.magic x : 'a Js.null) == Js.null))
(Stdlib.( && )
(Js.typeof x = "object")
(Stdlib.( && )
(not (Js.Array.isArray x))
(not (Stdlib.( == ) (Obj.magic x : 'a Js.null) Js.null))))
then
Ppx_deriving_json_runtime.of_json_error "expected a JSON object";
let fs = (Obj.magic x : < k : Js.Json.t Js.undefined > Js.t) in
Expand Down Expand Up @@ -283,30 +291,35 @@
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
if tag = "A" then (
if len <> 1 then
if Stdlib.( = ) tag "A" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
A)
else if tag = "B" then (
if len <> 2 then
else if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
B (int_of_json (Js.Array.unsafe_get array 1)))
else if tag = "C" then (
if len <> 2 then
else if Stdlib.( = ) tag "C" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
let fs = Js.Array.unsafe_get array 1 in
if
not
(Js.typeof fs = "object"
&& (not (Js.Array.isArray fs))
&& not ((Obj.magic fs : 'a Js.null) == Js.null))
(Stdlib.( && )
(Js.typeof fs = "object")
(Stdlib.( && )
(not (Js.Array.isArray fs))
(not
(Stdlib.( == )
(Obj.magic fs : 'a Js.null)
Js.null))))
then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON object";
Expand Down Expand Up @@ -372,7 +385,7 @@
let rec other_of_json_poly =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if tag = "C" then Some `C else None
if Stdlib.( = ) tag "C" then Some `C else None
: Js.Json.t -> other option)
and other_of_json =
Expand Down Expand Up @@ -407,17 +420,17 @@
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
if tag = "A" then (
if len <> 1 then
if Stdlib.( = ) tag "A" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `A)
else if tag = "B" then (
if len <> 2 then
else if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Some (`B (int_of_json (Js.Array.unsafe_get array 1))))
Expand Down Expand Up @@ -477,12 +490,12 @@
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
if tag = "C" then (
if len <> 2 then
if Stdlib.( = ) tag "C" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Some (`C (a_of_json (Js.Array.unsafe_get array 1))))
Expand Down Expand Up @@ -532,17 +545,17 @@
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
if tag = "A" then (
if len <> 1 then
if Stdlib.( = ) tag "A" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
A)
else if tag = "Fix" then (
if len <> 2 then
else if Stdlib.( = ) tag "Fix" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Fix (recur_of_json (Js.Array.unsafe_get array 1)))
Expand Down Expand Up @@ -590,17 +603,17 @@
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
if tag = "A" then (
if len <> 1 then
if Stdlib.( = ) tag "A" then (
if Stdlib.( <> ) len 1 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 1";
Some `A)
else if tag = "Fix" then (
if len <> 2 then
else if Stdlib.( = ) tag "Fix" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
Some
Expand Down Expand Up @@ -655,8 +668,8 @@
let rec evar_of_json =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if tag = "A" then A
else if tag = "b_aliased" then B
if Stdlib.( = ) tag "A" then A
else if Stdlib.( = ) tag "b_aliased" then B
else Ppx_deriving_json_runtime.of_json_error "invalid JSON"
: Js.Json.t -> evar)
Expand Down Expand Up @@ -687,8 +700,8 @@
let rec epoly_of_json_poly =
(fun x ->
let tag = Ppx_deriving_json_runtime.Primitives.string_of_json x in
if tag = "A_aliased" then Some `a
else if tag = "b" then Some `b
if Stdlib.( = ) tag "A_aliased" then Some `a
else if Stdlib.( = ) tag "b" then Some `b
else None
: Js.Json.t -> epoly option)
Expand Down Expand Up @@ -729,17 +742,17 @@
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
if tag = "A" then (
if len <> 2 then
if Stdlib.( = ) tag "A" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
A (a_of_json (Js.Array.unsafe_get array 1)))
else if tag = "B" then (
if len <> 2 then
else if Stdlib.( = ) tag "B" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
B (b_of_json (Js.Array.unsafe_get array 1)))
Expand Down Expand Up @@ -784,9 +797,11 @@
(fun x ->
if
not
(Js.typeof x = "object"
&& (not (Js.Array.isArray x))
&& not ((Obj.magic x : 'a Js.null) == Js.null))
(Stdlib.( && )
(Js.typeof x = "object")
(Stdlib.( && )
(not (Js.Array.isArray x))
(not (Stdlib.( == ) (Obj.magic x : 'a Js.null) Js.null))))
then
Ppx_deriving_json_runtime.of_json_error "expected a JSON object";
let fs = (Obj.magic x : < a : Js.Json.t Js.undefined > Js.t) in
Expand Down Expand Up @@ -830,20 +845,25 @@
if Js.Array.isArray x then
let array = (Obj.magic x : Js.Json.t array) in
let len = Js.Array.length array in
if len > 0 then
if Stdlib.( > ) len 0 then
let tag = Js.Array.unsafe_get array 0 in
if Js.typeof tag = "string" then
let tag = (Obj.magic tag : string) in
if tag = "A" then (
if len <> 2 then
if Stdlib.( = ) tag "A" then (
if Stdlib.( <> ) len 2 then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON array of length 2";
let fs = Js.Array.unsafe_get array 1 in
if
not
(Js.typeof fs = "object"
&& (not (Js.Array.isArray fs))
&& not ((Obj.magic fs : 'a Js.null) == Js.null))
(Stdlib.( && )
(Js.typeof fs = "object")
(Stdlib.( && )
(not (Js.Array.isArray fs))
(not
(Stdlib.( == )
(Obj.magic fs : 'a Js.null)
Js.null))))
then
Ppx_deriving_json_runtime.of_json_error
"expected a JSON object";
Expand Down

0 comments on commit 8d4151c

Please sign in to comment.