@@ -550,6 +550,34 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => {
550550 return { locals, imports, unresolved } ;
551551} ;
552552
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+
553581interface ResolvedJestFn {
554582 original : string | null ;
555583 local : string ;
@@ -560,9 +588,13 @@ const resolveToJestFn = (
560588 context : TSESLint . RuleContext < string , unknown [ ] > ,
561589 identifier : string ,
562590) : ResolvedJestFn | null => {
563- const references = collectReferences ( context . getScope ( ) ) ;
591+ const maybeImport = resolveScope ( context . getScope ( ) , identifier ) ;
564592
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+ }
566598
567599 if ( maybeImport ) {
568600 // the identifier is imported from @jest /globals,
@@ -578,12 +610,6 @@ const resolveToJestFn = (
578610 return null ;
579611 }
580612
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-
587613 return {
588614 original : resolvePossibleAliasedGlobal ( identifier , context ) ,
589615 local : identifier ,
0 commit comments