From 65abcb4b37d72e4f58c3fe5893ea8b2b411c4b76 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Fri, 2 Jul 2021 15:55:07 +0800 Subject: [PATCH] fix: the hasOwn should be used to determine whether an attribute exists. (#737) --- src/reactivity/reactive.ts | 10 ++++++++-- test/v3/reactivity/reactive.spec.ts | 17 +++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/reactivity/reactive.ts b/src/reactivity/reactive.ts index 827223f6..4691011e 100644 --- a/src/reactivity/reactive.ts +++ b/src/reactivity/reactive.ts @@ -16,13 +16,19 @@ import { rawSet, accessModifiedSet } from '../utils/sets' export function isRaw(obj: any): boolean { return Boolean( - obj?.__ob__ && typeof obj.__ob__ === 'object' && obj.__ob__?.__raw__ + obj && + hasOwn(obj, '__ob__') && + typeof obj.__ob__ === 'object' && + obj.__ob__?.__raw__ ) } export function isReactive(obj: any): boolean { return Boolean( - obj?.__ob__ && typeof obj.__ob__ === 'object' && !obj.__ob__?.__raw__ + obj && + hasOwn(obj, '__ob__') && + typeof obj.__ob__ === 'object' && + !obj.__ob__?.__raw__ ) } diff --git a/test/v3/reactivity/reactive.spec.ts b/test/v3/reactivity/reactive.spec.ts index 4ae2e7de..7719a1e9 100644 --- a/test/v3/reactivity/reactive.spec.ts +++ b/test/v3/reactivity/reactive.spec.ts @@ -8,6 +8,7 @@ import { shallowReactive, set, markRaw, + isRaw, } from '../../../src' describe('reactivity/reactive', () => { @@ -35,6 +36,22 @@ describe('reactivity/reactive', () => { expect(Object.keys(observed)).toEqual(['foo']) }) + //#693 + test('the hasOwn should be used to determine whether an attribute exists.', () => { + const obj = {} + expect(isReactive(obj)).toBe(false) + expect(isRaw(obj)).toBe(false) + const mockObj = new Proxy(obj, { + get: (target, key) => { + if (!(key in Object.keys(target))) { + throw new Error(`the ${key.toString()} is not found in the target.`) + } + }, + }) + expect(isReactive(mockObj)).toBe(false) + expect(isRaw(obj)).toBe(false) + }) + test('proto', () => { const obj = {} const reactiveObj = reactive(obj)