6
6
7
7
const utils = require ( '../utils' )
8
8
const assert = require ( 'assert' )
9
+ const getPropertyName = require ( 'eslint/lib/ast-utils' ) . getStaticPropertyName
9
10
10
11
const RESERVER_NAMES = new Set ( require ( '../utils/vue-reserved.json' ) )
11
12
const SCOPE_NAMES = new Set ( [ 'props' , 'computed' , 'data' , 'methods' ] )
@@ -31,50 +32,60 @@ function create (context) {
31
32
// Helpers
32
33
// ----------------------------------------------------------------------
33
34
34
- function checkUsedNames ( name , node ) {
35
+ function checkUsedNames ( name , node , groupName ) {
35
36
assert ( typeof name === 'string' )
36
37
37
- if ( reservedNames . has ( name ) ) {
38
+ if ( groupName === 'data' && name . substr ( 0 , 1 ) === '_' ) {
38
39
context . report ( {
39
40
node : node ,
40
- message : 'Reserved key \ '{{name}}\'.' ,
41
+ message : "Field '{{name}}' which start with _ in data is reserved." ,
41
42
data : {
42
- name : name
43
+ name
44
+ }
45
+ } )
46
+ } else if ( reservedNames . has ( name ) ) {
47
+ context . report ( {
48
+ node : node ,
49
+ message : "Reserved key '{{name}}'." ,
50
+ data : {
51
+ name
43
52
}
44
53
} )
45
54
} else if ( usedNames . indexOf ( name ) !== - 1 ) {
46
55
context . report ( {
47
56
node : node ,
48
- message : ' Duplicate key \ '{{name}}\'.' ,
57
+ message : " Duplicate key '{{name}}'." ,
49
58
data : {
50
- name : name
59
+ name
51
60
}
52
61
} )
53
62
}
54
63
usedNames . push ( name )
55
64
}
56
65
57
- function checkArrayExpression ( node ) {
66
+ function checkArrayExpression ( node , groupName ) {
58
67
node . elements . forEach ( item => {
59
- if ( item . type === 'Literal' ) {
60
- checkUsedNames ( item . value , item )
68
+ const name = utils . getStaticPropertyName ( item )
69
+ if ( name ) {
70
+ checkUsedNames ( name , item , groupName )
61
71
}
62
72
} )
63
73
}
64
74
65
- function checkObjectExpression ( node ) {
75
+ function checkObjectExpression ( node , groupName ) {
66
76
node . properties . forEach ( item => {
67
- if ( item . type === 'Property' && item . key . type === 'Identifier' ) {
68
- checkUsedNames ( item . key . name , item . key )
77
+ const name = utils . getStaticPropertyName ( item )
78
+ if ( name ) {
79
+ checkUsedNames ( name , item . key , groupName )
69
80
}
70
81
} )
71
82
}
72
83
73
- function checkFunctionExpression ( node ) {
84
+ function checkFunctionExpression ( node , groupName ) {
74
85
if ( node . body . type === 'BlockStatement' ) {
75
86
node . body . body . forEach ( item => {
76
87
if ( item . type === 'ReturnStatement' && item . argument . type === 'ObjectExpression' ) {
77
- checkObjectExpression ( item . argument )
88
+ checkObjectExpression ( item . argument , groupName )
78
89
}
79
90
} )
80
91
}
@@ -89,11 +100,11 @@ function create (context) {
89
100
. filter ( p => p . type === 'Property' && p . key . type === 'Identifier' && scopeNames . has ( p . key . name ) )
90
101
. forEach ( node => {
91
102
if ( node . value . type === 'ArrayExpression' ) {
92
- checkArrayExpression ( node . value )
103
+ checkArrayExpression ( node . value , node . key . name )
93
104
} else if ( node . value . type === 'ObjectExpression' ) {
94
- checkObjectExpression ( node . value )
105
+ checkObjectExpression ( node . value , node . key . name )
95
106
} else if ( node . value . type === 'FunctionExpression' ) {
96
- checkFunctionExpression ( node . value )
107
+ checkFunctionExpression ( node . value , node . key . name )
97
108
}
98
109
} )
99
110
} )
0 commit comments