Skip to content

Commit 0e048f1

Browse files
authored
perf: use Set instead of iterating, and deduplicate a function (#1278)
* perf: use `Set` instead of iterating * test: add case for default import * test: add case for async function call * test: add case for `TSImportEqualsDeclaration` import * refactor: replace `scopeHasLocalReference` with `resolveScope`
1 parent ad04fcc commit 0e048f1

File tree

4 files changed

+37
-44
lines changed

4 files changed

+37
-44
lines changed

src/rules/no-disabled-tests.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
createRule,
33
getAccessorValue,
44
parseJestFnCall,
5-
scopeHasLocalReference,
5+
resolveScope,
66
} from './utils';
77

88
export default createRule({
@@ -80,7 +80,7 @@ export default createRule({
8080
}
8181
},
8282
'CallExpression[callee.name="pending"]'(node) {
83-
if (scopeHasLocalReference(context.getScope(), 'pending')) {
83+
if (resolveScope(context.getScope(), 'pending')) {
8484
return;
8585
}
8686

src/rules/no-jasmine-globals.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
createRule,
44
getNodeName,
55
isSupportedAccessor,
6-
scopeHasLocalReference,
6+
resolveScope,
77
} from './utils';
88

99
export default createRule({
@@ -46,7 +46,7 @@ export default createRule({
4646
calleeName === 'fail' ||
4747
calleeName === 'pending'
4848
) {
49-
if (scopeHasLocalReference(context.getScope(), calleeName)) {
49+
if (resolveScope(context.getScope(), calleeName)) {
5050
// It's a local variable, not a jasmine global.
5151
return;
5252
}

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

+25
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

+8-40
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ const findImportSourceNode = (
447447
): TSESTree.Node | null => {
448448
if (node.type === AST_NODE_TYPES.AwaitExpression) {
449449
if (node.argument.type === AST_NODE_TYPES.ImportExpression) {
450-
return (node.argument as TSESTree.ImportExpression).source;
450+
return node.argument.source;
451451
}
452452

453453
return null;
@@ -514,15 +514,16 @@ const describePossibleImportDef = (def: TSESLint.Scope.Definition) => {
514514
return null;
515515
};
516516

517-
const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => {
517+
export const resolveScope = (
518+
scope: TSESLint.Scope.Scope,
519+
identifier: string,
520+
): ImportDetails | 'local' | null => {
518521
let currentScope: TSESLint.Scope.Scope | null = scope;
519522

520523
while (currentScope !== null) {
521-
for (const ref of currentScope.variables) {
522-
if (ref.defs.length === 0) {
523-
continue;
524-
}
524+
const ref = currentScope.set.get(identifier);
525525

526+
if (ref && ref.defs.length > 0) {
526527
const def = ref.defs[ref.defs.length - 1];
527528

528529
const importDetails = describePossibleImportDef(def);
@@ -531,9 +532,7 @@ const resolveScope = (scope: TSESLint.Scope.Scope, identifier: string) => {
531532
return importDetails;
532533
}
533534

534-
if (ref.name === identifier) {
535-
return 'local';
536-
}
535+
return 'local';
537536
}
538537

539538
currentScope = currentScope.upper;
@@ -580,34 +579,3 @@ const resolveToJestFn = (
580579
type: 'global',
581580
};
582581
};
583-
584-
export const scopeHasLocalReference = (
585-
scope: TSESLint.Scope.Scope,
586-
referenceName: string,
587-
) => {
588-
let currentScope: TSESLint.Scope.Scope | null = scope;
589-
590-
while (currentScope !== null) {
591-
for (const ref of currentScope.variables) {
592-
if (ref.defs.length === 0) {
593-
continue;
594-
}
595-
596-
const def = ref.defs[ref.defs.length - 1];
597-
598-
const importDetails = describePossibleImportDef(def);
599-
600-
// referenceName was found as an imported identifier
601-
if (importDetails?.local === referenceName) {
602-
return true;
603-
}
604-
605-
// referenceName was found as a local variable or function declaration.
606-
return ref.name === referenceName;
607-
}
608-
609-
currentScope = currentScope.upper;
610-
}
611-
612-
return false;
613-
};

0 commit comments

Comments
 (0)