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
Provenance
- Commit
497d1a0- Tree
6c420ac- Author
Michel Weststrate <mweststrate@fb.com>- Committer
GitHub <noreply@github.com>- Parent 1
00caea1- Parent 2
d64c2bd
4 files changed+37 added−9 removed
modified:__tests__/frozen.js+23 added-2 removed
21 unmodified lines22 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 lines189 const frozen = produce(base, draft => {})190 191 // 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)195 196 m.delete("a")197 })198 })199 })200+201+ 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+ })209+210+ const state = {211+ x: 1212+ }213+214+ const state2 = produce(state, draft => {215+ draft.ref = component216+ })217+218+ 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
123345{ "javascript.validate.enable": false, "typescript.tsdk": "node_modules/typescript/lib" "typescript.tsdk": "node_modules/typescript/lib", "jest.enableInlineErrorMessages": true}modified:src/core/finalize.ts+5 added-2 removed
62 unmodified lines63646566676867686970717273747586 unmodified lines62 unmodified lines
const state: ImmerState = value[DRAFT_STATE] // A plain object, might need freezing, might contain drafts if (!state) { each( value, each(value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path) (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path), true // See #590, don't recurse into non-enumarable of non drafted objects ) return value } // Never finalize drafts owned by another scope.86 unmodified linesmodified:src/utils/common.ts+7 added-4 removed
65 unmodified lines66 : /* istanbul ignore next */ Object.getOwnPropertyNames67 68 export 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 ): void72-export function each(obj: any, iter: any) {73+export 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 lines180 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 }186 187 function dontMutateFrozenCollections() {188 die(2)1 unmodified line