@@ -156,7 +156,12 @@ export default createRule('no-unused-props', {
156156
157157 const paths : PropertyPath [ ] = [ ] ;
158158 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+ ) {
160165 const referencePath = getPropertyPath ( reference . identifier ) ;
161166 paths . push ( referencePath ) ;
162167 }
@@ -265,11 +270,17 @@ export default createRule('no-unused-props', {
265270 if ( reportedProps . has ( currentPathStr ) ) continue ;
266271
267272 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 } .` ) ;
271278 } ) ;
272279
280+ if ( isUsedThisInPath && ! isUsedInPath ) {
281+ continue ;
282+ }
283+
273284 const isUsedInProps = usedProps . has ( propName ) ;
274285
275286 if ( ! isUsedInPath && ! isUsedInProps ) {
@@ -282,10 +293,11 @@ export default createRule('no-unused-props', {
282293 parent : parentPath . join ( '.' )
283294 }
284295 } ) ;
296+ continue ;
285297 }
286298
287- const isUsedNested = usedPaths . some ( ( path ) => {
288- return path . join ( '.' ) . startsWith ( `${ currentPathStr } .` ) ;
299+ const isUsedNested = joinedUsedPaths . some ( ( path ) => {
300+ return path . startsWith ( `${ currentPathStr } .` ) ;
289301 } ) ;
290302
291303 if ( isUsedNested || isUsedInProps ) {
@@ -324,6 +336,18 @@ export default createRule('no-unused-props', {
324336 return usedProps . size === 0 ;
325337 }
326338
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+
327351 return {
328352 'VariableDeclaration > VariableDeclarator' : ( node : TSESTree . VariableDeclarator ) => {
329353 // Only check $props declarations
@@ -359,7 +383,7 @@ export default createRule('no-unused-props', {
359383
360384 checkUnusedProperties (
361385 propType ,
362- usedPaths ,
386+ normalizeUsedPaths ( usedPaths ) ,
363387 usedProps ,
364388 node . id ,
365389 [ ] ,
0 commit comments