Made sure maps and sets become truly immutable
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 7d32b7c · Browse files · 3 files·+47 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+47 added−7 removed
- tests:+33 added−0 removed
- other:+14 added−7 removed
Provenance
- Commit
82db631- Tree
b92ddda- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
7d32b7c
3 files changed+47 added−7 removed
modified:__tests__/frozen.js+33 added
131 unmodified linesExpand all132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170131 unmodified linesExpand all draft.a.push(3) }) expect(n).toEqual({c: true, a: [3]}) })
it("will freeze maps", () => { const base = new Map() debugger const res = produce(base, draft => { draft.set("a", 1) }) expect(() => res.set("b", 2)).toThrow( "This object has been frozen and should not be mutated" ) expect(() => res.clear()).toThrow( "This object has been frozen and should not be mutated" ) expect(() => res.delete("b")).toThrow( "This object has been frozen and should not be mutated" ) })
it("will freeze sets", () => { const base = new Set() const res = produce(base, draft => { base.add(1) }) expect(() => base.add(2)).toThrow( "This object has been frozen and should not be mutated" ) expect(() => base.delete(1)).toThrow( "This object has been frozen and should not be mutated" ) expect(() => base.clear()).toThrow( "This object has been frozen and should not be mutated" ) }) })}modified:src/common.js+11 added-3 removed
228 unmodified lines229 for (const key in obj) cloned[key] = clone(obj[key])230 return cloned231 }232 233-export function deepFreeze(obj) {233+export function freeze(obj, deep = false) {234 if (!isDraftable(obj) || isDraft(obj) || Object.isFrozen(obj)) return235+ if (isSet(obj)) {236+ obj.add = obj.clear = obj.delete = dontMutateFrozenCollections237+ } else if (isMap(obj)) {238+ obj.set = obj.clear = obj.delete = dontMutateFrozenCollections239+ }240 Object.freeze(obj)236- if (Array.isArray(obj)) obj.forEach(deepFreeze)237- else for (const key in obj) deepFreeze(obj[key])241+ if (deep) each(obj, (_, value) => freeze(value, true))242 }243+244+function dontMutateFrozenCollections() {245+ throw new Error("This object has been frozen and should not be mutated")246+}modified:src/immer.js+3 added-4 removed
13 unmodified lines14 isEnumerable,15 shallowCopy,16 DRAFT_STATE,17 NOTHING,18- deepFreeze18+ freeze19 } from "./common"20 import {ImmerScope} from "./scope"21 22 function verifyMinified() {}226 unmodified lines249 250 // At this point, all descendants of `state.copy` have been finalized,251 // so we can be sure that `scope.canAutoFreeze` is accurate.252 if (this.autoFreeze && scope.canAutoFreeze) {253- Object.freeze(state.copy)253+ freeze(state.copy, false)254 }255 256 if (path && scope.patches) {257 generatePatches(state, path, scope.patches, scope.inversePatches)65 unmodified lines323 return root324 }325 maybeFreeze(value, deep = false) {326 if (this.autoFreeze && !isDraft(value)) {327- if (deep) deepFreeze(value)328- else Object.freeze(value)327+ freeze(value, deep)328 }329 }330 }331 18 unmodified lines