fix: Fixed #620: Symbolic properties were not drafted or finalized correctly
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 7d6b57b · Browse files · 3 files·+46 added−6 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+46 added−6 removed
- tests:+33 added−0 removed
- other:+13 added−6 removed
Provenance
- Commit
91915cf- Tree
05f4350- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
7d6b57b
3 files changed+46 added−6 removed
modified:__tests__/base.js+33 added
2343 unmodified lines2344 expect(next.x).toBe(0)2345 expect(next.y).toBe(2)2346 expect(state.x).toBe(0)2347 })2348+2349+ describe("#620", () => {2350+ const customSymbol = Symbol("customSymbol")2351+2352+ class TestClass {2353+ [immerable] = true;2354+ [customSymbol] = 12355+ }2356+2357+ /* Create class instance */2358+ let test0 = new TestClass()2359+2360+ /* First produce. This works */2361+ let test1 = produce(test0, draft => {2362+ expect(draft[customSymbol]).toBe(1)2363+ draft[customSymbol] = 22364+ })2365+ expect(test1).toEqual({2366+ [immerable]: true,2367+ [customSymbol]: 22368+ })2369+2370+ /* Second produce. This does NOT work. See console error */2371+ /* With version 6.0.9, this works though */2372+ let test2 = produce(test1, draft => {2373+ expect(draft[customSymbol]).toBe(2)2374+ draft[customSymbol] = 32375+ })2376+ expect(test2).toEqual({2377+ [immerable]: true,2378+ [customSymbol]: 32379+ })2380+ })2381 }2382 2383 function testLiteralTypes(produce) {2384 class Foo {}68 unmodified linesmodified:src/plugins/es5.ts+10 added-5 removed
14 unmodified lines15 ProxyTypeES5Object,16 getCurrentScope,17 die,18 markChanged,19- objectTraps19+ objectTraps,20+ ownKeys21 } from "../internal"22 23 type ES5State = ES5ArrayState | ES5ObjectState24 24 unmodified lines49 const descriptors = Object.getOwnPropertyDescriptors(base)50 // Descriptors we want to skip:51 if (isArray) delete descriptors.length52 delete descriptors[DRAFT_STATE as any]52- for (let key in descriptors) {53+ const keys = ownKeys(descriptors)54+ for (let i = 0; i < keys.length; i++) {55+ const key: any = keys[i]56 descriptors[key] = proxyProperty(57 key,58 isArray || !!descriptors[key].enumerable59 )146 unmodified lines206 const {base_, draft_} = state207 208 // Search for added keys and changed keys. Start at the back, because209 // non-numeric keys are ordered by time of definition on the object.207- const keys = Object.keys(draft_)210+ const keys = ownKeys(draft_)211 for (let i = keys.length - 1; i >= 0; i--) {209- const key = keys[i]212+ const key: any = keys[i]213+ if (key === DRAFT_STATE) continue214 const baseValue = base_[key]215 // The `undefined` check is a fast path for pre-existing keys.216 if (baseValue === undefined && !has(base_, key)) {217 return true10 unmodified lines228 }229 230 // At this point, no keys were added or changed.231 // Compare key count to determine if keys were deleted.228- return keys.length !== Object.keys(base_).length232+ const baseIsDraft = !!base_[DRAFT_STATE as any]233+ return keys.length !== ownKeys(base_).length + (baseIsDraft ? 0 : 1) // + 1 to correct for DRAFT_STATE234 }235 236 function hasArrayChanges(state: ES5ArrayState) {237 const {draft_} = state34 unmodified linesmodified:src/utils/common.ts+3 added-1 removed
144 unmodified linesExpand all14514614714814914915015115215315415533 unmodified lines144 unmodified linesExpand allexport function shallowCopy(base: any) { if (Array.isArray(base)) return base.slice() const descriptors = Object.getOwnPropertyDescriptors(base) delete descriptors[DRAFT_STATE as any] for (let key in descriptors) { let keys = ownKeys(descriptors) for (let i = 0; i < keys.length; i++) { const key: any = keys[i] const desc = descriptors[key] if (desc.writable === false) { desc.writable = true desc.configurable = true33 unmodified lines