Skip to content

Commit 4bd491d

Browse files
committed
Add assertion for Ember.A modifying behavior
1 parent a83385c commit 4bd491d

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

packages/@ember/-internals/runtime/tests/system/native_array/a_test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,14 @@ moduleFor(
4747
proxy.pushObjects([4, 5]);
4848
assert.deepEqual(original, [1, 2, 3, 4, 5], 'pushObjects works');
4949
}
50+
51+
['@test Ember.A adds warnings about modification to original']() {
52+
let original = [1, 2];
53+
A(original);
54+
55+
expectAssertion(() => {
56+
original.pushObject(1);
57+
});
58+
}
5059
}
5160
);

packages/@ember/array/index.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2051,6 +2051,19 @@ interface EmberAProxy<T = unknown> extends NativeArray<T> {}
20512051
// Ensure instanceof works correctly
20522052
Object.setPrototypeOf(EmberAProxy.prototype, Array.prototype);
20532053

2054+
const nativeArrayThrowers = Object.fromEntries(
2055+
Array.from(NativeArray.keys()).map((key) => {
2056+
return [
2057+
key,
2058+
() =>
2059+
assert(
2060+
`Ember.A was incorrectly modifying the original array. Do not call the EmberArray function ${key} on the original array. Instead, call it on the value returned by Ember.A.`
2061+
),
2062+
];
2063+
})
2064+
);
2065+
const ModifiedNativeArray = Mixin.create(nativeArrayThrowers);
2066+
20542067
let A: <T>(arr?: Array<T>) => NativeArray<T>;
20552068

20562069
if (ENV.EXTEND_PROTOTYPES.Array) {
@@ -2076,6 +2089,12 @@ if (ENV.EXTEND_PROTOTYPES.Array) {
20762089
// SAFETY: If it's a true native array and it is also an EmberArray then it should be an Ember NativeArray
20772090
return arr as NativeArray<T>;
20782091
} else {
2092+
// Remove this in Ember 5.0.
2093+
if (DEBUG) {
2094+
if (arr) {
2095+
ModifiedNativeArray.apply(arr);
2096+
}
2097+
}
20792098
return new EmberAProxy(arr ?? []);
20802099
}
20812100
};

0 commit comments

Comments
 (0)