From d6a11308013359a271e35e96b7c6d8814da0c56d Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 5 Nov 2022 09:59:19 +1300 Subject: [PATCH 1/3] refactor: inline `collectReferences` utility into `scopeHasLocalReference` --- src/rules/utils/parseJestFnCall.ts | 81 +++++++++++++----------------- 1 file changed, 34 insertions(+), 47 deletions(-) diff --git a/src/rules/utils/parseJestFnCall.ts b/src/rules/utils/parseJestFnCall.ts index 64905f95e..5ad2add93 100644 --- a/src/rules/utils/parseJestFnCall.ts +++ b/src/rules/utils/parseJestFnCall.ts @@ -514,42 +514,6 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => { return null; }; -const collectReferences = (scope: TSESLint.Scope.Scope) => { - const locals = new Set(); - const imports = new Map(); - const unresolved = new Set(); - - let currentScope: TSESLint.Scope.Scope | null = scope; - - while (currentScope !== null) { - for (const ref of currentScope.variables) { - if (ref.defs.length === 0) { - continue; - } - - const def = ref.defs[ref.defs.length - 1]; - - const importDetails = describePossibleImportDef(def); - - if (importDetails) { - imports.set(importDetails.local, importDetails); - - continue; - } - - locals.add(ref.name); - } - - for (const ref of currentScope.through) { - unresolved.add(ref.identifier.name); - } - - currentScope = currentScope.upper; - } - - return { locals, imports, unresolved }; -}; - const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => { let currentScope: TSESLint.Scope.Scope | null = scope; @@ -621,15 +585,38 @@ export const scopeHasLocalReference = ( scope: TSESLint.Scope.Scope, referenceName: string, ) => { - const references = collectReferences(scope); - - return ( - // referenceName was found as a local variable or function declaration. - references.locals.has(referenceName) || - // referenceName was found as an imported identifier - references.imports.has(referenceName) || - // referenceName was not found as an unresolved reference, - // meaning it is likely not an implicit global reference. - !references.unresolved.has(referenceName) - ); + let unresolved = false; + let currentScope: TSESLint.Scope.Scope | null = scope; + + while (currentScope !== null) { + for (const ref of currentScope.variables) { + if (ref.defs.length === 0) { + continue; + } + + const def = ref.defs[ref.defs.length - 1]; + + const importDetails = describePossibleImportDef(def); + + // referenceName was found as an imported identifier + if (importDetails?.local === referenceName) { + return true; + } + + // referenceName was found as a local variable or function declaration. + if (ref.name === referenceName) { + return true; + } + } + + if (!unresolved) { + for (const ref of currentScope.through) { + unresolved = ref.identifier.name !== referenceName; + } + } + + currentScope = currentScope.upper; + } + + return unresolved; }; From 3431af2849536cb531ba30d896505cec140e70d0 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 5 Nov 2022 10:05:34 +1300 Subject: [PATCH 2/3] refactor: optimize a little further --- src/rules/utils/parseJestFnCall.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/rules/utils/parseJestFnCall.ts b/src/rules/utils/parseJestFnCall.ts index 5ad2add93..3a62d767f 100644 --- a/src/rules/utils/parseJestFnCall.ts +++ b/src/rules/utils/parseJestFnCall.ts @@ -585,7 +585,6 @@ export const scopeHasLocalReference = ( scope: TSESLint.Scope.Scope, referenceName: string, ) => { - let unresolved = false; let currentScope: TSESLint.Scope.Scope | null = scope; while (currentScope !== null) { @@ -609,14 +608,14 @@ export const scopeHasLocalReference = ( } } - if (!unresolved) { - for (const ref of currentScope.through) { - unresolved = ref.identifier.name !== referenceName; - } + if ( + currentScope.through.every(ref => ref.identifier.name !== referenceName) + ) { + return true; } currentScope = currentScope.upper; } - return unresolved; + return false; }; From b1c1c352ada8f5c9bc868808919043d44e9104b6 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 5 Nov 2022 10:14:59 +1300 Subject: [PATCH 3/3] refactor: remove unneeded checks --- src/rules/utils/parseJestFnCall.ts | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/src/rules/utils/parseJestFnCall.ts b/src/rules/utils/parseJestFnCall.ts index 3a62d767f..0430b29d7 100644 --- a/src/rules/utils/parseJestFnCall.ts +++ b/src/rules/utils/parseJestFnCall.ts @@ -603,15 +603,7 @@ export const scopeHasLocalReference = ( } // referenceName was found as a local variable or function declaration. - if (ref.name === referenceName) { - return true; - } - } - - if ( - currentScope.through.every(ref => ref.identifier.name !== referenceName) - ) { - return true; + return ref.name === referenceName; } currentScope = currentScope.upper;