From aac0466cb8819fd132fbcc9c4d3e1014c14e2ad8 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 25 Nov 2021 18:15:06 +0800 Subject: [PATCH] fix(runtime-core): fix component public instance has check for accessed non-existent properties close #4962 --- .../runtime-core/__tests__/componentPublicInstance.spec.ts | 5 +++++ packages/runtime-core/src/componentPublicInstance.ts | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/componentPublicInstance.spec.ts b/packages/runtime-core/__tests__/componentPublicInstance.spec.ts index c4c456b5f7a..0274351f863 100644 --- a/packages/runtime-core/__tests__/componentPublicInstance.spec.ts +++ b/packages/runtime-core/__tests__/componentPublicInstance.spec.ts @@ -195,6 +195,11 @@ describe('component: proxy', () => { expect('$foobar' in instanceProxy).toBe(false) expect('baz' in instanceProxy).toBe(false) + // #4962 triggering getter should not cause non-existent property to + // pass the has check + instanceProxy.baz + expect('baz' in instanceProxy).toBe(false) + // set non-existent (goes into proxyTarget sink) instanceProxy.baz = 1 expect('baz' in instanceProxy).toBe(true) diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index dd385a815c9..8298aff42b1 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -248,11 +248,11 @@ if (__COMPAT__) { } const enum AccessTypes { + OTHER, SETUP, DATA, PROPS, - CONTEXT, - OTHER + CONTEXT } export interface ComponentRenderContext { @@ -437,7 +437,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { ) { let normalizedProps return ( - accessCache![key] !== undefined || + !!accessCache![key] || (data !== EMPTY_OBJ && hasOwn(data, key)) || (setupState !== EMPTY_OBJ && hasOwn(setupState, key)) || ((normalizedProps = propsOptions[0]) && hasOwn(normalizedProps, key)) ||