Skip to content

Commit 1ad47d6

Browse files
committed
add chained to show weirdness
1 parent deb63af commit 1ad47d6

File tree

5 files changed

+143
-1
lines changed

5 files changed

+143
-1
lines changed

runtime/Js_json.resi

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,13 @@ let test: ('a, Kind.t<'b>) => bool
7373
/**
7474
`decodeString(json)` returns `Some(s)` if `json` is a `string`, `None` otherwise.
7575
*/
76+
@deprecated({
77+
reason: "Use pattern matching instead.",
78+
migrate: switch %insert.unlabelledArgument(0) {
79+
| JSON.String(str) => Some(str)
80+
| _ => None
81+
}
82+
})
7683
let decodeString: t => option<Js_string.t>
7784

7885
/**
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
external someJson: Js_json.t = "someJson"
2+
external strToJson: string => Js_json.t = "strToJson"
3+
4+
let decodeString1 = switch someJson {
5+
| JSON.String(str) => Some(str)
6+
| _ => None
7+
}
8+
let decodeString2 = switch someJson {
9+
| JSON.String(str) => Some(str)
10+
| _ => None
11+
}
12+
let decodeString3 = switch [1, 2, 3]
13+
->Array.map(v => v->Int.toString)
14+
->Array.join(" ")
15+
->strToJson {
16+
| JSON.String(str) => Some(str)
17+
| _ => None
18+
}
19+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
external someJson: Js_json.t = "someJson"
2+
external strToJson: string => Js_json.t = "strToJson"
3+
4+
let decodeString1 = someJson->Js_json.decodeString
5+
let decodeString2 = Js_json.decodeString(someJson)
6+
let decodeString3 =
7+
[1, 2, 3]
8+
->Array.map(v => v->Int.toString)
9+
->Array.join(" ")
10+
->strToJson
11+
->Js_json.decodeString
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// This file is autogenerated so it can be type checked.
2+
// It's the migrated version of src/migrate/StdlibMigration_JSON.res.
3+
external someJson: Js_json.t = "someJson"
4+
external strToJson: string => Js_json.t = "strToJson"
5+
6+
let decodeString1 = switch someJson {
7+
| JSON.String(str) => Some(str)
8+
| _ => None
9+
}
10+
let decodeString2 = switch someJson {
11+
| JSON.String(str) => Some(str)
12+
| _ => None
13+
}
14+
let decodeString3 = switch [1, 2, 3]
15+
->Array.map(v => v->Int.toString)
16+
->Array.join(" ")
17+
->strToJson {
18+
| JSON.String(str) => Some(str)
19+
| _ => None
20+
}

tools/src/tools.ml

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1306,7 +1306,6 @@ module Migrate = struct
13061306

13071307
(*
13081308
TODO:
1309-
- Migrate unlabelled arguments (Array.reduceXxx)
13101309
- Migrate type usage (Js.Array2.t -> array, etc)
13111310
*)
13121311

@@ -1487,6 +1486,80 @@ module Migrate = struct
14871486
filtered_args @ template_args_to_insert
14881487
end
14891488

1489+
(* Finds a specific argument in a list of arguments. *)
1490+
let find_arg args (find_this_arg : [`Labelled of string | `Unlabelled of int])
1491+
=
1492+
let unlabelled_count = ref 0 in
1493+
args
1494+
|> List.find_map (fun (lbl, arg) ->
1495+
match (find_this_arg, lbl) with
1496+
| ( `Labelled arg_name,
1497+
(Asttypes.Labelled {txt = label} | Optional {txt = label}) )
1498+
when label = arg_name ->
1499+
Some (lbl, arg)
1500+
| `Unlabelled count, Nolabel ->
1501+
let current_count = !unlabelled_count in
1502+
incr unlabelled_count;
1503+
if current_count = count then Some (lbl, arg) else None
1504+
| _, Nolabel ->
1505+
incr unlabelled_count;
1506+
None
1507+
| _ -> None)
1508+
1509+
let replace_from_args_in_expr expr source_args =
1510+
let mapper =
1511+
{
1512+
Ast_mapper.default_mapper with
1513+
expr =
1514+
(fun mapper exp ->
1515+
match exp with
1516+
| {
1517+
pexp_desc =
1518+
Pexp_extension
1519+
( {txt = "insert.labelledArgument"},
1520+
PStr
1521+
[
1522+
{
1523+
pstr_desc =
1524+
Pstr_eval
1525+
( {
1526+
pexp_desc =
1527+
Pexp_constant (Pconst_string (arg_name, _));
1528+
},
1529+
_ );
1530+
};
1531+
] );
1532+
} -> (
1533+
match find_arg source_args (`Labelled arg_name) with
1534+
| Some (_, arg) -> arg
1535+
| None -> exp)
1536+
| {
1537+
pexp_desc =
1538+
Pexp_extension
1539+
( {txt = "insert.unlabelledArgument"},
1540+
PStr
1541+
[
1542+
{
1543+
pstr_desc =
1544+
Pstr_eval
1545+
( {
1546+
pexp_desc =
1547+
Pexp_constant (Pconst_integer (count_str, _));
1548+
},
1549+
_ );
1550+
};
1551+
] );
1552+
} -> (
1553+
match
1554+
find_arg source_args (`Unlabelled (int_of_string count_str))
1555+
with
1556+
| Some (_, arg) -> arg
1557+
| None -> exp)
1558+
| _ -> Ast_mapper.default_mapper.expr mapper exp);
1559+
}
1560+
in
1561+
mapper.expr mapper expr
1562+
14901563
let makeMapper (deprecated_used : Cmt_utils.deprecated_used list) =
14911564
(* Function calls *)
14921565
let deprecated_function_calls =
@@ -1560,6 +1633,12 @@ module Migrate = struct
15601633

15611634
(* TODO: Here we could add strict and partial mode, to control if args are merged or not. *)
15621635
match deprecated_info.migration_template with
1636+
| Some {pexp_desc = Pexp_match (e, cases)} ->
1637+
{
1638+
exp with
1639+
pexp_desc =
1640+
Pexp_match (replace_from_args_in_expr e source_args, cases);
1641+
}
15631642
| Some
15641643
{
15651644
pexp_desc =
@@ -1610,6 +1689,12 @@ module Migrate = struct
16101689
Hashtbl.remove loc_to_deprecated_fn_call fn_loc;
16111690

16121691
match deprecated_info.migration_template with
1692+
| Some {pexp_desc = Pexp_match (e, cases)} ->
1693+
{
1694+
exp with
1695+
pexp_desc =
1696+
Pexp_match (replace_from_args_in_expr e [lhs], cases);
1697+
}
16131698
| Some
16141699
{
16151700
pexp_desc =

0 commit comments

Comments
 (0)