Skip to content

Commit a2839b5

Browse files
committed
Final fixes
1 parent 298dd48 commit a2839b5

File tree

7 files changed

+71
-49
lines changed

7 files changed

+71
-49
lines changed

analysis/reanalyze/src/ExnLib.ml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,19 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
5050
("float_of_string", [failure]);
5151
]
5252
in
53-
let stdlibBigInt = [("fromStringExn", [jsExn])] in
54-
let stdlibBool = [("fromStringExn", [invalidArgument])] in
53+
let stdlibBigInt =
54+
[
55+
("fromStringExn", [jsExn]);
56+
("fromStringOrThrow", [jsExn]);
57+
("fromFloatOrThrow", [jsExn]);
58+
]
59+
in
60+
let stdlibBool =
61+
[
62+
("fromStringExn", [invalidArgument]);
63+
("fromStringOrThrow", [invalidArgument]);
64+
]
65+
in
5566
let stdlibError = [("raise", [jsExn])] in
5667
let stdlibExn =
5768
[
@@ -68,6 +79,7 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
6879
[
6980
("parseExn", [jsExn]);
7081
("parseExnWithReviver", [jsExn]);
82+
("parseOrThrow", [jsExn]);
7183
("stringifyAny", [jsExn]);
7284
("stringifyAnyWithIndent", [jsExn]);
7385
("stringifyAnyWithReplacer", [jsExn]);

runtime/Stdlib_BigInt.res

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ type t = bigint
66
@val external asIntN: (~width: int, bigint) => bigint = "BigInt.asIntN"
77
@val external asUintN: (~width: int, bigint) => bigint = "BigInt.asUintN"
88

9-
@val
109
/**
1110
Parses the given `string` into a `bigint` using JavaScript semantics. Return the
1211
number as a `bigint` if successfully parsed. Throws a syntax exception otherwise.
@@ -31,6 +30,7 @@ switch BigInt.fromStringOrThrow("a") {
3130
}
3231
```
3332
*/
33+
@val
3434
external fromStringOrThrow: string => bigint = "BigInt"
3535

3636
/**
@@ -64,7 +64,6 @@ external fromStringExn: string => bigint = "BigInt"
6464

6565
@val external fromInt: int => bigint = "BigInt"
6666

67-
@val
6867
/**
6968
Converts a `float` to a `bigint` using JavaScript semantics.
7069
Throws an exception if the float is not an integer or is infinite/NaN.
@@ -85,6 +84,7 @@ switch BigInt.fromFloatOrThrow(123.5) {
8584
}
8685
```
8786
*/
87+
@val
8888
external fromFloatOrThrow: float => bigint = "BigInt"
8989

9090
let fromFloat = (value: float) => {

runtime/Stdlib_JSON.res

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ type replacer = Keys(array<string>) | Replacer((string, t) => t)
1515
@deprecated("Use `parseOrThrow` instead") @raises @val
1616
external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
1717

18+
@deprecated("Use `parseOrThrow` with optional parameter instead") @raises @val
19+
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
20+
1821
@val external stringify: (t, ~replacer: replacer=?, ~space: int=?) => string = "JSON.stringify"
1922
@deprecated("Use `stringify` with optional parameter instead") @val
2023
external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify"

runtime/Stdlib_JSON.resi

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ try {
6060
6161
- Raises a SyntaxError (Exn.t) if the string isn't valid JSON.
6262
*/
63-
@raises(Exn.t)
64-
@val
63+
@raises(Exn.t) @val
6564
external parseOrThrow: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
6665

6766
/**
@@ -107,11 +106,45 @@ try {
107106
108107
- Raises a SyntaxError (Exn.t) if the string isn't valid JSON.
109108
*/
110-
@deprecated("Use `parseOrThrow` instead")
111-
@raises(Exn.t)
112-
@val
109+
@deprecated("Use `parseOrThrow` instead") @raises(Exn.t) @val
113110
external parseExn: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse"
114111

112+
/**
113+
`parseExnWithReviver(string, reviver)`
114+
115+
Parses a JSON string or throws a JavaScript exception (SyntaxError), if the string isn't valid.
116+
The reviver describes how the value should be transformed. It is a function which receives a key and a value.
117+
It returns a JSON type.
118+
119+
## Examples
120+
```rescript
121+
let reviver = (_, value: JSON.t) =>
122+
switch value {
123+
| String(string) => string->String.toUpperCase->JSON.Encode.string
124+
| Number(number) => (number *. 2.0)->JSON.Encode.float
125+
| _ => value
126+
}
127+
128+
let jsonString = `{"hello":"world","someNumber":21}`
129+
130+
JSON.parseExnWithReviver(jsonString, reviver)->Console.log
131+
// { hello: 'WORLD', someNumber: 42 }
132+
133+
try {
134+
JSON.parseExnWithReviver("", reviver)->Console.log
135+
// error
136+
} catch {
137+
| JsExn(_) => Console.log("error")
138+
}
139+
```
140+
141+
## Exceptions
142+
143+
- Raises a SyntaxError if the string is not a valid JSON.
144+
*/
145+
@deprecated("Use `parseOrThrow` with optional parameter instead") @raises(Exn.t) @val
146+
external parseExnWithReviver: (string, (string, t) => t) => t = "JSON.parse"
147+
115148
/**
116149
`stringify(json, ~replacer=?, ~space=?)`
117150
@@ -181,8 +214,7 @@ JSON.stringifyWithIndent(json, 2)
181214
// }
182215
```
183216
*/
184-
@deprecated("Use `stringify` with optional parameter instead")
185-
@val
217+
@deprecated("Use `stringify` with optional parameter instead") @val
186218
external stringifyWithIndent: (t, @as(json`null`) _, int) => string = "JSON.stringify"
187219

188220
/**
@@ -214,8 +246,7 @@ JSON.stringifyWithReplacer(json, replacer)
214246
// {"foo":"BAR","hello":"WORLD","someNumber":42}
215247
```
216248
*/
217-
@deprecated("Use `stringify` with optional parameter instead")
218-
@val
249+
@deprecated("Use `stringify` with optional parameter instead") @val
219250
external stringifyWithReplacer: (t, (string, t) => t) => string = "JSON.stringify"
220251

221252
/**
@@ -251,8 +282,7 @@ JSON.stringifyWithReplacerAndIndent(json, replacer, 2)
251282
// }
252283
```
253284
*/
254-
@deprecated("Use `stringify` with optional parameters instead")
255-
@val
285+
@deprecated("Use `stringify` with optional parameters instead") @val
256286
external stringifyWithReplacerAndIndent: (t, (string, t) => t, int) => string = "JSON.stringify"
257287

258288
/**
@@ -275,8 +305,7 @@ JSON.stringifyWithFilter(json, ["foo", "someNumber"])
275305
// {"foo":"bar","someNumber":42}
276306
```
277307
*/
278-
@deprecated("Use `stringify` with optional parameter instead")
279-
@val
308+
@deprecated("Use `stringify` with optional parameter instead") @val
280309
external stringifyWithFilter: (t, array<string>) => string = "JSON.stringify"
281310

282311
/**
@@ -302,8 +331,7 @@ JSON.stringifyWithFilterAndIndent(json, ["foo", "someNumber"], 2)
302331
// }
303332
```
304333
*/
305-
@deprecated("Use `stringify` with optional parameters instead")
306-
@val
334+
@deprecated("Use `stringify` with optional parameters instead") @val
307335
external stringifyWithFilterAndIndent: (t, array<string>, int) => string = "JSON.stringify"
308336

309337
/**
@@ -372,8 +400,7 @@ switch BigInt.fromInt(0)->JSON.stringifyAny {
372400
- Raises a TypeError if the value contains circular references.
373401
- Raises a TypeError if the value contains `BigInt`s.
374402
*/
375-
@raises(Exn.t)
376-
@val
403+
@raises(Exn.t) @val
377404
external stringifyAny: ('a, ~replacer: replacer=?, ~space: int=?) => option<string> =
378405
"JSON.stringify"
379406

@@ -416,9 +443,7 @@ switch BigInt.fromInt(0)->JSON.stringifyAny {
416443
- Raises a TypeError if the value contains circular references.
417444
- Raises a TypeError if the value contains `BigInt`s.
418445
*/
419-
@deprecated("Use `stringifyAny` with optional parameter instead")
420-
@raises(Exn.t)
421-
@val
446+
@deprecated("Use `stringifyAny` with optional parameter instead") @raises(Exn.t) @val
422447
external stringifyAnyWithIndent: ('a, @as(json`null`) _, int) => option<string> = "JSON.stringify"
423448

424449
/**
@@ -465,9 +490,7 @@ switch BigInt.fromInt(0)->JSON.stringifyAny {
465490
- Raises a TypeError if the value contains circular references.
466491
- Raises a TypeError if the value contains `BigInt`s.
467492
*/
468-
@deprecated("Use `stringifyAny` with optional parameter instead")
469-
@raises
470-
@val
493+
@deprecated("Use `stringifyAny` with optional parameter instead") @raises @val
471494
external stringifyAnyWithReplacer: ('a, (string, t) => t) => option<string> = "JSON.stringify"
472495

473496
/**
@@ -515,9 +538,7 @@ switch BigInt.fromInt(0)->JSON.stringifyAny {
515538
- Raises a TypeError if the value contains circular references.
516539
- Raises a TypeError if the value contains `BigInt`s.
517540
*/
518-
@deprecated("Use `stringifyAny` with optional parameters instead")
519-
@raises
520-
@val
541+
@deprecated("Use `stringifyAny` with optional parameters instead") @raises @val
521542
external stringifyAnyWithReplacerAndIndent: ('a, (string, t) => t, int) => option<string> =
522543
"JSON.stringify"
523544

@@ -556,9 +577,7 @@ switch BigInt.fromInt(0)->JSON.stringifyAny {
556577
- Raises a TypeError if the value contains circular references.
557578
- Raises a TypeError if the value contains `BigInt`s.
558579
*/
559-
@deprecated("Use `stringifyAny` with optional parameter instead")
560-
@raises
561-
@val
580+
@deprecated("Use `stringifyAny` with optional parameter instead") @raises @val
562581
external stringifyAnyWithFilter: ('a, array<string>) => string = "JSON.stringify"
563582

564583
/**
@@ -611,9 +630,7 @@ switch BigInt.fromInt(0)->JSON.stringifyAny {
611630
- Raises a TypeError if the value contains circular references.
612631
- Raises a TypeError if the value contains `BigInt`s.
613632
*/
614-
@deprecated("Use `stringifyAny` with optional parameters instead")
615-
@raises
616-
@val
633+
@deprecated("Use `stringifyAny` with optional parameters instead") @raises @val
617634
external stringifyAnyWithFilterAndIndent: ('a, array<string>, int) => string = "JSON.stringify"
618635

619636
module Classify: {

tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,5 @@
9595
Exception Analysis
9696
ExternalTest.res:7:5-24
9797
bigIntFromStringExn2 might raise JsExn (ExternalTest.res:7:35) and is not annotated with @raises(JsExn)
98-
99-
Exception Analysis
100-
StdlibTest.res:11:5-23
101-
bigIntFromStringExn raises nothing and is annotated with redundant @raises(JsExn)
102-
103-
Exception Analysis
104-
StdlibTest.res:14:5-16
105-
jsonParseExn raises nothing and is annotated with redundant @raises(JsExn)
10698

107-
Analysis reported 26 issues (Exception Analysis:26)
99+
Analysis reported 24 issues (Exception Analysis:24)

tests/analysis_tests/tests/not_compiled/expected/DocTemplate.res.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ newText:
2424
/**
2525

2626
*/
27-
@unboxed
28-
type name = Name(string)
27+
@unboxed type name = Name(string)
2928

3029
Xform not_compiled/DocTemplate.res 8:4
3130
can't find module DocTemplate

tests/analysis_tests/tests/not_compiled/expected/DocTemplate.resi.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ newText:
2222
/**
2323

2424
*/
25-
@unboxed
26-
type name = Name(string)
25+
@unboxed type name = Name(string)
2726

2827
Xform not_compiled/DocTemplate.resi 8:4
2928
Hit: Add Documentation template

0 commit comments

Comments
 (0)