Skip to content

Commit 6f85b64

Browse files
authored
refactor: inline collectReferences utility into scopeHasLocalReference (#1276)
* refactor: inline `collectReferences` utility into `scopeHasLocalReference` * refactor: optimize a little further * refactor: remove unneeded checks
1 parent 49e6f2f commit 6f85b64

File tree

1 file changed

+25
-47
lines changed

1 file changed

+25
-47
lines changed

src/rules/utils/parseJestFnCall.ts

Lines changed: 25 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -514,42 +514,6 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => {
514514
return null;
515515
};
516516

517-
const collectReferences = (scope: TSESLint.Scope.Scope) => {
518-
const locals = new Set();
519-
const imports = new Map<string, ImportDetails>();
520-
const unresolved = new Set();
521-
522-
let currentScope: TSESLint.Scope.Scope | null = scope;
523-
524-
while (currentScope !== null) {
525-
for (const ref of currentScope.variables) {
526-
if (ref.defs.length === 0) {
527-
continue;
528-
}
529-
530-
const def = ref.defs[ref.defs.length - 1];
531-
532-
const importDetails = describePossibleImportDef(def);
533-
534-
if (importDetails) {
535-
imports.set(importDetails.local, importDetails);
536-
537-
continue;
538-
}
539-
540-
locals.add(ref.name);
541-
}
542-
543-
for (const ref of currentScope.through) {
544-
unresolved.add(ref.identifier.name);
545-
}
546-
547-
currentScope = currentScope.upper;
548-
}
549-
550-
return { locals, imports, unresolved };
551-
};
552-
553517
const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => {
554518
let currentScope: TSESLint.Scope.Scope | null = scope;
555519

@@ -621,15 +585,29 @@ export const scopeHasLocalReference = (
621585
scope: TSESLint.Scope.Scope,
622586
referenceName: string,
623587
) => {
624-
const references = collectReferences(scope);
625-
626-
return (
627-
// referenceName was found as a local variable or function declaration.
628-
references.locals.has(referenceName) ||
629-
// referenceName was found as an imported identifier
630-
references.imports.has(referenceName) ||
631-
// referenceName was not found as an unresolved reference,
632-
// meaning it is likely not an implicit global reference.
633-
!references.unresolved.has(referenceName)
634-
);
588+
let currentScope: TSESLint.Scope.Scope | null = scope;
589+
590+
while (currentScope !== null) {
591+
for (const ref of currentScope.variables) {
592+
if (ref.defs.length === 0) {
593+
continue;
594+
}
595+
596+
const def = ref.defs[ref.defs.length - 1];
597+
598+
const importDetails = describePossibleImportDef(def);
599+
600+
// referenceName was found as an imported identifier
601+
if (importDetails?.local === referenceName) {
602+
return true;
603+
}
604+
605+
// referenceName was found as a local variable or function declaration.
606+
return ref.name === referenceName;
607+
}
608+
609+
currentScope = currentScope.upper;
610+
}
611+
612+
return false;
635613
};

0 commit comments

Comments
 (0)