Skip to content

Commit 9040d1e

Browse files
authored
Fix shadowing Js.Json.t with type kind (#6317)
* fix shadowing Js.Json.t with type kind * changelog * type s instead kind * changelog * Revert "type s instead kind" This reverts commit 7a8880b. * Kind.t * changelog
1 parent 60cf75a commit 9040d1e

File tree

6 files changed

+60
-45
lines changed

6 files changed

+60
-45
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
#### :rocket: New Feature
1515
- Variants: Allow coercing from variant to variant, where applicable. https://github.com/rescript-lang/rescript-compiler/pull/6314
1616

17+
#### :boom: Breaking Change
18+
- 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
19+
1720
# 11.0.0-beta.3
1821

1922
#### :rocket: New Feature

jscomp/others/js_json.res

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ type rec t =
3434
| Object(Js.Dict.t<t>)
3535
| Array(array<t>)
3636

37-
type rec kind<_> =
38-
| String: kind<Js_string.t>
39-
| Number: kind<float>
40-
| Object: kind<Js_dict.t<t>>
41-
| Array: kind<array<t>>
42-
| Boolean: kind<bool>
43-
| Null: kind<Js_types.null_val>
37+
module Kind = {
38+
type json = t
39+
type rec t<_> =
40+
| String: t<Js_string.t>
41+
| Number: t<float>
42+
| Object: t<Js_dict.t<json>>
43+
| Array: t<array<json>>
44+
| Boolean: t<bool>
45+
| Null: t<Js_types.null_val>
46+
}
4447

4548
type tagged_t =
4649
| JSONFalse
@@ -72,14 +75,14 @@ let classify = (x: t): tagged_t => {
7275
}
7376
}
7477

75-
let test = (type a, x: 'a, v: kind<a>): bool =>
78+
let test = (type a, x: 'a, v: Kind.t<a>): bool =>
7679
switch v {
77-
| Number => Js.typeof(x) == "number"
78-
| Boolean => Js.typeof(x) == "boolean"
79-
| String => Js.typeof(x) == "string"
80-
| Null => Obj.magic(x) === Js.null
81-
| Array => Js_array2.isArray(x)
82-
| Object => Obj.magic(x) !== Js.null && (Js.typeof(x) == "object" && !Js_array2.isArray(x))
80+
| Kind.Number => Js.typeof(x) == "number"
81+
| Kind.Boolean => Js.typeof(x) == "boolean"
82+
| Kind.String => Js.typeof(x) == "string"
83+
| Kind.Null => Obj.magic(x) === Js.null
84+
| Kind.Array => Js_array2.isArray(x)
85+
| Kind.Object => Obj.magic(x) !== Js.null && (Js.typeof(x) == "object" && !Js_array2.isArray(x))
8386
}
8487

8588
let decodeString = json =>

jscomp/others/js_json.resi

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ type rec t =
4040
| Object(Js.Dict.t<t>)
4141
| Array(array<t>)
4242

43-
/** Underlying type of a JSON value */
44-
type rec kind<_> =
45-
| String: kind<Js_string.t>
46-
| Number: kind<float>
47-
| Object: kind<Js_dict.t<t>>
48-
| Array: kind<array<t>>
49-
| Boolean: kind<bool>
50-
| Null: kind<Js_types.null_val>
43+
module Kind: {
44+
type json = t
45+
/** Underlying type of a JSON value */
46+
type rec t<_> =
47+
| String: t<Js_string.t>
48+
| Number: t<float>
49+
| Object: t<Js_dict.t<json>>
50+
| Array: t<array<json>>
51+
| Boolean: t<bool>
52+
| Null: t<Js_types.null_val>
53+
}
5154

5255
type tagged_t =
5356
| JSONFalse
@@ -63,7 +66,7 @@ type tagged_t =
6366
let classify: t => tagged_t
6467

6568
/** `test(v, kind)` returns `true` if `v` is of `kind`. */
66-
let test: ('a, kind<'b>) => bool
69+
let test: ('a, Kind.t<'b>) => bool
6770

6871
/** `decodeString(json)` returns `Some(s)` if `json` is a `string`, `None` otherwise. */
6972
let decodeString: t => option<Js_string.t>

jscomp/test/js_json_test.res

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,40 +145,40 @@ let () = {
145145

146146
/* Check that the given json value is an array and that its element
147147
* a position [i] is equal to both the [kind] and [expected] value */
148-
let eq_at_i = (type a, loc: string, json: J.t, i: int, kind: J.kind<a>, expected: a): unit => {
148+
let eq_at_i = (type a, loc: string, json: J.t, i: int, kind: J.Kind.t<a>, expected: a): unit => {
149149
let ty = J.classify(json)
150150
switch ty {
151151
| J.JSONArray(x) =>
152152
let ty = J.classify(x[i])
153153
switch kind {
154-
| J.Boolean =>
154+
| J.Kind.Boolean =>
155155
switch ty {
156156
| JSONTrue => eq(loc, true, expected)
157157

158158
| JSONFalse => eq(loc, false, expected)
159159
| _ => false_(loc)
160160
}
161-
| J.Number =>
161+
| J.Kind.Number =>
162162
switch ty {
163163
| JSONNumber(f) => eq(loc, f, expected)
164164
| _ => false_(loc)
165165
}
166-
| J.Object =>
166+
| J.Kind.Object =>
167167
switch ty {
168168
| JSONObject(f) => eq(loc, f, expected)
169169
| _ => false_(loc)
170170
}
171-
| J.Array =>
171+
| J.Kind.Array =>
172172
switch ty {
173173
| JSONArray(f) => eq(loc, f, expected)
174174
| _ => false_(loc)
175175
}
176-
| J.Null =>
176+
| J.Kind.Null =>
177177
switch ty {
178178
| JSONNull => true_(loc)
179179
| _ => false_(loc)
180180
}
181-
| J.String =>
181+
| J.Kind.String =>
182182
switch ty {
183183
| JSONString(f) => eq(loc, f, expected)
184184
| _ => false_(loc)
@@ -196,18 +196,18 @@ let () = {
196196
|> J.stringify
197197
|> J.parseExn
198198

199-
eq_at_i(__LOC__, json, 0, J.String, "string 0")
200-
eq_at_i(__LOC__, json, 1, J.String, "string 1")
201-
eq_at_i(__LOC__, json, 2, J.String, "string 2")
199+
eq_at_i(__LOC__, json, 0, J.Kind.String, "string 0")
200+
eq_at_i(__LOC__, json, 1, J.Kind.String, "string 1")
201+
eq_at_i(__LOC__, json, 2, J.Kind.String, "string 2")
202202
()
203203
}
204204

205205
let () = {
206206
let json = ["string 0", "string 1", "string 2"] |> J.stringArray |> J.stringify |> J.parseExn
207207

208-
eq_at_i(__LOC__, json, 0, J.String, "string 0")
209-
eq_at_i(__LOC__, json, 1, J.String, "string 1")
210-
eq_at_i(__LOC__, json, 2, J.String, "string 2")
208+
eq_at_i(__LOC__, json, 0, J.Kind.String, "string 0")
209+
eq_at_i(__LOC__, json, 1, J.Kind.String, "string 1")
210+
eq_at_i(__LOC__, json, 2, J.Kind.String, "string 2")
211211
()
212212
}
213213

@@ -216,9 +216,9 @@ let () = {
216216
let json = a |> J.numberArray |> J.stringify |> J.parseExn
217217

218218
/* Loop is unrolled to keep relevant location information */
219-
eq_at_i(__LOC__, json, 0, J.Number, a[0])
220-
eq_at_i(__LOC__, json, 1, J.Number, a[1])
221-
eq_at_i(__LOC__, json, 2, J.Number, a[2])
219+
eq_at_i(__LOC__, json, 0, J.Kind.Number, a[0])
220+
eq_at_i(__LOC__, json, 1, J.Kind.Number, a[1])
221+
eq_at_i(__LOC__, json, 2, J.Kind.Number, a[2])
222222
()
223223
}
224224

@@ -227,9 +227,9 @@ let () = {
227227
let json = a |> Array.map(float_of_int) |> J.numberArray |> J.stringify |> J.parseExn
228228

229229
/* Loop is unrolled to keep relevant location information */
230-
eq_at_i(__LOC__, json, 0, J.Number, float_of_int(a[0]))
231-
eq_at_i(__LOC__, json, 1, J.Number, float_of_int(a[1]))
232-
eq_at_i(__LOC__, json, 2, J.Number, float_of_int(a[2]))
230+
eq_at_i(__LOC__, json, 0, J.Kind.Number, float_of_int(a[0]))
231+
eq_at_i(__LOC__, json, 1, J.Kind.Number, float_of_int(a[1]))
232+
eq_at_i(__LOC__, json, 2, J.Kind.Number, float_of_int(a[2]))
233233
()
234234
}
235235

@@ -238,9 +238,9 @@ let () = {
238238
let json = a |> J.booleanArray |> J.stringify |> J.parseExn
239239

240240
/* Loop is unrolled to keep relevant location information */
241-
eq_at_i(__LOC__, json, 0, J.Boolean, a[0])
242-
eq_at_i(__LOC__, json, 1, J.Boolean, a[1])
243-
eq_at_i(__LOC__, json, 2, J.Boolean, a[2])
241+
eq_at_i(__LOC__, json, 0, J.Kind.Boolean, a[0])
242+
eq_at_i(__LOC__, json, 1, J.Kind.Boolean, a[1])
243+
eq_at_i(__LOC__, json, 2, J.Kind.Boolean, a[2])
244244
()
245245
}
246246

lib/es6/js_json.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import * as Caml_option from "./caml_option.js";
44

5+
var Kind = {};
6+
57
function classify(x) {
68
var ty = typeof x;
79
if (ty === "string") {
@@ -158,6 +160,7 @@ function deserializeUnsafe(s) {
158160
}
159161

160162
export {
163+
Kind ,
161164
classify ,
162165
test ,
163166
decodeString ,

lib/js/js_json.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var Caml_option = require("./caml_option.js");
44

5+
var Kind = {};
6+
57
function classify(x) {
68
var ty = typeof x;
79
if (ty === "string") {
@@ -157,6 +159,7 @@ function deserializeUnsafe(s) {
157159
return patch(JSON.parse(s));
158160
}
159161

162+
exports.Kind = Kind;
160163
exports.classify = classify;
161164
exports.test = test;
162165
exports.decodeString = decodeString;

0 commit comments

Comments
 (0)