Skip to content

Commit b2b9d1c

Browse files
committed
fix property reference proxy check for hand-written render functions
1 parent adf33c1 commit b2b9d1c

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

src/core/instance/proxy.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { warn, makeMap } from '../util/index'
44

5-
let hasProxy, proxyHandlers, initProxy
5+
let initProxy
66

77
if (process.env.NODE_ENV !== 'production') {
88
const allowedGlobals = makeMap(
@@ -21,20 +21,22 @@ if (process.env.NODE_ENV !== 'production') {
2121
)
2222
}
2323

24-
hasProxy =
24+
const hasProxy =
2525
typeof Proxy !== 'undefined' &&
2626
Proxy.toString().match(/native code/)
2727

28-
proxyHandlers = {
28+
const hasHandler = {
2929
has (target, key) {
3030
const has = key in target
3131
const isAllowed = allowedGlobals(key) || key.charAt(0) === '_'
3232
if (!has && !isAllowed) {
3333
warnNonPresent(target, key)
3434
}
3535
return has || !isAllowed
36-
},
36+
}
37+
}
3738

39+
const getHandler = {
3840
get (target, key) {
3941
if (typeof key === 'string' && !(key in target)) {
4042
warnNonPresent(target, key)
@@ -45,7 +47,16 @@ if (process.env.NODE_ENV !== 'production') {
4547

4648
initProxy = function initProxy (vm) {
4749
if (hasProxy) {
48-
vm._renderProxy = new Proxy(vm, proxyHandlers)
50+
// determine which proxy handler to use
51+
let handlers
52+
const options = vm.$options
53+
if (options.template || options.el) {
54+
handlers = hasHandler
55+
}
56+
if (options.render) {
57+
handlers = options.render._withStripped ? getHandler : hasHandler
58+
}
59+
vm._renderProxy = handlers ? new Proxy(vm, handlers) : vm
4960
} else {
5061
vm._renderProxy = vm
5162
}

test/unit/features/instance/render-proxy.spec.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,23 @@ if (typeof Proxy !== 'undefined') {
1010
})
1111

1212
it('should warn missing property in render fns without `with`', () => {
13+
const render = function (h) {
14+
return h('div', [this.a])
15+
}
16+
render._withStripped = true
17+
new Vue({
18+
render
19+
}).$mount()
20+
expect(`Property or method "a" is not defined`).toHaveBeenWarned()
21+
})
22+
23+
it('should not warn for hand-written render functions', () => {
1324
new Vue({
1425
render (h) {
1526
return h('div', [this.a])
1627
}
1728
}).$mount()
18-
expect(`Property or method "a" is not defined`).toHaveBeenWarned()
29+
expect(`Property or method "a" is not defined`).not.toHaveBeenWarned()
1930
})
2031
})
2132
}

0 commit comments

Comments
 (0)