]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
refactor(runtime-core): extract function `isReservedPrefix` (#3265)
authoredison <daiwei521@126.com>
Fri, 13 May 2022 08:52:19 +0000 (16:52 +0800)
committerGitHub <noreply@github.com>
Fri, 13 May 2022 08:52:19 +0000 (04:52 -0400)
* chore(runtime-core): extract function isReservedKey

* chore: improve code

Co-authored-by: Evan You <yyx990803@gmail.com>
packages/runtime-core/src/componentOptions.ts
packages/runtime-core/src/componentPublicInstance.ts

index ed95174b187edd20351e997e0748594dcec3adaa..c482d0519656b9dbe73e401ade8171046ac4226c 100644 (file)
@@ -58,7 +58,8 @@ import { EmitsOptions, EmitsToProps } from './componentEmits'
 import { Directive } from './directives'
 import {
   CreateComponentPublicInstance,
-  ComponentPublicInstance
+  ComponentPublicInstance,
+  isReservedPrefix
 } from './componentPublicInstance'
 import { warn } from './warning'
 import { VNodeChild } from './vnode'
@@ -681,7 +682,7 @@ export function applyOptions(instance: ComponentInternalInstance) {
         for (const key in data) {
           checkDuplicateProperties!(OptionTypes.DATA, key)
           // expose data on ctx during dev
-          if (key[0] !== '$' && key[0] !== '_') {
+          if (!isReservedPrefix(key[0])) {
             Object.defineProperty(ctx, key, {
               configurable: true,
               enumerable: true,
index 33e8ce5fac0d31220d3274e8e353b5c5b70d553c..986a2e79b5586504794d4601cb93962d9a8406bc 100644 (file)
@@ -274,6 +274,8 @@ export interface ComponentRenderContext {
   _: ComponentInternalInstance
 }
 
+export const isReservedPrefix = (key: string) => key === '_' || key === '$'
+
 export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
   get({ _: instance }: ComponentRenderContext, key: string) {
     const { ctx, setupState, data, props, accessCache, type, appContext } =
@@ -385,11 +387,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
         // to infinite warning loop
         key.indexOf('__v') !== 0)
     ) {
-      if (
-        data !== EMPTY_OBJ &&
-        (key[0] === '$' || key[0] === '_') &&
-        hasOwn(data, key)
-      ) {
+      if (data !== EMPTY_OBJ && isReservedPrefix(key[0]) && hasOwn(data, key)) {
         warn(
           `Property ${JSON.stringify(
             key
@@ -571,7 +569,7 @@ export function exposeSetupStateOnRenderContext(
   const { ctx, setupState } = instance
   Object.keys(toRaw(setupState)).forEach(key => {
     if (!setupState.__isScriptSetup) {
-      if (key[0] === '$' || key[0] === '_') {
+      if (isReservedPrefix(key[0])) {
         warn(
           `setup() return property ${JSON.stringify(
             key