@@ -26,9 +26,9 @@ module.exports = {
26
26
} ,
27
27
/** @param {RuleContext } context */
28
28
create ( context ) {
29
- /** @type {Map<ObjectExpression, Set<string>> } */
29
+ /** @type {Map<ObjectExpression|CallExpression , Set<string>> } */
30
30
const propsMap = new Map ( )
31
- /** @type { { type: 'export' | 'mark' | 'definition', object: ObjectExpression } | null } */
31
+ /** @type { { type: 'export' | 'mark' | 'definition', object: ObjectExpression } | { type: 'setup', object: CallExpression } | null } */
32
32
let vueObjectData = null
33
33
34
34
/**
@@ -123,7 +123,7 @@ module.exports = {
123
123
* @param {string[] } path
124
124
* @returns {Generator<{ node: Identifier, path: string[] }> }
125
125
*/
126
- function * iterateParamProperties ( param , path ) {
126
+ function * iteratePatternProperties ( param , path ) {
127
127
if ( ! param ) {
128
128
return
129
129
}
@@ -133,28 +133,94 @@ module.exports = {
133
133
path
134
134
}
135
135
} else if ( param . type === 'RestElement' ) {
136
- yield * iterateParamProperties ( param . argument , path )
136
+ yield * iteratePatternProperties ( param . argument , path )
137
137
} else if ( param . type === 'AssignmentPattern' ) {
138
- yield * iterateParamProperties ( param . left , path )
138
+ yield * iteratePatternProperties ( param . left , path )
139
139
} else if ( param . type === 'ObjectPattern' ) {
140
140
for ( const prop of param . properties ) {
141
141
if ( prop . type === 'Property' ) {
142
142
const name = getPropertyNameText ( prop )
143
- yield * iterateParamProperties ( prop . value , [ ...path , name ] )
143
+ yield * iteratePatternProperties ( prop . value , [ ...path , name ] )
144
144
} else if ( prop . type === 'RestElement' ) {
145
- yield * iterateParamProperties ( prop . argument , path )
145
+ yield * iteratePatternProperties ( prop . argument , path )
146
146
}
147
147
}
148
148
} else if ( param . type === 'ArrayPattern' ) {
149
149
for ( let index = 0 ; index < param . elements . length ; index ++ ) {
150
150
const element = param . elements [ index ]
151
- yield * iterateParamProperties ( element , [ ...path , `${ index } ` ] )
151
+ yield * iteratePatternProperties ( element , [ ...path , `${ index } ` ] )
152
152
}
153
153
}
154
154
}
155
155
156
- return Object . assign (
156
+ /**
157
+ * @param {Identifier } prop
158
+ * @param {string[] } path
159
+ */
160
+ function verifyPropVariable ( prop , path ) {
161
+ const variable = findVariable ( context . getScope ( ) , prop )
162
+ if ( ! variable ) {
163
+ return
164
+ }
165
+
166
+ for ( const reference of variable . references ) {
167
+ if ( ! reference . isRead ( ) ) {
168
+ continue
169
+ }
170
+ const id = reference . identifier
171
+
172
+ const invalid = utils . findMutating ( id )
173
+ if ( ! invalid ) {
174
+ continue
175
+ }
176
+ let name
177
+ if ( path . length === 0 ) {
178
+ if ( invalid . pathNodes . length === 0 ) {
179
+ continue
180
+ }
181
+ const mem = invalid . pathNodes [ 0 ]
182
+ name = getPropertyNameText ( mem )
183
+ } else {
184
+ if ( invalid . pathNodes . length === 0 && invalid . kind !== 'call' ) {
185
+ continue
186
+ }
187
+ name = path [ 0 ]
188
+ }
189
+
190
+ report ( invalid . node , name )
191
+ }
192
+ }
193
+
194
+ return utils . compositingVisitors (
157
195
{ } ,
196
+ utils . defineScriptSetupVisitor ( context , {
197
+ onDefinePropsEnter ( node , props ) {
198
+ const propsSet = new Set (
199
+ props . map ( ( p ) => p . propName ) . filter ( utils . isDef )
200
+ )
201
+ propsMap . set ( node , propsSet )
202
+ vueObjectData = {
203
+ type : 'setup' ,
204
+ object : node
205
+ }
206
+
207
+ if (
208
+ ! node . parent ||
209
+ node . parent . type !== 'VariableDeclarator' ||
210
+ node . parent . init !== node
211
+ ) {
212
+ return
213
+ }
214
+
215
+ for ( const { node : prop , path } of iteratePatternProperties (
216
+ node . parent . id ,
217
+ [ ]
218
+ ) ) {
219
+ verifyPropVariable ( prop , path )
220
+ propsSet . add ( prop . name )
221
+ }
222
+ }
223
+ } ) ,
158
224
utils . defineVueVisitor ( context , {
159
225
onVueObjectEnter ( node ) {
160
226
propsMap . set (
@@ -169,7 +235,9 @@ module.exports = {
169
235
} ,
170
236
onVueObjectExit ( node , { type } ) {
171
237
if (
172
- ( ! vueObjectData || vueObjectData . type !== 'export' ) &&
238
+ ( ! vueObjectData ||
239
+ ( vueObjectData . type !== 'export' &&
240
+ vueObjectData . type !== 'setup' ) ) &&
173
241
type !== 'instance'
174
242
) {
175
243
vueObjectData = {
@@ -191,41 +259,11 @@ module.exports = {
191
259
// cannot check
192
260
return
193
261
}
194
- for ( const { node : prop , path } of iterateParamProperties (
262
+ for ( const { node : prop , path } of iteratePatternProperties (
195
263
propsParam ,
196
264
[ ]
197
265
) ) {
198
- const variable = findVariable ( context . getScope ( ) , prop )
199
- if ( ! variable ) {
200
- continue
201
- }
202
-
203
- for ( const reference of variable . references ) {
204
- if ( ! reference . isRead ( ) ) {
205
- continue
206
- }
207
- const id = reference . identifier
208
-
209
- const invalid = utils . findMutating ( id )
210
- if ( ! invalid ) {
211
- continue
212
- }
213
- let name
214
- if ( path . length === 0 ) {
215
- if ( invalid . pathNodes . length === 0 ) {
216
- continue
217
- }
218
- const mem = invalid . pathNodes [ 0 ]
219
- name = getPropertyNameText ( mem )
220
- } else {
221
- if ( invalid . pathNodes . length === 0 && invalid . kind !== 'call' ) {
222
- continue
223
- }
224
- name = path [ 0 ]
225
- }
226
-
227
- report ( invalid . node , name )
228
- }
266
+ verifyPropVariable ( prop , path )
229
267
}
230
268
} ,
231
269
/** @param {(Identifier | ThisExpression) & { parent: MemberExpression } } node */
0 commit comments