Skip to content

Commit 1526f94

Browse files
committed
fix(watch): should not leak this context to setup watch getters
ref #3603
1 parent 9e3708c commit 1526f94

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,22 @@ describe('api: watch', () => {
878878
expect(source).toHaveBeenCalledWith(instance)
879879
})
880880

881+
test('should not leak `this.proxy` to setup()', () => {
882+
const source = jest.fn()
883+
884+
const Comp = defineComponent({
885+
render() {},
886+
setup() {
887+
watch(source, () => {})
888+
}
889+
})
890+
891+
const root = nodeOps.createElement('div')
892+
createApp(Comp).mount(root)
893+
// should not have any arguments
894+
expect(source.mock.calls[0]).toMatchObject([])
895+
})
896+
881897
// #2728
882898
test('pre watcher callbacks should not track dependencies', async () => {
883899
const a = ref(0)
@@ -944,7 +960,7 @@ describe('api: watch', () => {
944960
await nextTick()
945961
expect(spy).toHaveBeenCalledTimes(2)
946962
})
947-
963+
948964
it('watching sources: ref<any[]>', async () => {
949965
const foo = ref([1])
950966
const spy = jest.fn()

packages/runtime-core/src/apiWatch.ts

+3-7
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,7 @@ function doWatch(
189189
} else if (isReactive(s)) {
190190
return traverse(s)
191191
} else if (isFunction(s)) {
192-
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [
193-
instance && (instance.proxy as any)
194-
])
192+
return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
195193
} else {
196194
__DEV__ && warnInvalidSource(s)
197195
}
@@ -200,9 +198,7 @@ function doWatch(
200198
if (cb) {
201199
// getter with cb
202200
getter = () =>
203-
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [
204-
instance && (instance.proxy as any)
205-
])
201+
callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER)
206202
} else {
207203
// no cb -> simple effect
208204
getter = () => {
@@ -371,7 +367,7 @@ export function instanceWatch(
371367
? source.includes('.')
372368
? createPathGetter(publicThis, source)
373369
: () => publicThis[source]
374-
: source.bind(publicThis)
370+
: source.bind(publicThis, publicThis)
375371
let cb
376372
if (isFunction(value)) {
377373
cb = value

0 commit comments

Comments
 (0)