Skip to content

Commit

Permalink
Merge d511b52 into 8a1c84c
Browse files Browse the repository at this point in the history
  • Loading branch information
aleclarson committed Dec 15, 2018
2 parents 8a1c84c + d511b52 commit a0634dc
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
19 changes: 19 additions & 0 deletions __tests__/base.js
Expand Up @@ -122,6 +122,25 @@ function runBaseTest(name, useProxies, freeze, useListener) {
expect(nextState.anObject.nested).toBe(undefined)
})

// Found by: https://github.com/mweststrate/immer/pull/267
it("can delete props added in the producer", () => {
const nextState = produce(
baseState,
s => {
s.anObject.test = true
delete s.anObject.test
},
listener
)
if (useProxies) {
expect(nextState).not.toBe(baseState)
expect(nextState).toEqual(baseState)
} else {
// The copy is avoided in ES5.
expect(nextState).toBe(baseState)
}
})

it("ignores single non-modification", () => {
const nextState = produce(
baseState,
Expand Down
11 changes: 7 additions & 4 deletions src/proxy.js
Expand Up @@ -107,8 +107,11 @@ function set(state, prop, value) {
}

function deleteProperty(state, prop) {
state.assigned[prop] = false
markChanged(state)
// The `undefined` check is a fast path for pre-existing keys.
if (state.base[prop] !== undefined || prop in state.base) {
state.assigned[prop] = false
markChanged(state)
}
delete state.copy[prop]
return true
}
Expand All @@ -117,8 +120,8 @@ function getOwnPropertyDescriptor(state, prop) {
const owner = state.modified
? state.copy
: has(state.proxies, prop)
? state.proxies
: state.base
? state.proxies
: state.base
const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)
if (descriptor && !(Array.isArray(owner) && prop === "length"))
descriptor.configurable = true
Expand Down

0 comments on commit a0634dc

Please sign in to comment.