From 8c72782ec10460ea4882eaf66773680c03564f71 Mon Sep 17 00:00:00 2001 From: waw666waw666 <184799977+waw666waw666@users.noreply.github.com> Date: Thu, 12 Nov 2026 17:32:30 +0000 Subject: [PATCH] fix: finalize drafts nested in inserted values after the array methods plugin moves them --- __tests__/base.js | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/core/finalize.ts | 40 ++++++++++++++++++++++++---------------- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/__tests__/base.js b/__tests__/base.js index 2ed0ca8ab..d8449a5a6 100644 --- a/__tests__/base.js +++ b/__tests__/base.js @@ -3412,6 +3412,50 @@ function runBaseTest( expect(next[1].nestedArray).toEqual(["a", "b"]) }) + describe("keeps drafts nested in an inserted value after the array moves it", () => { + const cases = [ + ["unshift", s => s.unshift({id: 0}), [{id: 0}, {id: 1}, {id: 2}]], + ["shift", s => s.shift(), [{id: 2}]], + ["splice", s => s.splice(0, 1), [{id: 2}]], + ["reverse", s => s.reverse(), [{id: 2}, {id: 1}]], + [ + "sort", + s => s.sort((a, b) => (b.id ?? 9) - (a.id ?? 9)), + [{id: 2}, {id: 1}] + ] + ] + for (const [method, move, rest] of cases) { + it(`after push() then ${method}()`, () => { + const base = [{id: 1}, {id: 2}] + const [next, patches, inversePatches] = produceWithPatches( + base, + s => { + s.push({ref: s[1]}) + move(s) + } + ) + const inserted = next.find(item => "ref" in item) + expect(isDraft(inserted.ref)).toBe(false) + expect(inserted.ref).toBe(base[1]) + expect(next.filter(item => !("ref" in item))).toEqual(rest) + expect(applyPatches(base, patches)).toEqual(next) + expect(applyPatches(next, inversePatches)).toEqual(base) + }) + } + + it("when the nested draft is modified", () => { + const base = {list: [{id: 1}]} + const next = produce(base, s => { + s.list.push({ref: s.list[0]}) + s.list[0].id = 5 + s.list.unshift({id: 0}) + }) + expect(isDraft(next.list[2].ref)).toBe(false) + expect(next).toEqual({list: [{id: 0}, {id: 5}, {ref: {id: 5}}]}) + expect(next.list[2].ref).toBe(next.list[1]) + }) + }) + it("supports assigning undefined to an existing property", () => { const nextState = produce(baseState, s => { s.aProp = undefined diff --git a/src/core/finalize.ts b/src/core/finalize.ts index 9a76e2b81..5cb0ae2c6 100644 --- a/src/core/finalize.ts +++ b/src/core/finalize.ts @@ -247,22 +247,30 @@ export function handleCrossReference( handleValue(value, scope_.handledSet_, scope_) } } else { - // Maps/objects - if (get(targetCopy, key, target.type_) === value) { - if ( - scope_.drafts_.length > 1 && - ((target as Exclude).assigned_!.get(key) ?? - false) === true && - target.copy_ - ) { - // This might be a non-draft value that has drafts - // inside. We do need to recurse here to handle those. - handleValue( - get(target.copy_, key, target.type_), - scope_.handledSet_, - scope_ - ) - } + // Maps/objects/arrays + const assigned_ = (target as Exclude).assigned_! + let isPresent = get(targetCopy, key, target.type_) === value + let isAssigned = isPresent && (assigned_.get(key) ?? false) === true + if ( + !isPresent && + target.type_ === ArchType.Array && + (target as ProxyArrayState).allIndicesReassigned_ + ) { + // The array methods plugin shifts and reorders elements on the + // copy directly (shift, unshift, splice, sort, reverse), so the + // value may have moved away from the index it was inserted at. + // Every index of such an array counts as assigned. + isPresent = isAssigned = targetCopy.includes(value) + } + if ( + isPresent && + isAssigned && + scope_.drafts_.length > 1 && + target.copy_ + ) { + // This might be a non-draft value that has drafts + // inside. We do need to recurse here to handle those. + handleValue(value, scope_.handledSet_, scope_) } } })