fix: incorrect patches for `delete` on arrays. Fixes #879
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Michel Weststrate · committed by Michel Weststrate · parent 0f96270 · Browse files · 3 files·+40 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
3 files+40 added−2 removed
- tests:+30 added−1 removed
- other:+10 added−1 removed
Provenance
- Commit
d91a659- Tree
4833a82- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
0f96270
3 files changed+40 added−2 removed
modified:__tests__/patch.js+30 added-1 removed
13 unmodified lines14 jest.setTimeout(1000)15 16 const isProd = process.env.NODE_ENV === "production"17 18-function runPatchTest(base, producer, patches, inversePathes) {18+function runPatchTest(base, producer, patches, inversePathes, expectedResult) {19 let resultProxies, resultEs520 21 function runPatchTestHelper() {22 let recordedPatches2 unmodified lines25 recordedPatches = p26 recordedInversePatches = i27 })28 29+ if (expectedResult !== undefined)30+ test("produced the correct result", () => {31+ expect(res).toEqual(expectedResult)32+ })33+34 test("produces the correct patches", () => {35 expect(recordedPatches).toEqual(patches)36 if (inversePathes) expect(recordedInversePatches).toEqual(inversePathes)37 })1355 unmodified lines1393 expect(res).toEqual(true)1394 expect(patches).toEqual([{op: "replace", path: [], value: true}])1395 }1396 })1397+1398+describe("#879 delete item from array", () => {1399+ runPatchTest(1400+ [1, 2, 3],1401+ draft => {1402+ delete draft[1]1403+ },1404+ [{op: "replace", path: [1], value: undefined}],1405+ [{op: "replace", path: [1], value: 2}],1406+ [1, undefined, 3]1407+ )1408+})1409+1410+describe("#879 delete item from array - 2", () => {1411+ runPatchTest(1412+ [1, 2, 3],1413+ draft => {1414+ delete draft[2]1415+ },1416+ [{op: "replace", path: [2], value: undefined}],1417+ [{op: "replace", path: [2], value: 3}],1418+ [1, 2, undefined]1419+ )1420+})modified:src/core/proxy.ts+2 added-1 removed
221 unmodified lines222 }223 })224 arrayTraps.deleteProperty = function(state, prop) {225 if (__DEV__ && isNaN(parseInt(prop as any))) die(13)226- return objectTraps.deleteProperty!.call(this, state[0], prop)226+ // @ts-ignore227+ return arrayTraps.set!.call(this, state, prop, undefined)228 }229 arrayTraps.set = function(state, prop, value) {230 if (__DEV__ && prop !== "length" && isNaN(parseInt(prop as any))) die(14)231 return objectTraps.set!.call(this, state[0], prop, value, state[0])49 unmodified linesmodified:src/plugins/es5.ts+8 added
193 unmodified lines194 const min = Math.min(draft_.length, base_.length)195 196 for (let i = 0; i < min; i++) {197 // Only untouched indices trigger recursion.198+ if (!draft_.hasOwnProperty(i)) {199+ assigned_[i] = true200+ }201 if (assigned_[i] === undefined) markChangesRecursively(draft_[i])202 }203 }204 }38 unmodified lines243 // those last items will have no intercepting property.244 // So if there is no own descriptor on the last position, we know that items were removed and added245 // N.B.: splice, unshift, etc only shift values around, but not prop descriptors, so we only have to check246 // the last one247+ // last descriptor can be not a trap, if the array was extended248 const descriptor = Object.getOwnPropertyDescriptor(249 draft_,250 draft_.length - 1251 )252 // descriptor can be null, but only for newly created sparse arrays, eg. new Array(10)253 if (descriptor && !descriptor.get) return true254+ // if we miss a property, it has been deleted, so array probobaly changed255+ for (let i = 0; i < draft_.length; i++) {256+ if (!draft_.hasOwnProperty(i)) return true257+ }258 // For all other cases, we don't have to compare, as they would have been picked up by the index setters259 return false260 }261 16 unmodified lines