diff --git a/CHANGELOG.md b/CHANGELOG.md index ad6db1ff6a..d9b878c37c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,9 @@ #### :rocket: New Feature - Variants: Allow coercing from variant to variant, where applicable. https://github.com/rescript-lang/rescript-compiler/pull/6314 +#### :boom: Breaking Change +- Fixed the issue of name collision between the newly defined Js.Json.t and the variant constructor in the existing Js.Json.kind type. To address this, the usage of the existing Js.Json.kind type can be updated to Js.Json.Kind.t. https://github.com/rescript-lang/rescript-compiler/pull/6317 + # 11.0.0-beta.3 #### :rocket: New Feature diff --git a/jscomp/others/js_json.res b/jscomp/others/js_json.res index 28585b636f..22a117ecce 100644 --- a/jscomp/others/js_json.res +++ b/jscomp/others/js_json.res @@ -34,13 +34,16 @@ type rec t = | Object(Js.Dict.t) | Array(array) -type rec kind<_> = - | String: kind - | Number: kind - | Object: kind> - | Array: kind> - | Boolean: kind - | Null: kind +module Kind = { + type json = t + type rec t<_> = + | String: t + | Number: t + | Object: t> + | Array: t> + | Boolean: t + | Null: t +} type tagged_t = | JSONFalse @@ -72,14 +75,14 @@ let classify = (x: t): tagged_t => { } } -let test = (type a, x: 'a, v: kind): bool => +let test = (type a, x: 'a, v: Kind.t): bool => switch v { - | Number => Js.typeof(x) == "number" - | Boolean => Js.typeof(x) == "boolean" - | String => Js.typeof(x) == "string" - | Null => Obj.magic(x) === Js.null - | Array => Js_array2.isArray(x) - | Object => Obj.magic(x) !== Js.null && (Js.typeof(x) == "object" && !Js_array2.isArray(x)) + | Kind.Number => Js.typeof(x) == "number" + | Kind.Boolean => Js.typeof(x) == "boolean" + | Kind.String => Js.typeof(x) == "string" + | Kind.Null => Obj.magic(x) === Js.null + | Kind.Array => Js_array2.isArray(x) + | Kind.Object => Obj.magic(x) !== Js.null && (Js.typeof(x) == "object" && !Js_array2.isArray(x)) } let decodeString = json => diff --git a/jscomp/others/js_json.resi b/jscomp/others/js_json.resi index e06f19cb1c..6c0163d43f 100644 --- a/jscomp/others/js_json.resi +++ b/jscomp/others/js_json.resi @@ -40,14 +40,17 @@ type rec t = | Object(Js.Dict.t) | Array(array) -/** Underlying type of a JSON value */ -type rec kind<_> = - | String: kind - | Number: kind - | Object: kind> - | Array: kind> - | Boolean: kind - | Null: kind +module Kind: { + type json = t + /** Underlying type of a JSON value */ + type rec t<_> = + | String: t + | Number: t + | Object: t> + | Array: t> + | Boolean: t + | Null: t +} type tagged_t = | JSONFalse @@ -63,7 +66,7 @@ type tagged_t = let classify: t => tagged_t /** `test(v, kind)` returns `true` if `v` is of `kind`. */ -let test: ('a, kind<'b>) => bool +let test: ('a, Kind.t<'b>) => bool /** `decodeString(json)` returns `Some(s)` if `json` is a `string`, `None` otherwise. */ let decodeString: t => option diff --git a/jscomp/test/js_json_test.res b/jscomp/test/js_json_test.res index d76d268d3c..04086a7cce 100644 --- a/jscomp/test/js_json_test.res +++ b/jscomp/test/js_json_test.res @@ -145,40 +145,40 @@ let () = { /* Check that the given json value is an array and that its element * a position [i] is equal to both the [kind] and [expected] value */ -let eq_at_i = (type a, loc: string, json: J.t, i: int, kind: J.kind, expected: a): unit => { +let eq_at_i = (type a, loc: string, json: J.t, i: int, kind: J.Kind.t, expected: a): unit => { let ty = J.classify(json) switch ty { | J.JSONArray(x) => let ty = J.classify(x[i]) switch kind { - | J.Boolean => + | J.Kind.Boolean => switch ty { | JSONTrue => eq(loc, true, expected) | JSONFalse => eq(loc, false, expected) | _ => false_(loc) } - | J.Number => + | J.Kind.Number => switch ty { | JSONNumber(f) => eq(loc, f, expected) | _ => false_(loc) } - | J.Object => + | J.Kind.Object => switch ty { | JSONObject(f) => eq(loc, f, expected) | _ => false_(loc) } - | J.Array => + | J.Kind.Array => switch ty { | JSONArray(f) => eq(loc, f, expected) | _ => false_(loc) } - | J.Null => + | J.Kind.Null => switch ty { | JSONNull => true_(loc) | _ => false_(loc) } - | J.String => + | J.Kind.String => switch ty { | JSONString(f) => eq(loc, f, expected) | _ => false_(loc) @@ -196,18 +196,18 @@ let () = { |> J.stringify |> J.parseExn - eq_at_i(__LOC__, json, 0, J.String, "string 0") - eq_at_i(__LOC__, json, 1, J.String, "string 1") - eq_at_i(__LOC__, json, 2, J.String, "string 2") + eq_at_i(__LOC__, json, 0, J.Kind.String, "string 0") + eq_at_i(__LOC__, json, 1, J.Kind.String, "string 1") + eq_at_i(__LOC__, json, 2, J.Kind.String, "string 2") () } let () = { let json = ["string 0", "string 1", "string 2"] |> J.stringArray |> J.stringify |> J.parseExn - eq_at_i(__LOC__, json, 0, J.String, "string 0") - eq_at_i(__LOC__, json, 1, J.String, "string 1") - eq_at_i(__LOC__, json, 2, J.String, "string 2") + eq_at_i(__LOC__, json, 0, J.Kind.String, "string 0") + eq_at_i(__LOC__, json, 1, J.Kind.String, "string 1") + eq_at_i(__LOC__, json, 2, J.Kind.String, "string 2") () } @@ -216,9 +216,9 @@ let () = { let json = a |> J.numberArray |> J.stringify |> J.parseExn /* Loop is unrolled to keep relevant location information */ - eq_at_i(__LOC__, json, 0, J.Number, a[0]) - eq_at_i(__LOC__, json, 1, J.Number, a[1]) - eq_at_i(__LOC__, json, 2, J.Number, a[2]) + eq_at_i(__LOC__, json, 0, J.Kind.Number, a[0]) + eq_at_i(__LOC__, json, 1, J.Kind.Number, a[1]) + eq_at_i(__LOC__, json, 2, J.Kind.Number, a[2]) () } @@ -227,9 +227,9 @@ let () = { let json = a |> Array.map(float_of_int) |> J.numberArray |> J.stringify |> J.parseExn /* Loop is unrolled to keep relevant location information */ - eq_at_i(__LOC__, json, 0, J.Number, float_of_int(a[0])) - eq_at_i(__LOC__, json, 1, J.Number, float_of_int(a[1])) - eq_at_i(__LOC__, json, 2, J.Number, float_of_int(a[2])) + eq_at_i(__LOC__, json, 0, J.Kind.Number, float_of_int(a[0])) + eq_at_i(__LOC__, json, 1, J.Kind.Number, float_of_int(a[1])) + eq_at_i(__LOC__, json, 2, J.Kind.Number, float_of_int(a[2])) () } @@ -238,9 +238,9 @@ let () = { let json = a |> J.booleanArray |> J.stringify |> J.parseExn /* Loop is unrolled to keep relevant location information */ - eq_at_i(__LOC__, json, 0, J.Boolean, a[0]) - eq_at_i(__LOC__, json, 1, J.Boolean, a[1]) - eq_at_i(__LOC__, json, 2, J.Boolean, a[2]) + eq_at_i(__LOC__, json, 0, J.Kind.Boolean, a[0]) + eq_at_i(__LOC__, json, 1, J.Kind.Boolean, a[1]) + eq_at_i(__LOC__, json, 2, J.Kind.Boolean, a[2]) () } diff --git a/lib/es6/js_json.js b/lib/es6/js_json.js index 10bf1cccae..961ee2e9ab 100644 --- a/lib/es6/js_json.js +++ b/lib/es6/js_json.js @@ -2,6 +2,8 @@ import * as Caml_option from "./caml_option.js"; +var Kind = {}; + function classify(x) { var ty = typeof x; if (ty === "string") { @@ -158,6 +160,7 @@ function deserializeUnsafe(s) { } export { + Kind , classify , test , decodeString , diff --git a/lib/js/js_json.js b/lib/js/js_json.js index b084afb882..6128ff3e74 100644 --- a/lib/js/js_json.js +++ b/lib/js/js_json.js @@ -2,6 +2,8 @@ var Caml_option = require("./caml_option.js"); +var Kind = {}; + function classify(x) { var ty = typeof x; if (ty === "string") { @@ -157,6 +159,7 @@ function deserializeUnsafe(s) { return patch(JSON.parse(s)); } +exports.Kind = Kind; exports.classify = classify; exports.test = test; exports.decodeString = decodeString;