Skip to content
Myr

fix: Fix handling of Symbol and non-enumerable properties in finalization / freeze. Fixes #1096, #1087, #1091 (#1105))

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 44363f7 · Browse files · 4 files+39 added−12 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+39 added−12 removed

  • tests:+17 added−0 removed
  • other:+22 added−12 removed

4 files+39 added−12 removed

modified:__tests__/base.js+16 added
2360 unmodified lines
2361 [immerable]: true,2362 [customSymbol]: 32363 })2364 })23652366 describe("#1096 / #1087 / proxies", () => {2367 const sym = Symbol()23682369 let state = {2370 id: 1,2371 [sym]: {x: 2}2372 }23732374 state = produce(state, draft => {2375 draft.id = 22376 draft[sym]2377 })23782379 expect(state[sym].x).toBe(2)2380 })2381}23822383function testLiteralTypes(produce) {2384 class Foo {}
70 unmodified lines
modified:__tests__/frozen.js+1 added
217 unmodified lines
218 expect(() => {219 state2.ref.state.x++220 }).not.toThrow()221 expect(state2.ref.state.x).toBe(2)222 expect(component.state.x).toBe(2)223 })224225 it("never freezes symbolic fields #590", () => {226 const component = {}
41 unmodified lines
modified:src/core/finalize.ts+10 added-7 removed
modified:src/utils/common.ts+12 added-5 removed
58 unmodified lines
59 if (!isDraft(value)) die(15, value)60 return value[DRAFT_STATE].base_61}6263/**64 * Each iterates a map, set or array.65 * Or, if any other kind of of object all it's own properties.66 * Regardless whether they are enumerable or symbols67 */68export function each<T extends Objectish>(69 obj: T,65 iter: (key: string | number, value: any, source: T) => void,66 enumerableOnly?: boolean70 iter: (key: string | number, value: any, source: T) => void71): void72export function each(obj: any, iter: any) {73 if (getArchtype(obj) === ArchType.Object) {70 Object.entries(obj).forEach(([key, value]) => {71 iter(key, value, obj)74 Reflect.ownKeys(obj).forEach(key => {75 iter(key, obj[key], obj)76 })77 } else {78 obj.forEach((entry: any, index: any) => iter(index, entry, obj))79 }
114 unmodified lines
194 if (getArchtype(obj) > 1 /* Map or Set */) {195 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any196 }197 Object.freeze(obj)194 if (deep) each(obj, (_key, value) => freeze(value, true), true)198 if (deep)199 // See #590, don't recurse into non-enumerable / Symbol properties when freezing200 // So use Object.entries (only string-like, enumerables) instead of each()201 Object.entries(obj).forEach(([key, value]) => freeze(value, true))202 return obj203}204205function dontMutateFrozenCollections() {
6 unmodified lines