Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: use Set instead of iterating, and deduplicate a function #175

Merged
merged 1 commit into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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