File tree Expand file tree Collapse file tree 2 files changed +51
-2
lines changed
test/unit/features/instance Expand file tree Collapse file tree 2 files changed +51
-2
lines changed Original file line number Diff line number Diff line change @@ -24,6 +24,16 @@ if (process.env.NODE_ENV !== 'production') {
24
24
)
25
25
}
26
26
27
+ const warnStartingWithDollar = ( 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
+
27
37
const hasProxy =
28
38
typeof Proxy !== 'undefined' && isNative ( Proxy )
29
39
@@ -47,7 +57,8 @@ if (process.env.NODE_ENV !== 'production') {
47
57
const has = key in target
48
58
const isAllowed = allowedGlobals ( key ) || ( typeof key === 'string' && key . charAt ( 0 ) === '_' )
49
59
if ( ! has && ! isAllowed ) {
50
- warnNonPresent ( target , key )
60
+ if ( key in target . $data ) warnStartingWithDollar ( target , key )
61
+ else warnNonPresent ( target , key )
51
62
}
52
63
return has || ! isAllowed
53
64
}
@@ -56,7 +67,8 @@ if (process.env.NODE_ENV !== 'production') {
56
67
const getHandler = {
57
68
get ( target , key ) {
58
69
if ( typeof key === 'string' && ! ( key in target ) ) {
59
- warnNonPresent ( target , key )
70
+ if ( key in target . $data ) warnStartingWithDollar ( target , key )
71
+ else warnNonPresent ( target , key )
60
72
}
61
73
return target [ key ]
62
74
}
Original file line number Diff line number Diff line change @@ -45,5 +45,42 @@ if (typeof Proxy !== 'undefined') {
45
45
46
46
expect ( vm . $el . textContent ) . toBe ( 'foo' )
47
47
} )
48
+
49
+ it ( 'should warn properties starting with $ when found' , ( ) => {
50
+ new Vue ( {
51
+ data : { $a : 'foo' } ,
52
+ template : `<div>{{ $a }}</div>`
53
+ } ) . $mount ( )
54
+ expect ( `Property "$a" must be accessed with "$data.$a"` ) . toHaveBeenWarned ( )
55
+ } )
56
+
57
+ it ( 'should warn properties starting with $ when not found' , ( ) => {
58
+ new Vue ( {
59
+ template : `<div>{{ $a }}</div>`
60
+ } ) . $mount ( )
61
+ expect ( `Property or method "$a" is not defined` ) . toHaveBeenWarned ( )
62
+ expect ( `Property "$a" must be accessed with "$data.$a"` ) . not . toHaveBeenWarned ( )
63
+ } )
64
+
65
+ it ( 'should warn properties starting with $ when not found (with stripped)' , ( ) => {
66
+ const render = function ( h ) {
67
+ return h ( 'p' , this . $a )
68
+ }
69
+ render . _withStripped = true
70
+ new Vue ( {
71
+ data : { $a : 'foo' } ,
72
+ render
73
+ } ) . $mount ( )
74
+ expect ( `Property "$a" must be accessed with "$data.$a"` ) . toHaveBeenWarned ( )
75
+ } )
76
+
77
+ it ( 'should not warn properties starting with $ when using $data to access' , ( ) => {
78
+ new Vue ( {
79
+ data : { $a : 'foo' } ,
80
+ template : `<div>{{ $data.$a }}</div>`
81
+ } ) . $mount ( )
82
+ expect ( `Property or method "$a" is not defined` ) . not . toHaveBeenWarned ( )
83
+ expect ( `Property or method "$a" is not defined` ) . not . toHaveBeenWarned ( )
84
+ } )
48
85
} )
49
86
}
You can’t perform that action at this time.
0 commit comments