Skip to content

Commit 9b3513f

Browse files
committed
support unlabelled arguments as well
1 parent 0509f79 commit 9b3513f

File tree

8 files changed

+415
-52
lines changed

8 files changed

+415
-52
lines changed

runtime/Js_array2.res

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,10 @@ Js.Array2.spliceInPlace(arr3, ~pos=9, ~remove=2, ~add=["x", "y", "z"]) == []
489489
arr3 == ["a", "b", "c", "d", "e", "f", "x", "y", "z"]
490490
```
491491
*/
492-
@send @variadic
492+
@send @variadic @deprecated({
493+
reason: "Use `Array.splice` instead.",
494+
migrate: Array.splice(~start=%insert.labelledArgument("pos"), ~remove=%insert.labelledArgument("remove"), ~insert=%insert.labelledArgument("add"))
495+
})
493496
external spliceInPlace: (t<'a>, ~pos: int, ~remove: int, ~add: array<'a>) => t<'a> = "splice"
494497

495498
/**
@@ -507,7 +510,10 @@ Js.Array2.removeFromInPlace(arr, ~pos=4) == ["e", "f"]
507510
arr == ["a", "b", "c", "d"]
508511
```
509512
*/
510-
@send
513+
@send @deprecated({
514+
reason: "Use `Array.removeInPlace` instead.",
515+
migrate: Array.removeInPlace(%insert.labelledArgument("pos"))
516+
})
511517
external removeFromInPlace: (t<'a>, ~pos: int) => t<'a> = "splice"
512518

513519
/**
@@ -525,7 +531,10 @@ Js.Array2.removeCountInPlace(arr, ~pos=2, ~count=3) == ["c", "d", "e"]
525531
arr == ["a", "b", "f"]
526532
```
527533
*/
528-
@send
534+
@send @deprecated({
535+
reason: "Use `Array.splice` instead.",
536+
migrate: Array.splice(~start=%insert.labelledArgument("pos"), ~remove=%insert.labelledArgument("count"), ~insert=[])
537+
})
529538
external removeCountInPlace: (t<'a>, ~pos: int, ~count: int) => t<'a> = "splice"
530539

531540
/**
@@ -790,7 +799,7 @@ on MDN.
790799
*/
791800
@deprecated({
792801
reason: "Use `Array.sliceToEnd` instead.",
793-
migrate: Array.sliceToEnd()
802+
migrate: Array.sliceToEnd(~start=%insert.unlabelledArgument(1))
794803
})
795804
@send
796805
external sliceFrom: (t<'a>, int) => t<'a> = "slice"
@@ -1163,6 +1172,10 @@ Js.Array2.reduce(
11631172
Js.Array2.reduce([2.0, 4.0], (acc, item) => item /. acc, 1.0) == 2.0 // 4.0 / (2.0 / 1.0)
11641173
```
11651174
*/
1175+
@deprecated({
1176+
reason: "Use `Array.reduce` instead.",
1177+
migrate: Array.reduce(%insert.unlabelledArgument(2), %insert.unlabelledArgument(1))
1178+
})
11661179
@send
11671180
external reduce: (t<'a>, ('b, 'a) => 'b, 'b) => 'b = "reduce"
11681181

@@ -1198,7 +1211,10 @@ let sumOfEvens = (accumulator, item, index) =>
11981211
Js.Array2.reducei([2, 5, 1, 4, 3], sumOfEvens, 0) == 6
11991212
```
12001213
*/
1201-
@send
1214+
@send @deprecated({
1215+
reason: "Use `Array.reduceWithIndex` instead.",
1216+
migrate: Array.reduceWithIndex(%insert.unlabelledArgument(2), %insert.unlabelledArgument(1))
1217+
})
12021218
external reducei: (t<'a>, ('b, 'a, int) => 'b, 'b) => 'b = "reduce"
12031219

12041220
/**
@@ -1230,7 +1246,10 @@ Js.Array2.reduceRight([10, 2, 4], sumOfSquares, 0) == 120
12301246
Js.Array2.reduceRight([2.0, 4.0], (acc, item) => item /. acc, 1.0) == 0.5 // 2.0 / (4.0 / 1.0)
12311247
```
12321248
*/
1233-
@send
1249+
@send @deprecated({
1250+
reason: "Use `Array.reduceRight` instead.",
1251+
migrate: Array.reduceRight(%insert.unlabelledArgument(2), %insert.unlabelledArgument(1))
1252+
})
12341253
external reduceRight: (t<'a>, ('b, 'a) => 'b, 'b) => 'b = "reduceRight"
12351254

12361255
/**
@@ -1267,7 +1286,10 @@ let sumOfEvens = (accumulator, item, index) =>
12671286
Js.Array2.reduceRighti([2, 5, 1, 4, 3], sumOfEvens, 0) == 6
12681287
```
12691288
*/
1270-
@send
1289+
@send @deprecated({
1290+
reason: "Use `Array.reduceRightWithIndex` instead.",
1291+
migrate: Array.reduceRightWithIndex(%insert.unlabelledArgument(2), %insert.unlabelledArgument(1))
1292+
})
12711293
external reduceRighti: (t<'a>, ('b, 'a, int) => 'b, 'b) => 'b = "reduceRight"
12721294

12731295
/**
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Migrations that will not compile after migration (by design)
2+
let sortInPlaceWith1 = [3, 1, 2]->Array.sort((a, b) => a - b)
3+
let sortInPlaceWith2 = Array.sort([3, 1, 2], (a, b) => a - b)
4+

tests/tools_tests/src/expected/StdlibMigration_Array.res.expected

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ let lastIndexOfFrom2 = Array.lastIndexOfFrom([1, 2, 1, 3, 1], 1, 3)
6060
let copy1 = [1, 2, 3]->Array.copy
6161
let copy2 = Array.copy([1, 2, 3])
6262

63-
let sliceFrom1 = [1, 2, 3, 4]->Array.sliceToEnd(2)
64-
let sliceFrom2 = Array.sliceToEnd([1, 2, 3, 4], 2)
63+
let sliceFrom1 = [1, 2, 3, 4]->Array.sliceToEnd(~start=2)
64+
let sliceFrom2 = Array.sliceToEnd([1, 2, 3, 4], ~start=2)
6565

6666
let toString1 = [1, 2, 3]->Array.toString
6767
let toString2 = Array.toString([1, 2, 3])
@@ -140,12 +140,30 @@ let sortInPlace2 = Array.toSorted(["c", "a", "b"], (a, b) =>
140140
%todo("This needs a comparator function. Use `String.compare` for strings, etc.")
141141
)
142142

143-
let sortInPlaceWith1 = [3, 1, 2]->Array.sort((a, b) => a - b)
144-
let sortInPlaceWith2 = Array.sort([3, 1, 2], (a, b) => a - b)
145-
146143
let unshift1 = [1, 2, 3]->Array.unshift(4)
147144
let unshift2 = Array.unshift([1, 2, 3], 4)
148145

149146
let unshiftMany1 = [1, 2, 3]->Array.unshiftMany([4, 5])
150147
let unshiftMany2 = Array.unshiftMany([1, 2, 3], [4, 5])
151148

149+
let reduce1 = [1, 2, 3]->Array.reduce(0, (acc, x) => acc + x)
150+
let reduce2 = Array.reduce([1, 2, 3], 0, (acc, x) => acc + x)
151+
152+
let spliceInPlace1 = [1, 2, 3]->Array.splice(~start=1, ~remove=1, ~insert=[4, 5])
153+
let spliceInPlace2 = Array.splice([1, 2, 3], ~start=1, ~remove=1, ~insert=[4, 5])
154+
155+
let removeFromInPlace1 = [1, 2, 3]->Array.removeInPlace(1)
156+
let removeFromInPlace2 = Array.removeInPlace([1, 2, 3], 1)
157+
158+
let removeCountInPlace1 = [1, 2, 3]->Array.splice(~start=1, ~remove=1, ~insert=[])
159+
let removeCountInPlace2 = Array.splice([1, 2, 3], ~start=1, ~remove=1, ~insert=[])
160+
161+
let reducei1 = [1, 2, 3]->Array.reduceWithIndex(0, (acc, x, i) => acc + x + i)
162+
let reducei2 = Array.reduceWithIndex([1, 2, 3], 0, (acc, x, i) => acc + x + i)
163+
164+
let reduceRight1 = [1, 2, 3]->Array.reduceRight(0, (acc, x) => acc + x)
165+
let reduceRight2 = Array.reduceRight([1, 2, 3], 0, (acc, x) => acc + x)
166+
167+
let reduceRighti1 = [1, 2, 3]->Array.reduceRightWithIndex(0, (acc, x, i) => acc + x + i)
168+
let reduceRighti2 = Array.reduceRightWithIndex([1, 2, 3], 0, (acc, x, i) => acc + x + i)
169+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Migrations that will not compile after migration (by design)
2+
let sortInPlaceWith1 = [3, 1, 2]->Js.Array2.sortInPlaceWith((a, b) => a - b)
3+
let sortInPlaceWith2 = Js.Array2.sortInPlaceWith([3, 1, 2], (a, b) => a - b)

tests/tools_tests/src/migrate/StdlibMigration_Array.res

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,29 @@ let pushMany2 = Js.Array2.pushMany([1, 2, 3], [4, 5])
135135
let sortInPlace1 = ["c", "a", "b"]->Js.Array2.sortInPlace
136136
let sortInPlace2 = Js.Array2.sortInPlace(["c", "a", "b"])
137137

138-
let sortInPlaceWith1 = [3, 1, 2]->Js.Array2.sortInPlaceWith((a, b) => a - b)
139-
let sortInPlaceWith2 = Js.Array2.sortInPlaceWith([3, 1, 2], (a, b) => a - b)
140-
141138
let unshift1 = [1, 2, 3]->Js.Array2.unshift(4)
142139
let unshift2 = Js.Array2.unshift([1, 2, 3], 4)
143140

144141
let unshiftMany1 = [1, 2, 3]->Js.Array2.unshiftMany([4, 5])
145142
let unshiftMany2 = Js.Array2.unshiftMany([1, 2, 3], [4, 5])
143+
144+
let reduce1 = [1, 2, 3]->Js.Array2.reduce((acc, x) => acc + x, 0)
145+
let reduce2 = Js.Array2.reduce([1, 2, 3], (acc, x) => acc + x, 0)
146+
147+
let spliceInPlace1 = [1, 2, 3]->Js.Array2.spliceInPlace(~pos=1, ~remove=1, ~add=[4, 5])
148+
let spliceInPlace2 = Js.Array2.spliceInPlace([1, 2, 3], ~pos=1, ~remove=1, ~add=[4, 5])
149+
150+
let removeFromInPlace1 = [1, 2, 3]->Js.Array2.removeFromInPlace(~pos=1)
151+
let removeFromInPlace2 = Js.Array2.removeFromInPlace([1, 2, 3], ~pos=1)
152+
153+
let removeCountInPlace1 = [1, 2, 3]->Js.Array2.removeCountInPlace(~pos=1, ~count=1)
154+
let removeCountInPlace2 = Js.Array2.removeCountInPlace([1, 2, 3], ~pos=1, ~count=1)
155+
156+
let reducei1 = [1, 2, 3]->Js.Array2.reducei((acc, x, i) => acc + x + i, 0)
157+
let reducei2 = Js.Array2.reducei([1, 2, 3], (acc, x, i) => acc + x + i, 0)
158+
159+
let reduceRight1 = [1, 2, 3]->Js.Array2.reduceRight((acc, x) => acc + x, 0)
160+
let reduceRight2 = Js.Array2.reduceRight([1, 2, 3], (acc, x) => acc + x, 0)
161+
162+
let reduceRighti1 = [1, 2, 3]->Js.Array2.reduceRighti((acc, x, i) => acc + x + i, 0)
163+
let reduceRighti2 = Js.Array2.reduceRighti([1, 2, 3], (acc, x, i) => acc + x + i, 0)
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
let shift1 = [1, 2, 3]->Array.shift
2+
let shift2 = Array.shift([1, 2, 3])
3+
4+
let slice1 = [1, 2, 3]->Array.slice(~start=1, ~end=2)
5+
let slice2 = Array.slice([1, 2, 3], ~start=1, ~end=2)
6+
7+
external someArrayLike: Js_array2.array_like<string> = "whatever"
8+
9+
let from1 = someArrayLike->Array.fromArrayLike
10+
let from2 = Array.fromArrayLike(someArrayLike)
11+
12+
let fromMap1 = someArrayLike->Array.fromArrayLikeWithMap(s => s ++ "!")
13+
let fromMap2 = Array.fromArrayLikeWithMap(someArrayLike, s => s ++ "!")
14+
15+
let isArray1 = [1, 2, 3]->Array.isArray
16+
let isArray2 = Array.isArray([1, 2, 3])
17+
18+
let length1 = [1, 2, 3]->Array.length
19+
let length2 = Array.length([1, 2, 3])
20+
21+
let fillInPlace1 = [1, 2, 3]->Array.fillAll(0)
22+
let fillInPlace2 = Array.fillAll([1, 2, 3], 0)
23+
24+
let fillFromInPlace1 = [1, 2, 3, 4]->Array.fillToEnd(0, ~start=2)
25+
let fillFromInPlace2 = Array.fillToEnd([1, 2, 3, 4], 0, ~start=2)
26+
27+
let fillRangeInPlace1 = [1, 2, 3, 4]->Array.fill(0, ~start=1, ~end=3)
28+
let fillRangeInPlace2 = Array.fill([1, 2, 3, 4], 0, ~start=1, ~end=3)
29+
30+
let pop1 = [1, 2, 3]->Array.pop
31+
let pop2 = Array.pop([1, 2, 3])
32+
33+
let reverseInPlace1 = [1, 2, 3]->Array.reverse
34+
let reverseInPlace2 = Array.reverse([1, 2, 3])
35+
36+
let concat1 = [1, 2]->Array.concat([3, 4])
37+
let concat2 = Array.concat([1, 2], [3, 4])
38+
39+
let concatMany1 = [1, 2]->Array.concatMany([[3, 4], [5, 6]])
40+
let concatMany2 = Array.concatMany([1, 2], [[3, 4], [5, 6]])
41+
42+
let includes1 = [1, 2, 3]->Array.includes(2)
43+
let includes2 = Array.includes([1, 2, 3], 2)
44+
45+
let indexOf1 = [1, 2, 3]->Array.indexOf(2)
46+
let indexOf2 = Array.indexOf([1, 2, 3], 2)
47+
48+
let indexOfFrom1 = [1, 2, 1, 3]->Array.indexOfFrom(1, 2)
49+
let indexOfFrom2 = Array.indexOfFrom([1, 2, 1, 3], 1, 2)
50+
51+
let joinWith1 = [1, 2, 3]->Array.joinUnsafe(",")
52+
let joinWith2 = Array.joinUnsafe([1, 2, 3], ",")
53+
54+
let lastIndexOf1 = [1, 2, 1, 3]->Array.lastIndexOf(1)
55+
let lastIndexOf2 = Array.lastIndexOf([1, 2, 1, 3], 1)
56+
57+
let lastIndexOfFrom1 = [1, 2, 1, 3, 1]->Array.lastIndexOfFrom(1, 3)
58+
let lastIndexOfFrom2 = Array.lastIndexOfFrom([1, 2, 1, 3, 1], 1, 3)
59+
60+
let copy1 = [1, 2, 3]->Array.copy
61+
let copy2 = Array.copy([1, 2, 3])
62+
63+
let sliceFrom1 = [1, 2, 3, 4]->Array.sliceToEnd(~start=2)
64+
let sliceFrom2 = Array.sliceToEnd([1, 2, 3, 4], ~start=2)
65+
66+
let toString1 = [1, 2, 3]->Array.toString
67+
let toString2 = Array.toString([1, 2, 3])
68+
69+
let toLocaleString1 = [1, 2, 3]->Array.toLocaleString
70+
let toLocaleString2 = Array.toLocaleString([1, 2, 3])
71+
72+
let every1 = [2, 4, 6]->Array.every(x => mod(x, 2) == 0)
73+
let every2 = Array.every([2, 4, 6], x => mod(x, 2) == 0)
74+
75+
let everyi1 = [0, 1, 2]->Array.everyWithIndex((x, i) => x == i)
76+
let everyi2 = Array.everyWithIndex([0, 1, 2], (x, i) => x == i)
77+
78+
let filter1 = [1, 2, 3, 4]->Array.filter(x => x > 2)
79+
let filter2 = Array.filter([1, 2, 3, 4], x => x > 2)
80+
81+
let filteri1 = [0, 1, 2, 3]->Array.filterWithIndex((_x, i) => i > 1)
82+
let filteri2 = Array.filterWithIndex([0, 1, 2, 3], (_x, i) => i > 1)
83+
84+
let find1 = [1, 2, 3, 4]->Array.find(x => x > 2)
85+
let find2 = Array.find([1, 2, 3, 4], x => x > 2)
86+
87+
let findi1 = [0, 1, 2, 3]->Array.findWithIndex((_x, i) => i > 1)
88+
let findi2 = Array.findWithIndex([0, 1, 2, 3], (_x, i) => i > 1)
89+
90+
let findIndex1 = [1, 2, 3, 4]->Array.findIndex(x => x > 2)
91+
let findIndex2 = Array.findIndex([1, 2, 3, 4], x => x > 2)
92+
93+
let findIndexi1 = [0, 1, 2, 3]->Array.findIndexWithIndex((_x, i) => i > 1)
94+
let findIndexi2 = Array.findIndexWithIndex([0, 1, 2, 3], (_x, i) => i > 1)
95+
96+
let forEach1 = [1, 2, 3]->Array.forEach(x => ignore(x))
97+
let forEach2 = Array.forEach([1, 2, 3], x => ignore(x))
98+
99+
let forEachi1 = [1, 2, 3]->Array.forEachWithIndex((x, i) => ignore(x + i))
100+
let forEachi2 = Array.forEachWithIndex([1, 2, 3], (x, i) => ignore(x + i))
101+
102+
let map1 = [1, 2, 3]->Array.map(x => x * 2)
103+
let map2 = Array.map([1, 2, 3], x => x * 2)
104+
105+
let mapi1 = [1, 2, 3]->Array.mapWithIndex((x, i) => x + i)
106+
let mapi2 = Array.mapWithIndex([1, 2, 3], (x, i) => x + i)
107+
108+
let some1 = [1, 2, 3, 4]->Array.some(x => x > 3)
109+
let some2 = Array.some([1, 2, 3, 4], x => x > 3)
110+
111+
let somei1 = [0, 1, 2, 3]->Array.someWithIndex((_x, i) => i > 2)
112+
let somei2 = Array.someWithIndex([0, 1, 2, 3], (_x, i) => i > 2)
113+
114+
let unsafeGet1 = [1, 2, 3]->Array.getUnsafe(1)
115+
let unsafeGet2 = Array.getUnsafe([1, 2, 3], 1)
116+
117+
let unsafeSet1 = [1, 2, 3]->Array.setUnsafe(1, 5)
118+
let unsafeSet2 = Array.setUnsafe([1, 2, 3], 1, 5)
119+
120+
let copyWithin1 = [1, 2, 3, 4, 5]->Array.copyAllWithin(~target=2)
121+
let copyWithin2 = Array.copyAllWithin([1, 2, 3, 4, 5], ~target=2)
122+
123+
let copyWithinFrom1 = [1, 2, 3, 4, 5]->Array.copyWithinToEnd(~target=0, ~start=2)
124+
let copyWithinFrom2 = Array.copyWithinToEnd([1, 2, 3, 4, 5], ~target=0, ~start=2)
125+
126+
let copyWithinFromRange1 = [1, 2, 3, 4, 5, 6]->Array.copyWithin(~target=1, ~start=2, ~end=5)
127+
let copyWithinFromRange2 = Array.copyWithin([1, 2, 3, 4, 5, 6], ~target=1, ~start=2, ~end=5)
128+
129+
let push1 = [1, 2, 3]->Array.push(4)
130+
let push2 = Array.push([1, 2, 3], 4)
131+
132+
let pushMany1 = [1, 2, 3]->Array.pushMany([4, 5])
133+
let pushMany2 = Array.pushMany([1, 2, 3], [4, 5])
134+
135+
let sortInPlace1 =
136+
["c", "a", "b"]->Array.toSorted((a, b) =>
137+
%todo("This needs a comparator function. Use `String.compare` for strings, etc.")
138+
)
139+
let sortInPlace2 = Array.toSorted(["c", "a", "b"], (a, b) =>
140+
%todo("This needs a comparator function. Use `String.compare` for strings, etc.")
141+
)
142+
143+
let unshift1 = [1, 2, 3]->Array.unshift(4)
144+
let unshift2 = Array.unshift([1, 2, 3], 4)
145+
146+
let unshiftMany1 = [1, 2, 3]->Array.unshiftMany([4, 5])
147+
let unshiftMany2 = Array.unshiftMany([1, 2, 3], [4, 5])
148+
149+
let reduce1 = [1, 2, 3]->Array.reduce(0, (acc, x) => acc + x)
150+
let reduce2 = Array.reduce([1, 2, 3], 0, (acc, x) => acc + x)
151+
152+
let spliceInPlace1 = [1, 2, 3]->Array.splice(~start=1, ~remove=1, ~insert=[4, 5])
153+
let spliceInPlace2 = Array.splice([1, 2, 3], ~start=1, ~remove=1, ~insert=[4, 5])
154+
155+
let removeFromInPlace1 = [1, 2, 3]->Array.removeInPlace(1)
156+
let removeFromInPlace2 = Array.removeInPlace([1, 2, 3], 1)
157+
158+
let removeCountInPlace1 = [1, 2, 3]->Array.splice(~start=1, ~remove=1, ~insert=[])
159+
let removeCountInPlace2 = Array.splice([1, 2, 3], ~start=1, ~remove=1, ~insert=[])
160+
161+
let reducei1 = [1, 2, 3]->Array.reduceWithIndex(0, (acc, x, i) => acc + x + i)
162+
let reducei2 = Array.reduceWithIndex([1, 2, 3], 0, (acc, x, i) => acc + x + i)
163+
164+
let reduceRight1 = [1, 2, 3]->Array.reduceRight(0, (acc, x) => acc + x)
165+
let reduceRight2 = Array.reduceRight([1, 2, 3], 0, (acc, x) => acc + x)
166+
167+
let reduceRighti1 = [1, 2, 3]->Array.reduceRightWithIndex(0, (acc, x, i) => acc + x + i)
168+
let reduceRighti2 = Array.reduceRightWithIndex([1, 2, 3], 0, (acc, x, i) => acc + x + i)
169+

tests/tools_tests/test.sh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ for file in src/migrate/*.{res,resi}; do
4343
done
4444

4545
# Move migrated files to expected directory so they can be compiled in the project
46-
#for file in src/migrate/StdlibMigration_*.res; do
47-
# expected_file="src/expected/$(basename $file).expected"
48-
# output="src/migrate/migrated/Migrated_$(basename $file)"
49-
# cp -f "$expected_file" "$output"
50-
#done
46+
for file in src/migrate/StdlibMigration_*.res; do
47+
expected_file="src/expected/$(basename $file).expected"
48+
output="src/migrate/migrated/Migrated_$(basename $file)"
49+
cp -f "$expected_file" "$output"
50+
done
5151

5252
warningYellow='\033[0;33m'
5353
successGreen='\033[0;32m'

0 commit comments

Comments
 (0)