diff --git a/CHANGES.md b/CHANGES.md index a913f6e..8cc56cf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,8 @@ +## Unreleased + +- PPX: Qualify usages of infix operators with `Stdlib` + ([#11](https://github.com/melange-community/melange-json/pull/11)) + ## 1.2.0 (2024-08-16) - Port PPX from @andreypopp/ppx_deriving_json diff --git a/ppx/browser/ppx_deriving_json_js.ml b/ppx/browser/ppx_deriving_json_js.ml index 4be4695..91e437d 100644 --- a/ppx/browser/ppx_deriving_json_js.ml +++ b/ppx/browser/ppx_deriving_json_js.ml @@ -55,19 +55,22 @@ 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.( && ) + (Stdlib.( = ) (Js.typeof [%e x]) "object") + (Stdlib.( && ) + (Stdlib.not (Js.Array.isArray [%e x])) + (Stdlib.not + (Stdlib.( == ) (Obj.magic [%e x] : 'a Js.null) Js.null)))] let ensure_json_object ~loc x = [%expr - if not [%e eis_json_object ~loc x] then + if Stdlib.not [%e eis_json_object ~loc x] then Ppx_deriving_json_runtime.of_json_error [%e estring ~loc (sprintf "expected a JSON object")]] 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)]] @@ -77,9 +80,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]] @@ -113,9 +118,9 @@ 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 + if Stdlib.( = ) (Js.typeof tag) "string" then let tag = (Obj.magic tag : string) in [%e body] else @@ -135,13 +140,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]]; @@ -154,10 +160,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 diff --git a/ppx/test/ppx_deriving_json_js.t b/ppx/test/ppx_deriving_json_js.t index 70fc2f2..066269c 100644 --- a/ppx/test/ppx_deriving_json_js.t +++ b/ppx/test/ppx_deriving_json_js.t @@ -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), @@ -117,10 +119,13 @@ let rec record_of_json = (fun x -> if - not - (Js.typeof x = "object" - && (not (Js.Array.isArray x)) - && not ((Obj.magic x : 'a Js.null) == Js.null)) + Stdlib.not + (Stdlib.( && ) + (Stdlib.( = ) (Js.typeof x) "object") + (Stdlib.( && ) + (Stdlib.not (Js.Array.isArray x)) + (Stdlib.not + (Stdlib.( == ) (Obj.magic x : 'a Js.null) Js.null)))) then Ppx_deriving_json_runtime.of_json_error "expected a JSON object"; let fs = @@ -179,10 +184,13 @@ let rec record_aliased_of_json = (fun x -> if - not - (Js.typeof x = "object" - && (not (Js.Array.isArray x)) - && not ((Obj.magic x : 'a Js.null) == Js.null)) + Stdlib.not + (Stdlib.( && ) + (Stdlib.( = ) (Js.typeof x) "object") + (Stdlib.( && ) + (Stdlib.not (Js.Array.isArray x)) + (Stdlib.not + (Stdlib.( == ) (Obj.magic x : 'a Js.null) Js.null)))) then Ppx_deriving_json_runtime.of_json_error "expected a JSON object"; let fs = @@ -238,10 +246,13 @@ let rec record_opt_of_json = (fun x -> if - not - (Js.typeof x = "object" - && (not (Js.Array.isArray x)) - && not ((Obj.magic x : 'a Js.null) == Js.null)) + Stdlib.not + (Stdlib.( && ) + (Stdlib.( = ) (Js.typeof x) "object") + (Stdlib.( && ) + (Stdlib.not (Js.Array.isArray x)) + (Stdlib.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 @@ -283,30 +294,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 + if Stdlib.( = ) (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.not + (Stdlib.( && ) + (Stdlib.( = ) (Js.typeof fs) "object") + (Stdlib.( && ) + (Stdlib.not (Js.Array.isArray fs)) + (Stdlib.not + (Stdlib.( == ) + (Obj.magic fs : 'a Js.null) + Js.null)))) then Ppx_deriving_json_runtime.of_json_error "expected a JSON object"; @@ -372,7 +388,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 = @@ -407,17 +423,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 + if Stdlib.( = ) (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)))) @@ -477,12 +493,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 + if Stdlib.( = ) (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)))) @@ -532,17 +548,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 + if Stdlib.( = ) (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))) @@ -590,17 +606,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 + if Stdlib.( = ) (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 @@ -655,8 +671,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) @@ -687,8 +703,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) @@ -729,17 +745,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 + if Stdlib.( = ) (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))) @@ -783,10 +799,13 @@ let rec allow_extra_fields_of_json = (fun x -> if - not - (Js.typeof x = "object" - && (not (Js.Array.isArray x)) - && not ((Obj.magic x : 'a Js.null) == Js.null)) + Stdlib.not + (Stdlib.( && ) + (Stdlib.( = ) (Js.typeof x) "object") + (Stdlib.( && ) + (Stdlib.not (Js.Array.isArray x)) + (Stdlib.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 @@ -830,20 +849,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 + if Stdlib.( = ) (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.not + (Stdlib.( && ) + (Stdlib.( = ) (Js.typeof fs) "object") + (Stdlib.( && ) + (Stdlib.not (Js.Array.isArray fs)) + (Stdlib.not + (Stdlib.( == ) + (Obj.magic fs : 'a Js.null) + Js.null)))) then Ppx_deriving_json_runtime.of_json_error "expected a JSON object";