Skip to content

Commit 7cfe243

Browse files
authored
update Core to v1.4 (#878)
* update Core to v1.4 * remove stringify
1 parent b6c7172 commit 7cfe243

10 files changed

+140
-144
lines changed

package-lock.json

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"@docsearch/react": "^3.5.2",
2121
"@headlessui/react": "^1.2.0",
2222
"@mdx-js/loader": "^2.3.0",
23-
"@rescript/core": "^1.3.0",
23+
"@rescript/core": "^1.4.0",
2424
"@rescript/react": "^0.12.0-alpha.3",
2525
"@rescript/tools": "^0.5.0",
2626
"acorn": "^8.11.3",

src/ApiDocs.res

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ let processStaticProps = (~slug: array<string>, ~version: string) => {
477477

478478
Variant({items: items})->Null.make
479479
}
480-
| None => Js.Null.empty
480+
| None => Null.null
481481
}
482482
Type({
483483
id,

src/components/SearchBox.res

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ let make = (
2929

3030
let onAreaFocus = evt => {
3131
let el = ReactEvent.Focus.target(evt)
32-
// TODO(aspeddro): Replace with `Nullable.isNullable` when Core merge https://github.com/rescript-association/rescript-core/pull/227 and publish a new release
33-
let isDiv = Js.Null_undefined.isNullable(el["type"])
32+
let isDiv = Nullable.isNullable(el["type"])
3433

3534
if isDiv && state === Inactive {
3635
focusInput()

src/vendor/Json.res

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,16 @@ module Encode = Json_encode
44
exception ParseError(string)
55

66
let parse = s =>
7-
try Some(Js.Json.parseExn(s)) catch {
7+
try Some(JSON.parseExn(s)) catch {
88
| _ => None
99
}
1010

1111
let parseOrRaise = s =>
12-
try Js.Json.parseExn(s) catch {
13-
| Js.Exn.Error(e) =>
14-
let message = switch Js.Exn.message(e) {
12+
try JSON.parseExn(s) catch {
13+
| Exn.Error(e) =>
14+
let message = switch Exn.message(e) {
1515
| Some(m) => m
1616
| None => "Unknown error"
1717
}
1818
\"@@"(raise, ParseError(message))
1919
}
20-
21-
@val external stringify: Js.Json.t => string = "JSON.stringify"

src/vendor/Json.resi

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,14 @@ module Encode = Json_encode
106106

107107
exception ParseError(string)
108108

109-
@ocaml.doc(" [parse s] returns [Some json] if s is a valid json string, [None] otherwise ")
110-
let parse: string => option<Js.Json.t>
109+
/**
110+
`parse(s)` returns `option<JSON.t>` if s is a valid json string, `None`
111+
otherwise
112+
*/
113+
let parse: string => option<JSON.t>
111114

112-
@ocaml.doc(
113-
" [parse s] returns a [Js.Json.t] if s is a valid json string, raises [ParseError] otherwise "
114-
)
115-
let parseOrRaise: string => Js.Json.t
116-
117-
@ocaml.doc(" [stringify json] returns the [string] representation of the given [Js.Json.t] value ")
118-
let stringify: Js.Json.t => string
115+
/**
116+
`parse(s)` returns a `JSON.t` if `s` is a valid json string, raises
117+
`ParseError` otherwise
118+
*/
119+
let parseOrRaise: string => JSON.t

src/vendor/Json_decode.res

Lines changed: 57 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,113 @@
11
@new external _unsafeCreateUninitializedArray: int => array<'a> = "Array"
22

3-
@val external _stringify: Js.Json.t => string = "JSON.stringify"
3+
let _isInteger = value => Float.isFinite(value) && Math.floor(value) === value
44

5-
let _isInteger = value => Js.Float.isFinite(value) && Js.Math.floor_float(value) === value
6-
7-
type decoder<'a> = Js.Json.t => 'a
5+
type decoder<'a> = JSON.t => 'a
86

97
exception DecodeError(string)
108

119
let id = json => json
1210

1311
let bool = json =>
14-
if Js.typeof(json) == "boolean" {
15-
(Obj.magic((json: Js.Json.t)): bool)
12+
if typeof(json) == #boolean {
13+
(Obj.magic((json: JSON.t)): bool)
1614
} else {
17-
\"@@"(raise, DecodeError("Expected boolean, got " ++ _stringify(json)))
15+
\"@@"(raise, DecodeError("Expected boolean, got " ++ JSON.stringify(json)))
1816
}
1917

2018
let float = json =>
21-
if Js.typeof(json) == "number" {
22-
(Obj.magic((json: Js.Json.t)): float)
19+
if typeof(json) == #number {
20+
(Obj.magic((json: JSON.t)): float)
2321
} else {
24-
\"@@"(raise, DecodeError("Expected number, got " ++ _stringify(json)))
22+
\"@@"(raise, DecodeError("Expected number, got " ++ JSON.stringify(json)))
2523
}
2624

2725
let int = json => {
2826
let f = float(json)
2927
if _isInteger(f) {
3028
(Obj.magic((f: float)): int)
3129
} else {
32-
\"@@"(raise, DecodeError("Expected integer, got " ++ _stringify(json)))
30+
\"@@"(raise, DecodeError("Expected integer, got " ++ JSON.stringify(json)))
3331
}
3432
}
3533

3634
let string = json =>
37-
if Js.typeof(json) == "string" {
38-
(Obj.magic((json: Js.Json.t)): string)
35+
if typeof(json) == #string {
36+
(Obj.magic((json: JSON.t)): string)
3937
} else {
40-
\"@@"(raise, DecodeError("Expected string, got " ++ _stringify(json)))
38+
\"@@"(raise, DecodeError("Expected string, got " ++ JSON.stringify(json)))
4139
}
4240

4341
let char = json => {
4442
let s = string(json)
4543
if String.length(s) == 1 {
4644
OCamlCompat.String.get(s, 0)
4745
} else {
48-
\"@@"(raise, DecodeError("Expected single-character string, got " ++ _stringify(json)))
46+
\"@@"(raise, DecodeError("Expected single-character string, got " ++ JSON.stringify(json)))
4947
}
5048
}
5149

52-
let date = json => Js.Date.fromString(string(json))
50+
let date = json => Date.fromString(string(json))
5351

5452
let nullable = (decode, json) =>
55-
if (Obj.magic(json): Js.null<'a>) === Js.null {
56-
Js.null
53+
if (Obj.magic(json): Null.t<'a>) === Null.null {
54+
Null.null
5755
} else {
58-
Js.Null.return(decode(json))
56+
Null.make(decode(json))
5957
}
6058

6159
/* TODO: remove this? */
6260
let nullAs = (value, json) =>
63-
if (Obj.magic(json): Js.null<'a>) === Js.null {
61+
if (Obj.magic(json): Null.t<'a>) === Null.null {
6462
value
6563
} else {
66-
\"@@"(raise, DecodeError("Expected null, got " ++ _stringify(json)))
64+
\"@@"(raise, DecodeError("Expected null, got " ++ JSON.stringify(json)))
6765
}
6866

6967
let array = (decode, json) =>
70-
if Js.Array.isArray(json) {
71-
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
72-
let length = Js.Array.length(source)
68+
if Array.isArray(json) {
69+
let source: array<JSON.t> = Obj.magic((json: JSON.t))
70+
let length = Array.length(source)
7371
let target = _unsafeCreateUninitializedArray(length)
7472
for i in 0 to length - 1 {
7573
let value = try decode(Array.getUnsafe(source, i)) catch {
7674
| DecodeError(msg) =>
77-
\"@@"(raise, DecodeError(msg ++ ("\n\tin array at index " ++ string_of_int(i))))
75+
\"@@"(raise, DecodeError(msg ++ ("\n\tin array at index " ++ Int.toString(i))))
7876
}
7977

8078
Array.setUnsafe(target, i, value)
8179
}
8280
target
8381
} else {
84-
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
82+
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
8583
}
8684

8785
let list = (decode, json) => array(decode, json)->List.fromArray
8886

8987
let pair = (decodeA, decodeB, json) =>
90-
if Js.Array.isArray(json) {
91-
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
92-
let length = Js.Array.length(source)
88+
if Array.isArray(json) {
89+
let source: array<JSON.t> = Obj.magic((json: JSON.t))
90+
let length = Array.length(source)
9391
if length == 2 {
9492
try (decodeA(Array.getUnsafe(source, 0)), decodeB(Array.getUnsafe(source, 1))) catch {
9593
| DecodeError(msg) => \"@@"(raise, DecodeError(msg ++ "\n\tin pair/tuple2"))
9694
}
9795
} else {
9896
\"@@"(
9997
raise,
100-
DecodeError(`Expected array of length 2, got array of length ${length->string_of_int}`),
98+
DecodeError(`Expected array of length 2, got array of length ${length->Int.toString}`),
10199
)
102100
}
103101
} else {
104-
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
102+
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
105103
}
106104

107105
let tuple2 = pair
108106

109107
let tuple3 = (decodeA, decodeB, decodeC, json) =>
110-
if Js.Array.isArray(json) {
111-
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
112-
let length = Js.Array.length(source)
108+
if Array.isArray(json) {
109+
let source: array<JSON.t> = Obj.magic((json: JSON.t))
110+
let length = Array.length(source)
113111
if length == 3 {
114112
try (
115113
decodeA(Array.getUnsafe(source, 0)),
@@ -121,17 +119,17 @@ let tuple3 = (decodeA, decodeB, decodeC, json) =>
121119
} else {
122120
\"@@"(
123121
raise,
124-
DecodeError(`Expected array of length 3, got array of length ${length->string_of_int}`),
122+
DecodeError(`Expected array of length 3, got array of length ${length->Int.toString}`),
125123
)
126124
}
127125
} else {
128-
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
126+
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
129127
}
130128

131129
let tuple4 = (decodeA, decodeB, decodeC, decodeD, json) =>
132-
if Js.Array.isArray(json) {
133-
let source: array<Js.Json.t> = Obj.magic((json: Js.Json.t))
134-
let length = Js.Array.length(source)
130+
if Array.isArray(json) {
131+
let source: array<JSON.t> = Obj.magic((json: JSON.t))
132+
let length = Array.length(source)
135133
if length == 4 {
136134
try (
137135
decodeA(Array.getUnsafe(source, 0)),
@@ -144,52 +142,52 @@ let tuple4 = (decodeA, decodeB, decodeC, decodeD, json) =>
144142
} else {
145143
\"@@"(
146144
raise,
147-
DecodeError(`Expected array of length 4, got array of length ${length->string_of_int}`),
145+
DecodeError(`Expected array of length 4, got array of length ${length->Int.toString}`),
148146
)
149147
}
150148
} else {
151-
\"@@"(raise, DecodeError("Expected array, got " ++ _stringify(json)))
149+
\"@@"(raise, DecodeError("Expected array, got " ++ JSON.stringify(json)))
152150
}
153151

154152
let dict = (decode, json) =>
155153
if (
156-
Js.typeof(json) == "object" &&
157-
(!Js.Array.isArray(json) &&
158-
!((Obj.magic(json): Js.null<'a>) === Js.null))
154+
typeof(json) == #object &&
155+
(!Array.isArray(json) &&
156+
!((Obj.magic(json): Null.t<'a>) === Null.null))
159157
) {
160-
let source: Js.Dict.t<Js.Json.t> = Obj.magic((json: Js.Json.t))
161-
let keys = Js.Dict.keys(source)
162-
let l = Js.Array.length(keys)
163-
let target = Js.Dict.empty()
158+
let source: Dict.t<JSON.t> = Obj.magic((json: JSON.t))
159+
let keys = Dict.keysToArray(source)
160+
let l = Array.length(keys)
161+
let target = Dict.make()
164162
for i in 0 to l - 1 {
165163
let key = Array.getUnsafe(keys, i)
166-
let value = try decode(Js.Dict.unsafeGet(source, key)) catch {
164+
let value = try decode(Dict.getUnsafe(source, key)) catch {
167165
| DecodeError(msg) => \"@@"(raise, DecodeError(msg ++ "\n\tin dict"))
168166
}
169167

170-
Js.Dict.set(target, key, value)
168+
Dict.set(target, key, value)
171169
}
172170
target
173171
} else {
174-
\"@@"(raise, DecodeError("Expected object, got " ++ _stringify(json)))
172+
\"@@"(raise, DecodeError("Expected object, got " ++ JSON.stringify(json)))
175173
}
176174

177175
let field = (key, decode, json) =>
178176
if (
179-
Js.typeof(json) == "object" &&
180-
(!Js.Array.isArray(json) &&
181-
!((Obj.magic(json): Js.null<'a>) === Js.null))
177+
typeof(json) == #object &&
178+
(!Array.isArray(json) &&
179+
!((Obj.magic(json): Null.t<'a>) === Null.null))
182180
) {
183-
let dict: Js.Dict.t<Js.Json.t> = Obj.magic((json: Js.Json.t))
184-
switch Js.Dict.get(dict, key) {
181+
let dict: Dict.t<JSON.t> = Obj.magic((json: JSON.t))
182+
switch Dict.get(dict, key) {
185183
| Some(value) =>
186184
try decode(value) catch {
187185
| DecodeError(msg) => \"@@"(raise, DecodeError(msg ++ ("\n\tat field '" ++ (key ++ "'"))))
188186
}
189187
| None => \"@@"(raise, DecodeError(`Expected field '${key}'`))
190188
}
191189
} else {
192-
\"@@"(raise, DecodeError("Expected object, got " ++ _stringify(json)))
190+
\"@@"(raise, DecodeError("Expected object, got " ++ JSON.stringify(json)))
193191
}
194192

195193
let rec at = (key_path, decoder, json) =>
@@ -208,12 +206,12 @@ let oneOf = (decoders, json) => {
208206
let rec inner = (decoders, errors) =>
209207
switch decoders {
210208
| list{} =>
211-
let formattedErrors = "\n- " ++ Js.Array.joinWith("\n- ", List.toArray(List.reverse(errors)))
209+
let formattedErrors = "\n- " ++ Array.join(List.toArray(List.reverse(errors)), "\n- ")
212210
\"@@"(
213211
raise,
214212
DecodeError(
215213
`All decoders given to oneOf failed. Here are all the errors: ${formattedErrors}\\nAnd the JSON being decoded: ` ++
216-
_stringify(json),
214+
JSON.stringify(json),
217215
),
218216
)
219217
| list{decode, ...rest} =>

0 commit comments

Comments
 (0)