@@ -499,11 +499,7 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => {
499
499
return null ;
500
500
} ;
501
501
502
- const collectReferences = ( scope : TSESLint . Scope . Scope ) => {
503
- const locals = new Set ( ) ;
504
- const imports = new Map < string , ImportDetails > ( ) ;
505
- const unresolved = new Set ( ) ;
506
-
502
+ const resolveScope = ( scope : TSESLint . Scope . Scope , identifier : string ) => {
507
503
let currentScope : TSESLint . Scope . Scope | null = scope ;
508
504
509
505
while ( currentScope !== null ) {
@@ -516,23 +512,19 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => {
516
512
517
513
const importDetails = describePossibleImportDef ( def ) ;
518
514
519
- if ( importDetails ) {
520
- imports . set ( importDetails . local , importDetails ) ;
521
-
522
- continue ;
515
+ if ( importDetails ?. local === identifier ) {
516
+ return importDetails ;
523
517
}
524
518
525
- locals . add ( ref . name ) ;
526
- }
527
-
528
- for ( const ref of currentScope . through ) {
529
- unresolved . add ( ref . identifier . name ) ;
519
+ if ( ref . name === identifier ) {
520
+ return 'local' ;
521
+ }
530
522
}
531
523
532
524
currentScope = currentScope . upper ;
533
525
}
534
526
535
- return { locals , imports , unresolved } ;
527
+ return null ;
536
528
} ;
537
529
538
530
interface ResolvedJestFn {
@@ -545,9 +537,13 @@ const resolveToJestFn = (
545
537
context : TSESLint . RuleContext < string , unknown [ ] > ,
546
538
identifier : string ,
547
539
) : ResolvedJestFn | null => {
548
- const references = collectReferences ( context . getScope ( ) ) ;
540
+ const maybeImport = resolveScope ( context . getScope ( ) , identifier ) ;
549
541
550
- const maybeImport = references . imports . get ( identifier ) ;
542
+ // the identifier was found as a local variable or function declaration
543
+ // meaning it's not a function from jest
544
+ if ( maybeImport === 'local' ) {
545
+ return null ;
546
+ }
551
547
552
548
if ( maybeImport ) {
553
549
// the identifier is imported from @jest /globals,
@@ -563,12 +559,6 @@ const resolveToJestFn = (
563
559
return null ;
564
560
}
565
561
566
- // the identifier was found as a local variable or function declaration
567
- // meaning it's not a function from jest
568
- if ( references . locals . has ( identifier ) ) {
569
- return null ;
570
- }
571
-
572
562
return {
573
563
original : resolvePossibleAliasedGlobal ( identifier , context ) ,
574
564
local : identifier ,
0 commit comments