Skip to content
Myr

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

3 files+46 added−6 removed

modified:__tests__/base.js+33 added
2343 unmodified lines
2344 expect(next.x).toBe(0)2345 expect(next.y).toBe(2)2346 expect(state.x).toBe(0)2347 })23482349 describe("#620", () => {2350 const customSymbol = Symbol("customSymbol")23512352 class TestClass {2353 [immerable] = true;2354 [customSymbol] = 12355 }23562357 /* Create class instance */2358 let test0 = new TestClass()23592360 /* 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 })23692370 /* 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}23822383function testLiteralTypes(produce) {2384 class Foo {}
68 unmodified lines
modified:src/plugins/es5.ts+10 added-5 removed
14 unmodified lines
15 ProxyTypeES5Object,16 getCurrentScope,17 die,18 markChanged,19 objectTraps19 objectTraps,20 ownKeys21} from "../internal"2223type ES5State = ES5ArrayState | ES5ObjectState24
24 unmodified lines
49 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 lines
206 const {base_, draft_} = state207208 // 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 true
10 unmodified lines
228 }229230 // 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 }235236 function hasArrayChanges(state: ES5ArrayState) {237 const {draft_} = state
34 unmodified lines
modified:src/utils/common.ts+3 added-1 removed