From c3aa9e7458966dee16a488885537f3530b400e49 Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 19 Feb 2021 18:00:18 +0800 Subject: [PATCH 1/2] chore(runtime-core): extract function isReservedKey --- packages/runtime-core/src/componentOptions.ts | 5 +++-- packages/runtime-core/src/componentPublicInstance.ts | 10 ++++------ 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index c836f125493..907ca5ec510 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -54,7 +54,8 @@ import { EmitsOptions } from './componentEmits' import { Directive } from './directives' import { CreateComponentPublicInstance, - ComponentPublicInstance + ComponentPublicInstance, + isReservedKey } from './componentPublicInstance' import { warn } from './warning' import { VNodeChild } from './vnode' @@ -640,7 +641,7 @@ export function applyOptions( for (const key in rawData) { checkDuplicateProperties!(OptionTypes.DATA, key) // expose data on ctx during dev - if (key[0] !== '$' && key[0] !== '_') { + if (!isReservedKey(key)) { Object.defineProperty(ctx, key, { configurable: true, enumerable: true, diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index c493fea0b90..4cc5bd2cf84 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -247,6 +247,8 @@ export interface ComponentRenderContext { _: ComponentInternalInstance } +export const isReservedKey = (key: string) => key[0] === '_' || key[0] === '$' + export const PublicInstanceProxyHandlers: ProxyHandler = { get({ _: instance }: ComponentRenderContext, key: string) { const { @@ -345,11 +347,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { // to infinite warning loop key.indexOf('__v') !== 0) ) { - if ( - data !== EMPTY_OBJ && - (key[0] === '$' || key[0] === '_') && - hasOwn(data, key) - ) { + if (data !== EMPTY_OBJ && isReservedKey(key) && hasOwn(data, key)) { warn( `Property ${JSON.stringify( key @@ -524,7 +522,7 @@ export function exposeSetupStateOnRenderContext( ) { const { ctx, setupState } = instance Object.keys(toRaw(setupState)).forEach(key => { - if (key[0] === '$' || key[0] === '_') { + if (isReservedKey(key)) { warn( `setup() return property ${JSON.stringify( key From 940af055dfabc454589f5667628bf02bbcd1d06d Mon Sep 17 00:00:00 2001 From: daiwei Date: Fri, 19 Feb 2021 20:12:45 +0800 Subject: [PATCH 2/2] chore: improve code --- packages/runtime-core/src/componentOptions.ts | 4 ++-- packages/runtime-core/src/componentPublicInstance.ts | 9 +++++---- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/packages/runtime-core/src/componentOptions.ts b/packages/runtime-core/src/componentOptions.ts index 907ca5ec510..e2a0dbdd02b 100644 --- a/packages/runtime-core/src/componentOptions.ts +++ b/packages/runtime-core/src/componentOptions.ts @@ -55,7 +55,7 @@ import { Directive } from './directives' import { CreateComponentPublicInstance, ComponentPublicInstance, - isReservedKey + isReservedPrefix } from './componentPublicInstance' import { warn } from './warning' import { VNodeChild } from './vnode' @@ -641,7 +641,7 @@ export function applyOptions( for (const key in rawData) { checkDuplicateProperties!(OptionTypes.DATA, key) // expose data on ctx during dev - if (!isReservedKey(key)) { + if (!isReservedPrefix(key[0])) { Object.defineProperty(ctx, key, { configurable: true, enumerable: true, diff --git a/packages/runtime-core/src/componentPublicInstance.ts b/packages/runtime-core/src/componentPublicInstance.ts index 4cc5bd2cf84..11892abc71d 100644 --- a/packages/runtime-core/src/componentPublicInstance.ts +++ b/packages/runtime-core/src/componentPublicInstance.ts @@ -11,7 +11,8 @@ import { isGloballyWhitelisted, NOOP, extend, - isString + isString, + makeMap } from '@vue/shared' import { ReactiveEffect, @@ -247,7 +248,7 @@ export interface ComponentRenderContext { _: ComponentInternalInstance } -export const isReservedKey = (key: string) => key[0] === '_' || key[0] === '$' +export const isReservedPrefix = /*#__PURE__*/ makeMap('_,$') export const PublicInstanceProxyHandlers: ProxyHandler = { get({ _: instance }: ComponentRenderContext, key: string) { @@ -347,7 +348,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler = { // to infinite warning loop key.indexOf('__v') !== 0) ) { - if (data !== EMPTY_OBJ && isReservedKey(key) && hasOwn(data, key)) { + if (data !== EMPTY_OBJ && isReservedPrefix(key[0]) && hasOwn(data, key)) { warn( `Property ${JSON.stringify( key @@ -522,7 +523,7 @@ export function exposeSetupStateOnRenderContext( ) { const { ctx, setupState } = instance Object.keys(toRaw(setupState)).forEach(key => { - if (isReservedKey(key)) { + if (isReservedPrefix(key[0])) { warn( `setup() return property ${JSON.stringify( key