Skip to content

Commit

Permalink
fix(reactivity): fix tracking when hasOwnProperty is called with non-…
Browse files Browse the repository at this point in the history
…string value

close vuejs#10455
close vuejs#10464
  • Loading branch information
yyx990803 authored and lynxlangya committed May 30, 2024
1 parent adae59d commit 349c639
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 15 additions & 0 deletions packages/reactivity/__tests__/reactiveArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,21 @@ describe('reactivity/reactive/Array', () => {
expect(fn).toHaveBeenCalledTimes(1)
})

test("should reactive when mutate array's index", () => {
const original = [1, 2, 3]
const observed = reactive(original)

let dummy
effect(() => {
dummy = observed.hasOwnProperty(0)
})

expect(dummy).toBe(true)

delete observed[0]
expect(dummy).toBe(false)
})

test('shift on Array should trigger dependency once', () => {
const arr = reactive([1, 2, 3])
const fn = vi.fn()
Expand Down
6 changes: 4 additions & 2 deletions packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ function createArrayInstrumentations() {
return instrumentations
}

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

class BaseReactiveHandler implements ProxyHandler<Target> {
Expand Down

0 comments on commit 349c639

Please sign in to comment.