Explorar o código

fix proxy missing property detection when render function does not use `with`

Evan You %!s(int64=9) %!d(string=hai) anos
pai
achega
fa61c0042c
Modificáronse 2 ficheiros con 38 adicións e 6 borrados
  1. 17 6
      src/core/instance/proxy.js
  2. 21 0
      test/unit/features/instance/render-proxy.spec.js

+ 17 - 6
src/core/instance/proxy.js

@@ -12,6 +12,15 @@ if (process.env.NODE_ENV !== 'production') {
     'require' // for Webpack/Browserify
   )
 
+  const warnNonPresent = (target, key) => {
+    warn(
+      `Property or method "${key}" is not defined on the instance but ` +
+      `referenced during render. Make sure to declare reactive data ` +
+      `properties in the data option.`,
+      target
+    )
+  }
+
   hasProxy =
     typeof Proxy !== 'undefined' &&
     Proxy.toString().match(/native code/)
@@ -21,14 +30,16 @@ if (process.env.NODE_ENV !== 'production') {
       const has = key in target
       const isAllowed = allowedGlobals(key) || key.charAt(0) === '_'
       if (!has && !isAllowed) {
-        warn(
-          `Property or method "${key}" is not defined on the instance but ` +
-          `referenced during render. Make sure to declare reactive data ` +
-          `properties in the data option.`,
-          target
-        )
+        warnNonPresent(target, key)
       }
       return has || !isAllowed
+    },
+
+    get (target, key) {
+      if (typeof key === 'string' && !(key in target)) {
+        warnNonPresent(target, key)
+      }
+      return target[key]
     }
   }
 

+ 21 - 0
test/unit/features/instance/render-proxy.spec.js

@@ -0,0 +1,21 @@
+import Vue from 'vue'
+
+if (typeof Proxy !== 'undefined') {
+  describe('render proxy', () => {
+    it('should warn missing property in render fns with `with`', () => {
+      new Vue({
+        template: `<div>{{ a }}</div>`
+      }).$mount()
+      expect(`Property or method "a" is not defined`).toHaveBeenWarned()
+    })
+
+    it('should warn missing property in render fns without `with`', () => {
+      new Vue({
+        render (h) {
+          return h('div', [this.a])
+        }
+      }).$mount()
+      expect(`Property or method "a" is not defined`).toHaveBeenWarned()
+    })
+  })
+}