Skip to content

Commit

Permalink
fix: use Array.prototype.slice() for copying arrays. Fixes #650
Browse files Browse the repository at this point in the history
  • Loading branch information
mweststrate committed Aug 25, 2020
2 parents 8f28982 + bb40c36 commit bf90358
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions __tests__/regressions.js
Expand Up @@ -140,5 +140,23 @@ function runBaseTest(name, useProxies, autoFreeze, useListener) {
})
expect(result).toEqual(new Set())
})

test("#650 - changes with overridden arr.slice() fail", () => {
const data = {
foo: [
{
isActive: false
}
]
}
// That's roughly what seamless-immutable does
data.foo.slice = (...args) =>
Object.freeze(Array.prototype.slice.call(data.foo, ...args))

const newData = produce(data, draft => {
draft.foo[0].isActive = true
})
expect(newData.foo[0].isActive).toBe(true)
})
})
}
2 changes: 1 addition & 1 deletion src/utils/common.ts
Expand Up @@ -154,7 +154,7 @@ export function latest(state: ImmerState): any {

/*#__PURE__*/
export function shallowCopy(base: any) {
if (Array.isArray(base)) return base.slice()
if (Array.isArray(base)) return Array.prototype.slice.call(base)
const descriptors = getOwnPropertyDescriptors(base)
delete descriptors[DRAFT_STATE as any]
let keys = ownKeys(descriptors)
Expand Down

0 comments on commit bf90358

Please sign in to comment.