From d0652cdb82b692cdee0f2ef4616b96e89c6d4ddf Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Apr 2024 10:51:52 +1200 Subject: [PATCH] perf: use `Set` instead of iterating, and deduplicate a function (#175) --- .../utils/__tests__/parseJestFnCall.test.ts | 25 +++++++++++++++++++ src/rules/utils/parseJestFnCall.ts | 17 ++++++------- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/rules/utils/__tests__/parseJestFnCall.test.ts b/src/rules/utils/__tests__/parseJestFnCall.test.ts index 0e8214e..96fce1d 100644 --- a/src/rules/utils/__tests__/parseJestFnCall.test.ts +++ b/src/rules/utils/__tests__/parseJestFnCall.test.ts @@ -441,6 +441,23 @@ ruleTester.run('esm', rule, { `, parserOptions: { sourceType: 'module' }, }, + { + code: dedent` + import ByDefault from './myfile'; + + ByDefault.sayHello(); + `, + parserOptions: { sourceType: 'module' }, + }, + { + code: dedent` + async function doSomething() { + const build = await rollup(config); + build.generate(); + } + `, + parserOptions: { sourceType: 'module', ecmaVersion: 2017 }, + }, ], invalid: [], }); @@ -782,6 +799,14 @@ ruleTester.run('typescript', rule, { parser: require.resolve('@typescript-eslint/parser'), parserOptions: { sourceType: 'module' }, }, + { + code: dedent` + import dedent = require('dedent'); + + dedent(); + `, + parser: require.resolve('@typescript-eslint/parser'), + }, ], invalid: [ { diff --git a/src/rules/utils/parseJestFnCall.ts b/src/rules/utils/parseJestFnCall.ts index 38df6bd..c7e2fa4 100644 --- a/src/rules/utils/parseJestFnCall.ts +++ b/src/rules/utils/parseJestFnCall.ts @@ -432,7 +432,7 @@ const findImportSourceNode = ( ): TSESTree.Node | null => { if (node.type === AST_NODE_TYPES.AwaitExpression) { if (node.argument.type === AST_NODE_TYPES.ImportExpression) { - return (node.argument as TSESTree.ImportExpression).source; + return node.argument.source; } return null; @@ -499,15 +499,16 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => { return null; }; -const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => { +const resolveScope = ( + scope: TSESLint.Scope.Scope, + identifier: string, +): ImportDetails | 'local' | null => { let currentScope: TSESLint.Scope.Scope | null = scope; while (currentScope !== null) { - for (const ref of currentScope.variables) { - if (ref.defs.length === 0) { - continue; - } + const ref = currentScope.set.get(identifier); + if (ref && ref.defs.length > 0) { const def = ref.defs[ref.defs.length - 1]; const importDetails = describePossibleImportDef(def); @@ -516,9 +517,7 @@ const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => { return importDetails; } - if (ref.name === identifier) { - return 'local'; - } + return 'local'; } currentScope = currentScope.upper;