Skip to content

Commit

Permalink
fix: the hasOwn should be used to determine whether an attribute exis…
Browse files Browse the repository at this point in the history
…ts. (#737)
  • Loading branch information
ygj6 committed Jul 2, 2021
1 parent 14d1c7b commit 65abcb4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/reactivity/reactive.ts
Expand Up @@ -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__
)
}

Expand Down
17 changes: 17 additions & 0 deletions test/v3/reactivity/reactive.spec.ts
Expand Up @@ -8,6 +8,7 @@ import {
shallowReactive,
set,
markRaw,
isRaw,
} from '../../../src'

describe('reactivity/reactive', () => {
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 65abcb4

Please sign in to comment.