From 026e7cdbe5a2c512f67b40afe3c08c519df642b1 Mon Sep 17 00:00:00 2001 From: Yang Mingshan Date: Wed, 14 Apr 2021 23:58:23 +0800 Subject: [PATCH 1/2] fix(watch): should not leak `this.proxy` to setup() --- .../runtime-core/__tests__/apiWatch.spec.ts | 21 ++++++++++++++++++- packages/runtime-core/src/apiWatch.ts | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 0c3719e7364..39088ebfed9 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -859,7 +859,7 @@ describe('api: watch', () => { expect(instance!.effects![0].active).toBe(false) }) - test('this.$watch should pass `this.proxy` to watch source as the first argument ', () => { + test('this.$watch should pass `this.proxy` to watch source as the first argument', () => { let instance: any const source = jest.fn() @@ -878,6 +878,25 @@ describe('api: watch', () => { expect(source).toHaveBeenCalledWith(instance) }) + test('should not leak `this.proxy` to setup()', () => { + const source = jest.fn() + const multipleSource = [jest.fn()] + + const Comp = defineComponent({ + render() {}, + setup() { + watch(source, () => {}) + watch(multipleSource, () => {}) + } + }) + + const root = nodeOps.createElement('div') + createApp(Comp).mount(root) + + expect(source).toBeCalledWith(undefined) + expect(multipleSource[0]).toBeCalledWith(undefined) + }) + // #2728 test('pre watcher callbacks should not track dependencies', async () => { const a = ref(0) diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 9498ced48fb..2f6dc8782a4 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -182,7 +182,7 @@ function doWatch( return traverse(s) } else if (isFunction(s)) { return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [ - instance && (instance.proxy as any) + arguments[3] && arguments[3].proxy ]) } else { __DEV__ && warnInvalidSource(s) @@ -193,7 +193,7 @@ function doWatch( // getter with cb getter = () => callWithErrorHandling(source, instance, ErrorCodes.WATCH_GETTER, [ - instance && (instance.proxy as any) + arguments[3] && arguments[3].proxy ]) } else { // no cb -> simple effect From 0567bda66b63e23b858dbd0e4f658c5c531aebeb Mon Sep 17 00:00:00 2001 From: Yang Mingshan Date: Thu, 15 Apr 2021 00:11:37 +0800 Subject: [PATCH 2/2] fix(watch): remove unnecessary arguments --- packages/runtime-core/__tests__/apiWatch.spec.ts | 3 --- packages/runtime-core/src/apiWatch.ts | 4 +--- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/packages/runtime-core/__tests__/apiWatch.spec.ts b/packages/runtime-core/__tests__/apiWatch.spec.ts index 39088ebfed9..f01b1338b7e 100644 --- a/packages/runtime-core/__tests__/apiWatch.spec.ts +++ b/packages/runtime-core/__tests__/apiWatch.spec.ts @@ -880,13 +880,11 @@ describe('api: watch', () => { test('should not leak `this.proxy` to setup()', () => { const source = jest.fn() - const multipleSource = [jest.fn()] const Comp = defineComponent({ render() {}, setup() { watch(source, () => {}) - watch(multipleSource, () => {}) } }) @@ -894,7 +892,6 @@ describe('api: watch', () => { createApp(Comp).mount(root) expect(source).toBeCalledWith(undefined) - expect(multipleSource[0]).toBeCalledWith(undefined) }) // #2728 diff --git a/packages/runtime-core/src/apiWatch.ts b/packages/runtime-core/src/apiWatch.ts index 2f6dc8782a4..0eb9db55e68 100644 --- a/packages/runtime-core/src/apiWatch.ts +++ b/packages/runtime-core/src/apiWatch.ts @@ -181,9 +181,7 @@ function doWatch( } else if (isReactive(s)) { return traverse(s) } else if (isFunction(s)) { - return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER, [ - arguments[3] && arguments[3].proxy - ]) + return callWithErrorHandling(s, instance, ErrorCodes.WATCH_GETTER) } else { __DEV__ && warnInvalidSource(s) }