fix: Trigger setters with the correct context, fixes #604
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 f97d037 · Browse files · 2 files·+104 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+104 added−3 removed
- tests:+82 added−0 removed
- other:+22 added−3 removed
Provenance
- Commit
2697430- Tree
ab73080- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent
f97d037
2 files changed+104 added−3 removed
added:__tests__/regressions.js+82 added
1+"use strict"2+import {3+ Immer,4+ nothing,5+ original,6+ isDraft,7+ immerable,8+ enableAllPlugins9+} from "../src/immer"10+11+enableAllPlugins()12+13+runBaseTest("proxy (no freeze)", true, false)14+runBaseTest("proxy (autofreeze)", true, true)15+runBaseTest("es5 (no freeze)", false, false)16+runBaseTest("es5 (autofreeze)", false, true)17+18+function runBaseTest(name, useProxies, autoFreeze, useListener) {19+ const listener = useListener ? function() {} : undefined20+ const {produce, produceWithPatches} = createPatchedImmer({21+ useProxies,22+ autoFreeze23+ })24+25+ // When `useListener` is true, append a function to the arguments of every26+ // uncurried `produce` call in every test. This makes tests easier to read.27+ function createPatchedImmer(options) {28+ const immer = new Immer(options)29+30+ const {produce} = immer31+ immer.produce = function(...args) {32+ return typeof args[1] === "function" && args.length < 333+ ? produce(...args, listener)34+ : produce(...args)35+ }36+37+ return immer38+ }39+40+ describe(`regressions ${name}`, () => {41+ test("#604 freeze inside class", () => {42+ class Thing {43+ [immerable] = true44+45+ constructor({x}) {46+ this._data = {x}47+ }48+49+ get x() {50+ return this._data.x51+ }52+53+ set x(x) {54+ this._data.x = x55+ }56+ }57+58+ let i = 159+ let item = new Thing({x: i})60+ let item0 = item61+62+ const bump = () => {63+ item = produce(item, draft => {64+ // uncomment this to make things work65+ //draft._data66+ draft.x = ++i67+ })68+ }69+70+ bump()71+ bump()72+73+ expect(i).toBe(3)74+ expect(item._data).toEqual({75+ x: 376+ })77+ expect(item0._data).toEqual({78+ x: 179+ })80+ })81+ })82+}modified:src/core/proxy.ts+22 added-3 removed
128 unmodified lines129 ownKeys(state) {130 return Reflect.ownKeys(latest(state))131 },132 set(state, prop: string /* strictly not, but helps TS */, value) {133+ const desc = getDescriptorFromProto(latest(state), prop)134+ if (desc?.set) {135+ // special case: if this write is captured by a setter, we have136+ // to trigger it with the correct context137+ desc.set.call(state.draft_, value)138+ return true139+ }140 state.assigned_[prop] = true141 if (!state.modified_) {142 if (is(value, peek(latest(state), prop)) && value !== undefined)143 return true70 unmodified lines214 return source[prop]215 }216 217 function readPropFromProto(state: ImmerState, source: any, prop: PropertyKey) {218+ const desc = getDescriptorFromProto(source, prop)219+ return desc220+ ? `value` in desc221+ ? desc.value222+ : // This is a very special case, if the prop is a getter defined by the223+ // prototype, we should invoke it with the draft as context!224+ desc.get?.call(state.draft_)225+ : undefined226+}227+228+function getDescriptorFromProto(229+ source: any,230+ prop: PropertyKey231+): PropertyDescriptor | undefined {232 // 'in' checks proto!233 if (!(prop in source)) return undefined234 let proto = Object.getPrototypeOf(source)235 while (proto) {236 const desc = Object.getOwnPropertyDescriptor(proto, prop)216- // This is a very special case, if the prop is a getter defined by the217- // prototype, we should invoke it with the draft as context!218- if (desc) return `value` in desc ? desc.value : desc.get?.call(state.draft_)237+ if (desc) return desc238 proto = Object.getPrototypeOf(proto)239 }240 return undefined241 }15 unmodified lines