Skip to content

Commit d0652cd

Browse files
authored
perf: use Set instead of iterating, and deduplicate a function (#175)
1 parent d9ca229 commit d0652cd

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/rules/utils/__tests__/parseJestFnCall.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,23 @@ ruleTester.run('esm', rule, {
441441
`,
442442
parserOptions: { sourceType: 'module' },
443443
},
444+
{
445+
code: dedent`
446+
import ByDefault from './myfile';
447+
448+
ByDefault.sayHello();
449+
`,
450+
parserOptions: { sourceType: 'module' },
451+
},
452+
{
453+
code: dedent`
454+
async function doSomething() {
455+
const build = await rollup(config);
456+
build.generate();
457+
}
458+
`,
459+
parserOptions: { sourceType: 'module', ecmaVersion: 2017 },
460+
},
444461
],
445462
invalid: [],
446463
});
@@ -782,6 +799,14 @@ ruleTester.run('typescript', rule, {
782799
parser: require.resolve('@typescript-eslint/parser'),
783800
parserOptions: { sourceType: 'module' },
784801
},
802+
{
803+
code: dedent`
804+
import dedent = require('dedent');
805+
806+
dedent();
807+
`,
808+
parser: require.resolve('@typescript-eslint/parser'),
809+
},
785810
],
786811
invalid: [
787812
{

src/rules/utils/parseJestFnCall.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ const findImportSourceNode = (
432432
): TSESTree.Node | null => {
433433
if (node.type === AST_NODE_TYPES.AwaitExpression) {
434434
if (node.argument.type === AST_NODE_TYPES.ImportExpression) {
435-
return (node.argument as TSESTree.ImportExpression).source;
435+
return node.argument.source;
436436
}
437437

438438
return null;
@@ -499,15 +499,16 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => {
499499
return null;
500500
};
501501

502-
const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => {
502+
const resolveScope = (
503+
scope: TSESLint.Scope.Scope,
504+
identifier: string,
505+
): ImportDetails | 'local' | null => {
503506
let currentScope: TSESLint.Scope.Scope | null = scope;
504507

505508
while (currentScope !== null) {
506-
for (const ref of currentScope.variables) {
507-
if (ref.defs.length === 0) {
508-
continue;
509-
}
509+
const ref = currentScope.set.get(identifier);
510510

511+
if (ref && ref.defs.length > 0) {
511512
const def = ref.defs[ref.defs.length - 1];
512513

513514
const importDetails = describePossibleImportDef(def);
@@ -516,9 +517,7 @@ const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => {
516517
return importDetails;
517518
}
518519

519-
if (ref.name === identifier) {
520-
return 'local';
521-
}
520+
return 'local';
522521
}
523522

524523
currentScope = currentScope.upper;

0 commit comments

Comments
 (0)