Skip to content
Myr

Fixed configurability of array.length on proxies. Fixes #50

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

Also increased test coverage and disabled manually defining properties on drafts

authored by Michel Weststrate · committed by Michel Weststrate · parent 29e2258 · Browse files · 4 files+111 added−5 removed
This commit

State

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

Diff stats

4 files+111 added−5 removed

  • tests:+99 added−2 removed
  • other:+12 added−3 removed

4 files+111 added−5 removed

modified:__tests__/base.js+99 added-2 removed
246 unmodified lines
247 s.aProp = "hello"248 delete s.anObject249 obj.coffee = true250 s.messy = obj251 debugger251 })252 expect(nextState).not.toBe(baseState)253 expect(nextState.anArray).toBe(baseState.anArray)254 expect(enumerableOnly(nextState)).toEqual({
18 unmodified lines
273 expect(nextState.aProp).toBe(undefined)274 })275276 // ES implementation does't protect against all outside modifications, just some..278 if (name === "proxy") {277 if (lib === immerProxy) {278 it("should revoke the proxy of the baseState after immer function is executed", () => {279 let proxy280 const nextState = produce(baseState, s => {281 proxy = s
47 unmodified lines
329 expect(draft.stuffz).toBe("coffee")330 })331 })332333 it("should be able to get property descriptors from objects", () => {334 produce({a: 1}, draft => {335 expect("a" in draft).toBe(true)336 expect("b" in draft).toBe(false)337 expect(338 Reflect.ownKeys(draft).filter(x => typeof x === "string")339 ).toEqual(["a"])340341 expect(342 Object.getOwnPropertyDescriptor(draft, "a")343 ).toMatchObject({344 configurable: true,345 enumerable: true346 })347 draft.a = 2348 expect(349 Object.getOwnPropertyDescriptor(draft, "a")350 ).toMatchObject({351 configurable: true,352 enumerable: true353 })354 expect(355 Object.getOwnPropertyDescriptor(draft, "b")356 ).toBeUndefined()357 draft.b = 2358 expect(359 Object.getOwnPropertyDescriptor(draft, "b")360 ).toMatchObject({361 configurable: true,362 enumerable: true363 })364 expect("a" in draft).toBe(true)365 expect("b" in draft).toBe(true)366 expect(367 Reflect.ownKeys(draft).filter(x => typeof x === "string")368 ).toEqual(["a", "b"])369 })370 })371372 it("should be able to get property descriptors from arrays", () => {373 produce([1], draft => {374 expect(0 in draft).toBe(true)375 expect(1 in draft).toBe(false)376 expect("0" in draft).toBe(true)377 expect("1" in draft).toBe(false)378 expect(length in draft).toBe(true)379 expect(380 Reflect.ownKeys(draft).filter(x => typeof x === "string")381 ).toEqual(["0", "length"])382383 expect(384 Object.getOwnPropertyDescriptor(draft, "length")385 ).toMatchObject({386 configurable: false,387 enumerable: false388 })389 draft[0] = 2390 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(391 {392 configurable: true,393 enumerable: true394 }395 )396 expect(Object.getOwnPropertyDescriptor(draft, 0)).toMatchObject(397 {398 configurable: true,399 enumerable: true400 }401 )402 expect(403 Object.getOwnPropertyDescriptor(draft, 1)404 ).toBeUndefined()405 draft[1] = 2406 expect(Object.getOwnPropertyDescriptor(draft, 1)).toMatchObject(407 {408 configurable: true,409 enumerable: true410 }411 )412 expect(413 Reflect.ownKeys(draft).filter(x => typeof x === "string")414 ).toEqual(["0", "1", "length"])415 })416 })417418 if (lib === immerProxy)419 it("should not be possible to set property descriptors", () => {420 expect(() => {421 produce({}, draft => {422 Object.defineProperty(draft, "xx", {423 enumerable: true,424 writeable: true,425 value: 2426 })427 })428 }).toThrowError(/not support/)429 })430431 afterEach(() => {432 expect(baseState).toBe(origBaseState)433 expect(baseState).toEqual(createBaseState())434 })
26 unmodified lines
modified:changelog.md+5 added
modified:immer.js+5 added-3 removed
152 unmodified lines
153 const owner = this.modified154 ? this.copy155 : prop in this.proxies ? this.proxies : this.base156 const descriptor = Reflect.getOwnPropertyDescriptor(owner, prop)157 if (descriptor) descriptor.configurable = true // XXX: is this really needed?157 if (descriptor && !(Array.isArray(owner) && prop === "length"))158 descriptor.configurable = true159 return descriptor160 }161162 defineProperty(property, descriptor) {162 this.markChanged()163 Object.defineProperty(this.copy, property, descriptor)163 throw new Error(164 "Immer does currently not support defining properties on draft objects"165 )166 }167168 markChanged() {169 if (!this.modified) {
109 unmodified lines
modified:readme.md+2 added
149 unmodified lines
150151This mechanism can also nicely be leveraged to further simplify our example reducer:152153```javascript154import produce from 'immer'155156const byId = produce((draft, action) => {157 switch (action.type) {158 case RECEIVE_PRODUCTS:159 action.products.forEach(product => {
129 unmodified lines