Skip to content

Commit 16a4386

Browse files
srujzsCommit Queue
authored andcommitted
[dart:js_interop] Add JSArray.add
Fixes #59830 Migration from Lists to JSArray sometimes involve usages of List.add. In the migration, if List.add was used, it would become JSAnyOperationExtension.add, which is the JS + operator. This then leads to bugs where users mistakenly assumed that something similar to List.add was occurring. CoreLibraryReviewExempt: Wasm and JS backends only. Change-Id: I2bb6684316725167a8fa65fd51483f2717788dad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/443182 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Srujan Gaddam <[email protected]> Reviewed-by: Stephen Adams <[email protected]>
1 parent 47ccb55 commit 16a4386

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929

3030
[#56468]: https://github.com/dart-lang/sdk/issues/56468
3131

32+
#### `dart:js_interop`
33+
34+
- `JSArray.add` is added to avoid cases where during migration from `List` to
35+
`JSArray`, `JSAnyOperatorExtension.add` is accidentally used. See [#59830][]
36+
for more details.
37+
38+
[#59830]: https://github.com/dart-lang/sdk/issues/59830
39+
3240
## 3.9.0
3341

3442
**Released on:** Unreleased

sdk/lib/js_interop/js_interop.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,14 @@ extension type JSArray<T extends JSAny?>._(JSArrayRepType _jsArray)
184184
/// Sets the [value] at [position] in this `Array`.
185185
@Since('3.6')
186186
external void operator []=(int position, T value);
187+
188+
/// Adds [value] to the end of this `Array`, extending the length by one.
189+
// This maps to `List.add` to avoid accidental usage of
190+
// `JSAnyOperatorExtension.add` when migrating `List`s to `JSArray`s. See
191+
// https://github.com/dart-lang/sdk/issues/59830.
192+
@Since('3.10')
193+
@JS('push')
194+
external void add(T value);
187195
}
188196

189197
/// A JavaScript `Promise` or a promise-like object.

tests/lib/js/static_interop_test/js_types_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ void syncTests() {
246246
arrN = JSArray.from<JSNumber>(arrN);
247247
Expect.equals(1, arrN.length);
248248
Expect.equals(1, arrN[0].toDartInt);
249+
arrN.add(0.toJS);
250+
Expect.equals(2, arrN.length);
251+
Expect.equals(0, arrN[1].toDartInt);
249252
final typedArray = JSArray.from<JSNumber>(JSUint8Array.withLength(4));
250253
Expect.equals(4, typedArray.length);
251254
Expect.equals(0, typedArray[0].toDartInt);

0 commit comments

Comments
 (0)