From 10bb34bb869a47c37d945f8c80abf723fac9fc1a Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 11 Jun 2020 15:12:57 -0400 Subject: [PATCH] fix(reactivity): fix toRaw for objects prototype inherting reactive fix #1246 --- packages/reactivity/__tests__/reactive.spec.ts | 10 +++++++++- packages/reactivity/src/baseHandlers.ts | 8 +++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/reactivity/__tests__/reactive.spec.ts b/packages/reactivity/__tests__/reactive.spec.ts index 46ce75b6acb..800171ef3bf 100644 --- a/packages/reactivity/__tests__/reactive.spec.ts +++ b/packages/reactivity/__tests__/reactive.spec.ts @@ -92,13 +92,21 @@ describe('reactivity/reactive', () => { expect(original.bar).toBe(original2) }) - test('unwrap', () => { + test('toRaw', () => { const original = { foo: 1 } const observed = reactive(original) expect(toRaw(observed)).toBe(original) expect(toRaw(original)).toBe(original) }) + test('toRaw on object using reactive as prototype', () => { + const original = reactive({}) + const obj = Object.create(original) + const raw = toRaw(obj) + expect(raw).toBe(obj) + expect(raw).not.toBe(toRaw(original)) + }) + test('should not unwrap Ref', () => { const observedNumberRef = reactive(ref(1)) const observedObjectRef = reactive(ref({ foo: 1 })) diff --git a/packages/reactivity/src/baseHandlers.ts b/packages/reactivity/src/baseHandlers.ts index 4a3f2d327a6..f8452a35d85 100644 --- a/packages/reactivity/src/baseHandlers.ts +++ b/packages/reactivity/src/baseHandlers.ts @@ -46,7 +46,13 @@ function createGetter(isReadonly = false, shallow = false) { return !isReadonly } else if (key === ReactiveFlags.isReadonly) { return isReadonly - } else if (key === ReactiveFlags.raw) { + } else if ( + key === ReactiveFlags.raw && + receiver === + (isReadonly + ? (target as any).__v_readonly + : (target as any).__v_reactive) + ) { return target }