Skip to content

Commit

Permalink
fix(reactivity): fix hasOwnProperty key coercion edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Apr 15, 2024
1 parent e5919d4 commit 969c5fb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
28 changes: 28 additions & 0 deletions packages/reactivity/__tests__/reactive.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,32 @@ describe('reactivity/reactive', () => {
const observed = reactive(original)
expect(isReactive(observed)).toBe(false)
})

test('hasOwnProperty edge case: Symbol values', () => {
const key = Symbol()
const obj = reactive({ [key]: 1 }) as { [key]?: 1 }
let dummy
effect(() => {
dummy = obj.hasOwnProperty(key)
})
expect(dummy).toBe(true)

delete obj[key]
expect(dummy).toBe(false)
})

test('hasOwnProperty edge case: non-string values', () => {
const key = {}
const obj = reactive({ '[object Object]': 1 }) as { '[object Object]'?: 1 }
let dummy
effect(() => {
// @ts-expect-error
dummy = obj.hasOwnProperty(key)
})
expect(dummy).toBe(true)

// @ts-expect-error
delete obj[key]
expect(dummy).toBe(false)
})
})
2 changes: 1 addition & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function createArrayInstrumentations() {

function hasOwnProperty(this: object, key: unknown) {
// #10455 hasOwnProperty may be called with non-string values
key = '' + key
if (!isSymbol(key)) key = String(key)
const obj = toRaw(this)
track(obj, TrackOpTypes.HAS, key)
return obj.hasOwnProperty(key as string)
Expand Down

0 comments on commit 969c5fb

Please sign in to comment.