]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(watch): should not leak this context to setup watch getters
authorEvan You <yyx990803@gmail.com>
Tue, 25 May 2021 15:10:11 +0000 (11:10 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 25 May 2021 15:10:11 +0000 (11:10 -0400)
ref #3603

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

index aed7d65b6a55beb23c9164e63268a0df1faaad98..28f06057bee6e96aee89784d529d82375f2a15e9 100644 (file)
@@ -878,6 +878,22 @@ describe('api: watch', () => {
     expect(source).toHaveBeenCalledWith(instance)
   })
 
+  test('should not leak `this.proxy` to setup()', () => {
+    const source = jest.fn()
+
+    const Comp = defineComponent({
+      render() {},
+      setup() {
+        watch(source, () => {})
+      }
+    })
+
+    const root = nodeOps.createElement('div')
+    createApp(Comp).mount(root)
+    // should not have any arguments
+    expect(source.mock.calls[0]).toMatchObject([])
+  })
+
   // #2728
   test('pre watcher callbacks should not track dependencies', async () => {
     const a = ref(0)
@@ -944,7 +960,7 @@ describe('api: watch', () => {
     await nextTick()
     expect(spy).toHaveBeenCalledTimes(2)
   })
-  
+
   it('watching sources: ref<any[]>', async () => {
     const foo = ref([1])
     const spy = jest.fn()
index 41ded87847a0b7f0b85cec378f622b50e3ec3b77..0e33193b20a98a24302a79426460e33282fcc37c 100644 (file)
@@ -189,9 +189,7 @@ function doWatch(
         } else if (isReactive(s)) {
           return traverse(s)
         } else if (isFunction(s)) {
-          return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [
-            instance && (instance.proxy as any)
-          ])
+          return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER)
         } else {
           __DEV__ && warnInvalidSource(s)
         }
@@ -200,9 +198,7 @@ function doWatch(
     if (cb) {
       // getter with cb
       getter = () =>
-        callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [
-          instance && (instance.proxy as any)
-        ])
+        callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER)
     } else {
       // no cb -> simple effect
       getter = () => {
@@ -371,7 +367,7 @@ export function instanceWatch(
     ? source.includes('.')
       ? createPathGetter(publicThis, source)
       : () => publicThis[source]
-    : source.bind(publicThis)
+    : source.bind(publicThis, publicThis)
   let cb
   if (isFunction(value)) {
     cb = value