UNPKG

2.81 kBJavaScriptView Raw
1/* not type checking this file because flow doesn't play well with Proxy */
2
3import config from 'core/config'
4import { warn, makeMap, isNative } from '../util/index'
5
6let initProxy
7
8if (process.env.NODE_ENV !== 'production') {
9 const allowedGlobals = makeMap(
10 'Infinity,undefined,NaN,isFinite,isNaN,' +
11 'parseFloat,parseInt,decodeURI,decodeURIComponent,encodeURI,encodeURIComponent,' +
12 'Math,Number,Date,Array,Object,Boolean,String,RegExp,Map,Set,JSON,Intl,' +
13 'require' // for Webpack/Browserify
14 )
15
16 const warnNonPresent = (target, key) => {
17 warn(
18 `Property or method "${key}" is not defined on the instance but ` +
19 'referenced during render. Make sure that this property is reactive, ' +
20 'either in the data option, or for class-based components, by ' +
21 'initializing the property. ' +
22 'See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.',
23 target
24 )
25 }
26
27 const warnReservedPrefix = (target, key) => {
28 warn(
29 `Property "${key}" must be accessed with "$data.${key}" because ` +
30 'properties starting with "$" or "_" are not proxied in the Vue instance to ' +
31 'prevent conflicts with Vue internals' +
32 'See: https://vuejs.org/v2/api/#data',
33 target
34 )
35 }
36
37 const hasProxy =
38 typeof Proxy !== 'undefined' && isNative(Proxy)
39
40 if (hasProxy) {
41 const isBuiltInModifier = makeMap('stop,prevent,self,ctrl,shift,alt,meta,exact')
42 config.keyCodes = new Proxy(config.keyCodes, {
43 set (target, key, value) {
44 if (isBuiltInModifier(key)) {
45 warn(`Avoid overwriting built-in modifier in config.keyCodes: .${key}`)
46 return false
47 } else {
48 target[key] = value
49 return true
50 }
51 }
52 })
53 }
54
55 const hasHandler = {
56 has (target, key) {
57 const has = key in target
58 const isAllowed = allowedGlobals(key) ||
59 (typeof key === 'string' && key.charAt(0) === '_' && !(key in target.$data))
60 if (!has && !isAllowed) {
61 if (key in target.$data) warnReservedPrefix(target, key)
62 else warnNonPresent(target, key)
63 }
64 return has || !isAllowed
65 }
66 }
67
68 const getHandler = {
69 get (target, key) {
70 if (typeof key === 'string' && !(key in target)) {
71 if (key in target.$data) warnReservedPrefix(target, key)
72 else warnNonPresent(target, key)
73 }
74 return target[key]
75 }
76 }
77
78 initProxy = function initProxy (vm) {
79 if (hasProxy) {
80 // determine which proxy handler to use
81 const options = vm.$options
82 const handlers = options.render && options.render._withStripped
83 ? getHandler
84 : hasHandler
85 vm._renderProxy = new Proxy(vm, handlers)
86 } else {
87 vm._renderProxy = vm
88 }
89 }
90}
91
92export { initProxy }