Skip to content

Commit 08e130c

Browse files
authored
perf: don't collect more info than needed when resolving jest functions (#172)
1 parent c9bb32f commit 08e130c

File tree

1 file changed

+13
-23
lines changed

1 file changed

+13
-23
lines changed

src/rules/utils/parseJestFnCall.ts

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -499,11 +499,7 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => {
499499
return null;
500500
};
501501

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) => {
507503
let currentScope: TSESLint.Scope.Scope | null = scope;
508504

509505
while (currentScope !== null) {
@@ -516,23 +512,19 @@ const collectReferences = (scope: TSESLint.Scope.Scope) => {
516512

517513
const importDetails = describePossibleImportDef(def);
518514

519-
if (importDetails) {
520-
imports.set(importDetails.local, importDetails);
521-
522-
continue;
515+
if (importDetails?.local === identifier) {
516+
return importDetails;
523517
}
524518

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+
}
530522
}
531523

532524
currentScope = currentScope.upper;
533525
}
534526

535-
return { locals, imports, unresolved };
527+
return null;
536528
};
537529

538530
interface ResolvedJestFn {
@@ -545,9 +537,13 @@ const resolveToJestFn = (
545537
context: TSESLint.RuleContext<string, unknown[]>,
546538
identifier: string,
547539
): ResolvedJestFn | null => {
548-
const references = collectReferences(context.getScope());
540+
const maybeImport = resolveScope(context.getScope(), identifier);
549541

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+
}
551547

552548
if (maybeImport) {
553549
// the identifier is imported from @jest/globals,
@@ -563,12 +559,6 @@ const resolveToJestFn = (
563559
return null;
564560
}
565561

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-
572562
return {
573563
original: resolvePossibleAliasedGlobal(identifier, context),
574564
local: identifier,

0 commit comments

Comments
 (0)