Skip to content
Myr

fix: Don't recurse into non-enumerable members when freezing tree. Fixes #590

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

authored by Michel Weststrate · committed by GitHub · parents 00caea1, d64c2bd · Browse files · 4 files+37 added−9 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+37 added−9 removed

  • tests:+23 added−2 removed
  • other:+14 added−7 removed

4 files+37 added−9 removed

modified:__tests__/frozen.js+23 added-2 removed
21 unmodified lines
22 it("never freezes the base state", () => {23 const base = {arr: [1], obj: {a: 1}}24 const next = produce(base, draft => {25 draft.arr.push(1)26 debugger26 })27 expect(isFrozen(base)).toBeFalsy()28 expect(isFrozen(base.arr)).toBeFalsy()29 expect(isFrozen(next)).toBeTruthy()
159 unmodified lines
189 const frozen = produce(base, draft => {})190191 // https://github.com/immerjs/immer/issues/472192 produce(frozen, draft => {194 // if (useProxies) debugger193 ;["b", "c"].forEach(other => {194 const m = draft.map.get(other)195196 m.delete("a")197 })198 })199 })200201 it("never freezes non-enumerable fields #590", () => {202 const component = {}203 Object.defineProperty(component, "state", {204 value: {x: 1},205 enumerable: false,206 writable: true,207 configurable: true208 })209210 const state = {211 x: 1212 }213214 const state2 = produce(state, draft => {215 draft.ref = component216 })217218 expect(() => {219 state2.ref.state.x++220 }).not.toThrow()221 expect(state2.ref.state.x).toBe(2)222 })223 })224}
modified:.vscode/settings.json+2 added-1 removed
modified:src/core/finalize.ts+5 added-2 removed
modified:src/utils/common.ts+7 added-4 removed
65 unmodified lines
66 : /* istanbul ignore next */ Object.getOwnPropertyNames6768export function each<T extends Objectish>(69 obj: T,70 iter: (key: string | number, value: any, source: T) => void70 iter: (key: string | number, value: any, source: T) => void,71 enumerableOnly?: boolean72): void72export function each(obj: any, iter: any) {73export function each(obj: any, iter: any, enumerableOnly = false) {74 if (getArchtype(obj) === ArchtypeObject) {74 ownKeys(obj).forEach(key => iter(key, obj[key], obj))75 ;(enumerableOnly ? Object.keys : ownKeys)(obj).forEach(key =>76 iter(key, obj[key], obj)77 )78 } else {79 obj.forEach((entry: any, index: any) => iter(index, entry, obj))80 }81}
98 unmodified lines
180 if (getArchtype(obj) > 1 /* Map or Set */) {181 obj.set = obj.add = obj.clear = obj.delete = dontMutateFrozenCollections as any182 }183 Object.freeze(obj)181 if (deep) each(obj, (_, value) => freeze(value, true))184 if (deep) each(obj, (key, value) => freeze(value, true), true)185}186187function dontMutateFrozenCollections() {188 die(2)
1 unmodified line