Merge branch 'dalcib-getters' into immer-7
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 · parents b896a3a, f33d6b6 · Browse files · 4 files·+130 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+130 added−13 removed
- tests:+116 added−9 removed
- other:+14 added−4 removed
Provenance
- Commit
58412b5- Tree
45f6163- Author
Michel Weststrate <mweststrate@fb.com>- Committer
Michel Weststrate <mweststrate@fb.com>- Parent 1
b896a3a- Parent 2
f33d6b6
4 files changed+130 added−13 removed
modified:__tests__/base.js+116 added-9 removed
19 unmodified lines20 test("immer should have no dependencies", () => {21 expect(require("../package.json").dependencies).toBeUndefined()22 })23 24-runBaseTest("proxy (no freeze)", true, false)25-runBaseTest("proxy (autofreeze)", true, true)26-runBaseTest("proxy (patch listener)", true, false, true)27-runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)24+// runBaseTest("proxy (no freeze)", true, false)25+// runBaseTest("proxy (autofreeze)", true, true)26+// runBaseTest("proxy (patch listener)", true, false, true)27+// runBaseTest("proxy (autofreeze)(patch listener)", true, true, true)28 29 runBaseTest("es5 (no freeze)", false, false)30-runBaseTest("es5 (autofreeze)", false, true)31-runBaseTest("es5 (patch listener)", false, false, true)32-runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)30+// runBaseTest("es5 (autofreeze)", false, true)31+// runBaseTest("es5 (patch listener)", false, false, true)32+// runBaseTest("es5 (autofreeze)(patch listener)", false, true, true)33 34 function runBaseTest(name, useProxies, autoFreeze, useListener) {35 const listener = useListener ? function() {} : undefined36 const {produce, produceWithPatches} = createPatchedImmer({896 unmodified lines933 expect(nextState.foo).toBeTruthy()934 expect(isEnumerable(nextState, "foo")).toBeFalsy()935 })936 937- it("throws on computed properties", () => {937+ it("does not throws on computed properties", () => {938 const baseState = {}939 Object.defineProperty(baseState, "foo", {940 get: () => {},941 enumerable: true4 unmodified lines946 if (useProxies) {947 s.modified = true948 }949 })950- }).toThrowErrorMatchingSnapshot()950+ }).not.toThrow()951 })952 953+ it("can work with computed props", () => {954+ const baseState = {955+ x: 1,956+ set y(v) {957+ this.x = v958+ },959+ get y() {960+ return this.x961+ }962+ }963+964+ debugger965+ const nextState = produce(baseState, d => {966+ expect(d.y).toBe(1)967+ d.y = 2968+ expect(d.x).toBe(2)969+ expect(d.y).toBe(2)970+ })971+ expect(baseState.x).toBe(1)972+ expect(baseState.y).toBe(1)973+974+ expect(nextState.x).toBe(2)975+ expect(nextState.y).toBe(2)976+ })977+978+ it("can work with class with computed props", () => {979+ class State {980+ [immerable] = true981+982+ x = 1983+984+ set y(v) {985+ this.x = v986+ }987+988+ get y() {989+ return this.x990+ }991+ }992+993+ const baseState = new State()994+995+ const nextState = produce(baseState, d => {996+ expect(d.y).toBe(1)997+ d.y = 2998+ expect(d.x).toBe(2)999+ expect(d.y).toBe(2)1000+ expect(Object.hasOwnProperty(d, "y")).toBeNull()1001+ })1002+ expect(baseState.x).toBe(1)1003+ expect(baseState.y).toBe(1)1004+1005+ expect(nextState.x).toBe(2)1006+ expect(nextState.y).toBe(2)1007+ expect(Object.hasOwnProperty(nextState, "y")).toBeNull()1008+ })1009+1010 it("allows inherited computed properties", () => {1011 const proto = {}1012 Object.defineProperty(proto, "foo", {1013 get() {1126 unmodified lines2140 expect(result[0]).toBe(random)2141 })2142 })2143 }2144+2145+ describe("class with getters", () => {2146+ class State {2147+ [immerable] = true2148+ _bar = {baz: 1}2149+ foo2150+ get bar() {2151+ return this._bar2152+ }2153+ syncFoo() {2154+ this.foo = this.bar.baz2155+ this.bar.baz++2156+ }2157+ }2158+ const state = new State()2159+2160+ it("should use a method to assing a field using a getter that return a non primitive object", () => {2161+ const newState = produce(state, draft => {2162+ draft.syncFoo()2163+ })2164+ expect(newState.foo).toEqual(1)2165+ expect(newState.bar).toEqual({baz: 2})2166+ expect(state.bar).toEqual({baz: 1})2167+ })2168+ })2169+2170+ describe("class with setters", () => {2171+ class State {2172+ [immerable] = true2173+ _bar = 02174+ get bar() {2175+ return this._bar2176+ }2177+ set bar(x) {2178+ this._bar = x2179+ }2180+ }2181+ const state = new State()2182+2183+ it("should define a field with a setter", () => {2184+ const newState3 = produce(state, d => {2185+ d.bar = 12186+ expect(d._bar).toEqual(1)2187+ })2188+ expect(newState3._bar).toEqual(1)2189+ expect(newState3.bar).toEqual(1)2190+ expect(state._bar).toEqual(0)2191+ expect(state.bar).toEqual(0)2192+ })2193+ })2194 }2195 2196 function testLiteralTypes(produce) {2197 class Foo {}52 unmodified linesmodified:src/core/immerClass.ts-2 removed
3 unmodified lines4 ImmerState,5 Drafted,6 isDraftable,7 processResult,8- NOTHING,8 Patch,9 Objectish,10 DRAFT_STATE,11 Draft,2 unmodified lines14 isMap,15 isSet,16 markChangedProxy,17 createProxyProxy,19- freeze,18 getPlugin,19 die,20 hasProxies,21 isMinified,207 unmodified linesmodified:src/core/proxy.ts+10 added-1 removed
17 unmodified lines18 createProxy,19 ProxyTypeProxyObject,20 ProxyTypeProxyArray21 } from "../internal"22+import {isPlainObject} from "../utils/common"23 24 interface ProxyBaseState extends ImmerBaseState {25 assigned_: {26 [property: string]: boolean87 unmodified lines114 if (!state.modified_ && has(drafts, prop)) {115 return drafts![prop as any]116 }117 117- const value = latest(state)[prop]118+ const l = latest(state)119+ const desc = Object.getOwnPropertyDescriptor(120+ isPlainObject(l) ? l : Object.getPrototypeOf(l),121+ prop122+ )123+ // If the prop is a getter, don't proxy the full prop, but invoke the getter with the correct scope. Fixes #558124+ if (desc?.get) return desc.get.call(state.draft_)125+126+ const value = l[prop]127 if (state.finalized_ || !isDraftable(value)) {128 return value129 }130 137 unmodified linesmodified:src/utils/common.ts+4 added-1 removed
157 unmodified linesExpand all15815916016116216216316416516616716816929 unmodified lines157 unmodified linesExpand all } const desc = Object.getOwnPropertyDescriptor(base, key)! let {value} = desc if (desc.get) { if (!invokeGetters) die(1) if (!invokeGetters) { Object.defineProperty(clone, key, desc) return } value = desc.get.call(base) } if (desc.enumerable) { clone[key] = value29 unmodified lines