@@ -550,6 +550,34 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => {
550
550
return { locals, imports, unresolved } ;
551
551
} ;
552
552
553
+ const resolveScope = ( scope : TSESLint . Scope . Scope , identifier : string ) => {
554
+ let currentScope : TSESLint . Scope . Scope | null = scope ;
555
+
556
+ while ( currentScope !== null ) {
557
+ for ( const ref of currentScope . variables ) {
558
+ if ( ref . defs . length === 0 ) {
559
+ continue ;
560
+ }
561
+
562
+ const def = ref . defs [ ref . defs . length - 1 ] ;
563
+
564
+ const importDetails = describePossibleImportDef ( def ) ;
565
+
566
+ if ( importDetails ?. local === identifier ) {
567
+ return importDetails ;
568
+ }
569
+
570
+ if ( ref . name === identifier ) {
571
+ return 'local' ;
572
+ }
573
+ }
574
+
575
+ currentScope = currentScope . upper ;
576
+ }
577
+
578
+ return null ;
579
+ } ;
580
+
553
581
interface ResolvedJestFn {
554
582
original : string | null ;
555
583
local : string ;
@@ -560,9 +588,13 @@ const resolveToJestFn = (
560
588
context : TSESLint . RuleContext < string , unknown [ ] > ,
561
589
identifier : string ,
562
590
) : ResolvedJestFn | null => {
563
- const references = collectReferences ( context . getScope ( ) ) ;
591
+ const maybeImport = resolveScope ( context . getScope ( ) , identifier ) ;
564
592
565
- const maybeImport = references . imports . get ( identifier ) ;
593
+ // the identifier was found as a local variable or function declaration
594
+ // meaning it's not a function from jest
595
+ if ( maybeImport === 'local' ) {
596
+ return null ;
597
+ }
566
598
567
599
if ( maybeImport ) {
568
600
// the identifier is imported from @jest /globals,
@@ -578,12 +610,6 @@ const resolveToJestFn = (
578
610
return null ;
579
611
}
580
612
581
- // the identifier was found as a local variable or function declaration
582
- // meaning it's not a function from jest
583
- if ( references . locals . has ( identifier ) ) {
584
- return null ;
585
- }
586
-
587
613
return {
588
614
original : resolvePossibleAliasedGlobal ( identifier , context ) ,
589
615
local : identifier ,
0 commit comments