Skip to content

Commit

Permalink
perf: don't collect more info than needed when resolving jest functio…
Browse files Browse the repository at this point in the history
…ns (#172)
  • Loading branch information
G-Rath authored Apr 19, 2024
1 parent c9bb32f commit 08e130c
Showing 1 changed file with 13 additions and 23 deletions.
36 changes: 13 additions & 23 deletions src/rules/utils/parseJestFnCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,7 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => {
return null;
};

const collectReferences = (scope: TSESLint.Scope.Scope) => {
const locals = new Set();
const imports = new Map<string, ImportDetails>();
const unresolved = new Set();

const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => {
let currentScope: TSESLint.Scope.Scope | null = scope;

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

const importDetails = describePossibleImportDef(def);

if (importDetails) {
imports.set(importDetails.local, importDetails);

continue;
if (importDetails?.local === identifier) {
return importDetails;
}

locals.add(ref.name);
}

for (const ref of currentScope.through) {
unresolved.add(ref.identifier.name);
if (ref.name === identifier) {
return 'local';
}
}

currentScope = currentScope.upper;
}

return { locals, imports, unresolved };
return null;
};

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

const maybeImport = references.imports.get(identifier);
// the identifier was found as a local variable or function declaration
// meaning it's not a function from jest
if (maybeImport === 'local') {
return null;
}

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

// the identifier was found as a local variable or function declaration
// meaning it's not a function from jest
if (references.locals.has(identifier)) {
return null;
}

return {
original: resolvePossibleAliasedGlobal(identifier, context),
local: identifier,
Expand Down

0 comments on commit 08e130c

Please sign in to comment.