Skip to content

Commit

Permalink
perf: use Set instead of iterating, and deduplicate a function (#175)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Apr 19, 2024
1 parent d9ca229 commit d0652cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 9 deletions.
25 changes: 25 additions & 0 deletions src/rules/utils/__tests__/parseJestFnCall.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
});
Expand Down Expand Up @@ -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: [
{
Expand Down
17 changes: 8 additions & 9 deletions src/rules/utils/parseJestFnCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit d0652cd

Please sign in to comment.