fix: handle deletions of state proxy properties (#13008)

pull/13015/head
Rich Harris 3 months ago committed by GitHub
parent 96117fa44d
commit cb4f82f811
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: always return true from `deleteProperty` trap

@ -0,0 +1,5 @@
---
'svelte': patch
---
fix: handle deletions of previously-unread state proxy properties

@ -19,7 +19,7 @@ import * as e from './errors.js';
* @param {T} value * @param {T} value
* @param {ProxyMetadata | null} [parent] * @param {ProxyMetadata | null} [parent]
* @param {Source<T>} [prev] dev mode only * @param {Source<T>} [prev] dev mode only
* @returns {ProxyStateObject<T> | T} * @returns {T}
*/ */
export function proxy(value, parent = null, prev) { export function proxy(value, parent = null, prev) {
// if non-proxyable, or is already a proxy, return `value` // if non-proxyable, or is already a proxy, return `value`
@ -91,17 +91,17 @@ export function proxy(value, parent = null, prev) {
deleteProperty(target, prop) { deleteProperty(target, prop) {
var s = sources.get(prop); var s = sources.get(prop);
var exists = s !== undefined ? s.v !== UNINITIALIZED : prop in target;
if (s !== undefined) { if (s === undefined) {
if (prop in target) {
sources.set(prop, source(UNINITIALIZED));
}
} else {
set(s, UNINITIALIZED); set(s, UNINITIALIZED);
}
if (exists) {
update_version(version); update_version(version);
} }
return exists; return true;
}, },
get(target, prop, receiver) { get(target, prop, receiver) {

@ -85,3 +85,14 @@ test('does not re-proxy proxies', () => {
assert.equal(inner.count, 1); assert.equal(inner.count, 1);
assert.equal(outer.inner.count, 1); assert.equal(outer.inner.count, 1);
}); });
test('deletes a property', () => {
const state = proxy({ a: 1, b: 2 } as { a?: number; b?: number; c?: number });
delete state.a;
assert.equal(JSON.stringify(state), '{"b":2}');
delete state.a;
// deleting a non-existent property should succeed
delete state.c;
});

Loading…
Cancel
Save