From eda083eba02b8a39bb40f6297569b58c0f7c32ec Mon Sep 17 00:00:00 2001 From: Julian Meinking <12785972+DrJume@users.noreply.github.com> Date: Sat, 18 Jun 2022 18:56:38 +0200 Subject: [PATCH 1/3] fix(runtime-core): `in` operator returning `false` for built-in instance properties in `exposeProxy` `exposeProxy` implements a fallback logic for accessing built-in instance properties but was missing the same logic for the `in` operator in the Proxy's `has()` handler. fix #6137 --- packages/runtime-core/__tests__/apiExpose.spec.ts | 2 ++ packages/runtime-core/src/component.ts | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/packages/runtime-core/__tests__/apiExpose.spec.ts b/packages/runtime-core/__tests__/apiExpose.spec.ts index 1235096b016..c717964e407 100644 --- a/packages/runtime-core/__tests__/apiExpose.spec.ts +++ b/packages/runtime-core/__tests__/apiExpose.spec.ts @@ -216,7 +216,9 @@ describe('api: expose', () => { } const root = nodeOps.createElement('div') render(h(Parent), root) + expect('$el' in childRef.value).toBe(true) expect(childRef.value.$el.tag).toBe('div') + expect('$parent' in grandChildRef.value).toBe(true) expect(grandChildRef.value.$parent).toBe(childRef.value) expect(grandChildRef.value.$parent.$parent).toBe(grandChildRef.value.$root) }) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index eededdf34d0..eefc537f7ef 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -942,6 +942,15 @@ export function getExposeProxy(instance: ComponentInternalInstance) { } else if (key in publicPropertiesMap) { return publicPropertiesMap[key](instance) } + }, + has(target, key: string) { + if (key in target) { + return true + } else if (key in publicPropertiesMap) { + return true + } + + return false } })) ) From c2a61762797f4ef69859215f2ad551e4deb7599a Mon Sep 17 00:00:00 2001 From: Julian Meinking <12785972+DrJume@users.noreply.github.com> Date: Sat, 18 Jun 2022 19:27:45 +0200 Subject: [PATCH 2/3] refactor(runtime-core): simplify logic in exposeProxy has() handler --- packages/runtime-core/src/component.ts | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/packages/runtime-core/src/component.ts b/packages/runtime-core/src/component.ts index eefc537f7ef..0361af4a69e 100644 --- a/packages/runtime-core/src/component.ts +++ b/packages/runtime-core/src/component.ts @@ -944,13 +944,7 @@ export function getExposeProxy(instance: ComponentInternalInstance) { } }, has(target, key: string) { - if (key in target) { - return true - } else if (key in publicPropertiesMap) { - return true - } - - return false + return key in target || key in publicPropertiesMap } })) ) From f65a6776aae7fea3a8aee5d022b8ae4191434e73 Mon Sep 17 00:00:00 2001 From: Julian Meinking <12785972+DrJume@users.noreply.github.com> Date: Mon, 4 Jul 2022 10:26:52 +0200 Subject: [PATCH 3/3] test: access custom exposed property not in publicPropertiesMap --- packages/runtime-core/__tests__/apiExpose.spec.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/__tests__/apiExpose.spec.ts b/packages/runtime-core/__tests__/apiExpose.spec.ts index c717964e407..1bfd513943b 100644 --- a/packages/runtime-core/__tests__/apiExpose.spec.ts +++ b/packages/runtime-core/__tests__/apiExpose.spec.ts @@ -203,7 +203,9 @@ describe('api: expose', () => { return h('div') }, setup(_, { expose }) { - expose() + expose({ + foo: 42 + }) return () => h(GrandChild, { ref: grandChildRef }) } }) @@ -218,6 +220,7 @@ describe('api: expose', () => { render(h(Parent), root) expect('$el' in childRef.value).toBe(true) expect(childRef.value.$el.tag).toBe('div') + expect('foo' in childRef.value).toBe(true) expect('$parent' in grandChildRef.value).toBe(true) expect(grandChildRef.value.$parent).toBe(childRef.value) expect(grandChildRef.value.$parent.$parent).toBe(grandChildRef.value.$root)