Skip to content
Myr

fix: do not record a deletion when a base value is written back (#1295)

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

The set trap has a fast path for assigning a property its own base value back after Immer drafted it. That path recorded the property in `assigned_` as `false`, which patch generation reads as a deletion, so a producer that writes a base value back and then changes anything else on the same object emits a spurious "remove" patch. Replaying the patches then drops a property that is still present in the produced result. The property still holds its base value at that point, so nothing was assigned and nothing was deleted. Remove the entry instead.

authored by Bao Nguyen · committed by GitHub · parent beb5bc3 · Browse files · 2 files+35 added−1 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+35 added−1 removed

  • tests:+32 added−0 removed
  • other:+3 added−1 removed

2 files+35 added−1 removed

modified:__tests__/patch.js+32 added
1603 unmodified lines
1604 expect(() => {1605 applyPatches({a: null}, [{op: "add", path: ["a", "b"], value: 1}])1606 }).toThrow(isProd ? "18" : "Cannot apply patch, path doesn't resolve: a/b")1607})16081609describe("assigning a base value back over its own draft", () => {1610 // Reading a property drafts it; writing the base value back leaves the1611 // property untouched, so it must not show up in the patches at all.1612 const rootChild = {id: 1}1613 runPatchTests(1614 "root level",1615 {child: rootChild, other: 0},1616 d => {1617 d.child1618 d.child = rootChild1619 d.other = 11620 },1621 [{op: "replace", path: ["other"], value: 1}],1622 [{op: "replace", path: ["other"], value: 0}],1623 {child: {id: 1}, other: 1}1624 )16251626 const nestedChild = {id: 1}1627 runPatchTests(1628 "nested",1629 {a: {child: nestedChild, other: 0}},1630 d => {1631 d.a.child1632 d.a.child = nestedChild1633 d.a.other = 11634 },1635 [{op: "replace", path: ["a", "other"], value: 1}],1636 [{op: "replace", path: ["a", "other"], value: 0}],1637 {a: {child: {id: 1}, other: 1}}1638 )1639})
modified:src/core/proxy.ts+3 added-1 removed
184 unmodified lines
185 // special case, if we assigning the original value to a draft, we can ignore the assignment186 const currentState: ProxyObjectState = current?.[DRAFT_STATE]187 if (currentState && currentState.base_ === value) {188 state.copy_![prop] = value189 // The property still holds its base value, so nothing was assigned190 // and nothing was deleted. `false` here reads as a deletion later.189 state.assigned_!.set(prop, false)191 state.assigned_!.delete(prop)192 return true193 }194 if (195 is(value, current) &&
166 unmodified lines