Skip to content

Commit 802ac55

Browse files
authored
Merge pull request #336 from knockout/fix/proxy-delete-property
Fix ko.proxy deleteProperty trap dropping the property key
2 parents 20132bd + bdac39c commit 802ac55

3 files changed

Lines changed: 34 additions & 1 deletion

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
"@tko/computed": patch
3+
---
4+
5+
Fix `ko.proxy` `deleteProperty` trap silently doing nothing
6+
7+
The `deleteProperty` handler on proxies built by `ko.proxy` declared only one
8+
parameter, named `property`. Per the Proxy spec, the trap is invoked with
9+
`(target, property)` — so the handler was receiving `target` (the internal
10+
`function(){}`) in place of the property key, stringifying it, and attempting
11+
to delete that bogus key. The actual property remained on both the mirror
12+
observable store and the underlying object, and its tracked observable stayed
13+
alive.
14+
15+
`delete proxied.foo` now correctly removes `foo` from both the proxy and the
16+
underlying object and returns `true`. Added a regression test to
17+
`proxyBehavior.ts` — the bug had been present since `ko.proxy` was introduced
18+
in 2017 and was untested.

packages/computed/spec/proxyBehavior.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,19 @@ describe('Proxy', function () {
106106
}
107107
expect(p.x2).to.equal(16)
108108
})
109+
110+
it('deletes properties from both the proxy and the underlying object', function () {
111+
const x: { a; b? } = { a: 1, b: 2 }
112+
const p = proxy(x)
113+
expect('b' in p).to.equal(true)
114+
expect(x.b).to.equal(2)
115+
116+
const result = delete p.b
117+
expect(result).to.equal(true)
118+
expect('b' in p).to.equal(false)
119+
expect('b' in x).to.equal(false)
120+
// Remaining properties are untouched.
121+
expect(p.a).to.equal(1)
122+
expect(x.a).to.equal(1)
123+
})
109124
})

packages/computed/src/proxy.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export function proxy(object) {
5454
object[prop] = value
5555
return true
5656
},
57-
deleteProperty(property) {
57+
deleteProperty(_target, property) {
5858
delete mirror[property as any]
5959
return delete object[property as any]
6060
},

0 commit comments

Comments
 (0)