@@ -156,7 +156,12 @@ export default createRule('no-unused-props', {
156
156
157
157
const paths : PropertyPath [ ] = [ ] ;
158
158
for ( const reference of variable . references ) {
159
- if ( 'identifier' in reference && reference . identifier . type === 'Identifier' ) {
159
+ if (
160
+ 'identifier' in reference &&
161
+ reference . identifier . type === 'Identifier' &&
162
+ ( reference . identifier . range [ 0 ] !== node . range [ 0 ] ||
163
+ reference . identifier . range [ 1 ] !== node . range [ 1 ] )
164
+ ) {
160
165
const referencePath = getPropertyPath ( reference . identifier ) ;
161
166
paths . push ( referencePath ) ;
162
167
}
@@ -265,11 +270,17 @@ export default createRule('no-unused-props', {
265
270
if ( reportedProps . has ( currentPathStr ) ) continue ;
266
271
267
272
const propType = typeChecker . getTypeOfSymbol ( prop ) ;
268
- const isUsedInPath = usedPaths . some ( ( path ) => {
269
- const usedPath = path . join ( '.' ) ;
270
- return usedPath === currentPathStr || usedPath . startsWith ( `${ currentPathStr } .` ) ;
273
+
274
+ const joinedUsedPaths = usedPaths . map ( ( path ) => path . join ( '.' ) ) ;
275
+ const isUsedThisInPath = joinedUsedPaths . includes ( currentPathStr ) ;
276
+ const isUsedInPath = joinedUsedPaths . some ( ( path ) => {
277
+ return path . startsWith ( `${ currentPathStr } .` ) ;
271
278
} ) ;
272
279
280
+ if ( isUsedThisInPath && ! isUsedInPath ) {
281
+ continue ;
282
+ }
283
+
273
284
const isUsedInProps = usedProps . has ( propName ) ;
274
285
275
286
if ( ! isUsedInPath && ! isUsedInProps ) {
@@ -282,10 +293,11 @@ export default createRule('no-unused-props', {
282
293
parent : parentPath . join ( '.' )
283
294
}
284
295
} ) ;
296
+ continue ;
285
297
}
286
298
287
- const isUsedNested = usedPaths . some ( ( path ) => {
288
- return path . join ( '.' ) . startsWith ( `${ currentPathStr } .` ) ;
299
+ const isUsedNested = joinedUsedPaths . some ( ( path ) => {
300
+ return path . startsWith ( `${ currentPathStr } .` ) ;
289
301
} ) ;
290
302
291
303
if ( isUsedNested || isUsedInProps ) {
@@ -324,6 +336,18 @@ export default createRule('no-unused-props', {
324
336
return usedProps . size === 0 ;
325
337
}
326
338
339
+ function normalizeUsedPaths ( paths : PropertyPath [ ] ) : PropertyPath [ ] {
340
+ const normalized : PropertyPath [ ] = [ ] ;
341
+ for ( const path of paths . sort ( ( a , b ) => a . length - b . length ) ) {
342
+ if ( path . length === 0 ) continue ;
343
+ if ( normalized . some ( ( p ) => p . every ( ( part , idx ) => part === path [ idx ] ) ) ) {
344
+ continue ;
345
+ }
346
+ normalized . push ( path ) ;
347
+ }
348
+ return normalized ;
349
+ }
350
+
327
351
return {
328
352
'VariableDeclaration > VariableDeclarator' : ( node : TSESTree . VariableDeclarator ) => {
329
353
// Only check $props declarations
@@ -359,7 +383,7 @@ export default createRule('no-unused-props', {
359
383
360
384
checkUnusedProperties (
361
385
propType ,
362
- usedPaths ,
386
+ normalizeUsedPaths ( usedPaths ) ,
363
387
usedProps ,
364
388
node . id ,
365
389
[ ] ,
0 commit comments