]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(runtime-core): warn against user properties with reserved prefixes
authorEvan You <yyx990803@gmail.com>
Fri, 1 May 2020 15:23:01 +0000 (11:23 -0400)
committerEvan You <yyx990803@gmail.com>
Fri, 1 May 2020 15:23:22 +0000 (11:23 -0400)
packages/runtime-core/src/componentOptions.ts
packages/runtime-core/src/componentProxy.ts

index 5a773f080fc32a984d4074a111c8d2262101518f..90712944b849292817452fe1ea9d61643b75f5d9 100644 (file)
@@ -417,12 +417,14 @@ export function applyOptions(
       for (const key in rawData) {
         checkDuplicateProperties!(OptionTypes.DATA, key)
         // expose data on ctx during dev
-        Object.defineProperty(ctx, key, {
-          configurable: true,
-          enumerable: true,
-          get: () => rawData[key],
-          set: NOOP
-        })
+        if (key[0] !== '$' && key[0] !== '_') {
+          Object.defineProperty(ctx, key, {
+            configurable: true,
+            enumerable: true,
+            get: () => rawData[key],
+            set: NOOP
+          })
+        }
       }
     }
   }
index bee09ee87ffc31d7deb254b33348595fc61d61ed..e5fe0bdeea1a56636d9e735eb0fda20edff92f0d 100644 (file)
@@ -195,10 +195,19 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
     ) {
       return globalProperties[key]
     } else if (__DEV__ && currentRenderingInstance) {
-      warn(
-        `Property ${JSON.stringify(key)} was accessed during render ` +
-          `but is not defined on instance.`
-      )
+      if (data !== EMPTY_OBJ && key[0] === '$' && hasOwn(data, key)) {
+        warn(
+          `Property ${JSON.stringify(
+            key
+          )} must be accessed via $data because it starts with a reserved ` +
+            `character and is not proxied on the render context.`
+        )
+      } else {
+        warn(
+          `Property ${JSON.stringify(key)} was accessed during render ` +
+            `but is not defined on instance.`
+        )
+      }
     }
   },
 
@@ -280,7 +289,15 @@ export const RuntimeCompiledPublicInstanceProxyHandlers = {
     return PublicInstanceProxyHandlers.get!(target, key, target)
   },
   has(_: ComponentRenderContext, key: string) {
-    return key[0] !== '_' && !isGloballyWhitelisted(key)
+    const has = key[0] !== '_' && !isGloballyWhitelisted(key)
+    if (__DEV__ && !has && PublicInstanceProxyHandlers.has!(_, key)) {
+      warn(
+        `Property ${JSON.stringify(
+          key
+        )} should not start with _ which is a reserved prefix for Vue internals.`
+      )
+    }
+    return has
   }
 }