Skip to content
Myr

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

2 files+104 added−3 removed

added:__tests__/regressions.js+82 added
1"use strict"2import {3 Immer,4 nothing,5 original,6 isDraft,7 immerable,8 enableAllPlugins9} from "../src/immer"1011enableAllPlugins()1213runBaseTest("proxy (no freeze)", true, false)14runBaseTest("proxy (autofreeze)", true, true)15runBaseTest("es5 (no freeze)", false, false)16runBaseTest("es5 (autofreeze)", false, true)1718function runBaseTest(name, useProxies, autoFreeze, useListener) {19 const listener = useListener ? function() {} : undefined20 const {produce, produceWithPatches} = createPatchedImmer({21 useProxies,22 autoFreeze23 })2425 // 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)2930 const {produce} = immer31 immer.produce = function(...args) {32 return typeof args[1] === "function" && args.length < 333 ? produce(...args, listener)34 : produce(...args)35 }3637 return immer38 }3940 describe(`regressions ${name}`, () => {41 test("#604 freeze inside class", () => {42 class Thing {43 [immerable] = true4445 constructor({x}) {46 this._data = {x}47 }4849 get x() {50 return this._data.x51 }5253 set x(x) {54 this._data.x = x55 }56 }5758 let i = 159 let item = new Thing({x: i})60 let item0 = item6162 const bump = () => {63 item = produce(item, draft => {64 // uncomment this to make things work65 //draft._data66 draft.x = ++i67 })68 }6970 bump()71 bump()7273 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 lines
129 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 true
70 unmodified lines
214 return source[prop]215}216217function 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}227228function 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