Skip to content
Myr

fix: undefined assigned to a prototype-inherited key gets dropped (#1262)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

If an object's prototype has a key set to undefined and you assign undefined to that same key on a draft, immer treats it as a no-op since `prop in state.copy_` walks the prototype chain and finds it "already there." No own property gets created, no patch gets recorded. Swapped that for the has() own-property check that's already used a few lines up for the same purpose, and added a regressions test.

authored by Alexander Kireyev · committed by GitHub · parent 48fc378 · Browse files · 2 files+24 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−2 removed

  • tests:+23 added−1 removed
  • other:+1 added−1 removed

2 files+24 added−2 removed

modified:__tests__/regressions.js+23 added-1 removed
3 unmodified lines
4 nothing,5 original,6 isDraft,7 immerable,8 enableMapSet8 enableMapSet,9 enablePatches10} from "../src/immer"1112enableMapSet()13enablePatches()1415runBaseTest("proxy (no freeze)", true, false)16runBaseTest("proxy (autofreeze)", true, true)17runBaseTest("es5 (no freeze)", false, false)
235 unmodified lines
253 baz: undefined254 })255 })256257 test("#1160 assigning undefined to a key only present on the prototype is still stored as an own property", () => {258 const proto = {[immerable]: true, name: undefined}259 const state = Object.create(proto)260261 expect(Object.hasOwnProperty.call(state, "name")).toBe(false)262263 const [newState, patches] = produceWithPatches(state, draft => {264 draft.name = undefined265 })266267 expect(Object.hasOwnProperty.call(newState, "name")).toBe(true)268 expect(patches).toEqual([269 {270 op: "add",271 path: ["name"],272 value: undefined273 }274 ])275 })276277 test("Nested and chained produce calls throw 'Cannot perform 'get' on a proxy that has been revoked' error", () => {278 const state = {279 foo: {280 bar: {
23 unmodified lines
modified:src/core/proxy.ts+1 added-1 removed
239 unmodified lines
240241 if (242 (state.copy_![prop] === value &&243 // special case: handle new props with value 'undefined'244 (value !== undefined || prop in state.copy_)) ||244 (value !== undefined || has(state.copy_, prop, state.type_))) ||245 // special case: NaN246 (Number.isNaN(value) && Number.isNaN(state.copy_![prop]))247 )248 return true
132 unmodified lines