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
Provenance
- Commit
8949a3e- Tree
860c4aa- Author
Michel Weststrate <mweststrate@gmail.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
44363f7
4 files changed+39 added−12 removed
modified:__tests__/base.js+16 added
2360 unmodified lines2361 [immerable]: true,2362 [customSymbol]: 32363 })2364 })2365+2366+ describe("#1096 / #1087 / proxies", () => {2367+ const sym = Symbol()2368+2369+ let state = {2370+ id: 1,2371+ [sym]: {x: 2}2372+ }2373+2374+ state = produce(state, draft => {2375+ draft.id = 22376+ draft[sym]2377+ })2378+2379+ expect(state[sym].x).toBe(2)2380+ })2381 }2382 2383 function testLiteralTypes(produce) {2384 class Foo {}70 unmodified linesmodified:__tests__/frozen.js+1 added
217 unmodified lines218 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 })224 225 it("never freezes symbolic fields #590", () => {226 const component = {}41 unmodified linesmodified:src/core/finalize.ts+10 added-7 removed
56 unmodified lines57585960616263646162656364656677 unmodified lines1441451461471511521481491501511521531541551561571581596 unmodified lines56 unmodified lines
const state: ImmerState = value[DRAFT_STATE] // A plain object, might need freezing, might contain drafts if (!state) { each( value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path), each(value, (key, childValue) => finalizeProperty(rootScope, state, value, key, childValue, path) true // See #590, don't recurse into non-enumerable of non drafted objects ) return value } // Never finalize drafts owned by another scope.77 unmodified lines // See add-data.js perf test return } finalize(rootScope, childValue) // immer deep freezes plain objects, so if there is no parent state, we freeze as well if (!parentState || !parentState.scope_.parent_) // Immer deep freezes plain objects, so if there is no parent state, we freeze as well // Per #590, we never freeze symbolic properties. Just to make sure don't accidentally interfere // with other frameworks. if ( (!parentState || !parentState.scope_.parent_) && typeof prop !== "symbol" && Object.prototype.propertyIsEnumerable.call(targetObject, prop) ) maybeFreeze(rootScope, childValue) }}
6 unmodified linesmodified:src/utils/common.ts+12 added-5 removed
58 unmodified lines59 if (!isDraft(value)) die(15, value)60 return value[DRAFT_STATE].base_61 }62 63+/**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+ */68 export 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 ): void72 export 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 lines194 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 }204 205 function dontMutateFrozenCollections() {6 unmodified lines