Skip to content

Commit 31c01ea

Browse files
authored
fix: replace use of deprecated methods (#178)
1 parent 0295807 commit 31c01ea

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

src/index.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { readdirSync } from 'fs';
22
import { join, parse } from 'path';
3-
import { TSESLint } from '@typescript-eslint/utils';
3+
import { TSESLint, TSESTree } from '@typescript-eslint/utils';
44

55
type RuleModule = TSESLint.RuleModule<string, unknown[]> & {
66
meta: Required<Pick<TSESLint.RuleMetaData<string>, 'docs'>>;
@@ -13,6 +13,17 @@ declare module '@typescript-eslint/utils/dist/ts-eslint/Rule' {
1313
}
1414
}
1515

16+
declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
17+
export interface SourceCode {
18+
/**
19+
* Returns the scope of the given node.
20+
* This information can be used track references to variables.
21+
* @since 8.37.0
22+
*/
23+
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
24+
}
25+
}
26+
1627
// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
1728
/* istanbul ignore next */
1829
const interopRequireDefault = (obj: any): { default: any } =>

src/rules/utils/parseJestFnCall.ts

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils';
21
import {
3-
AccessorNode,
2+
AST_NODE_TYPES,
3+
type TSESLint,
4+
type TSESTree,
5+
} from '@typescript-eslint/utils';
6+
import {
7+
type AccessorNode,
48
DescribeAlias,
59
HookName,
6-
KnownMemberExpression,
10+
type KnownMemberExpression,
711
ModifierName,
812
TestCaseName,
913
findTopMostCallExpression,
@@ -238,7 +242,7 @@ const parseJestFnCallWithReasonInner = (
238242
return null;
239243
}
240244

241-
const resolved = resolveToJestFn(context, getAccessorValue(first));
245+
const resolved = resolveToJestFn(context, first);
242246

243247
// we're not a jest function
244248
if (!resolved) {
@@ -534,9 +538,10 @@ interface ResolvedJestFn {
534538

535539
const resolveToJestFn = (
536540
context: TSESLint.RuleContext<string, unknown[]>,
537-
identifier: string,
541+
accessor: AccessorNode,
538542
): ResolvedJestFn | null => {
539-
const maybeImport = resolveScope(context.getScope(), identifier);
543+
const identifier = getAccessorValue(accessor);
544+
const maybeImport = resolveScope(getScope(context, accessor), identifier);
540545

541546
// the identifier was found as a local variable or function declaration
542547
// meaning it's not a function from jest
@@ -564,3 +569,20 @@ const resolveToJestFn = (
564569
type: 'global',
565570
};
566571
};
572+
573+
/* istanbul ignore next */
574+
const getScope = (
575+
context: TSESLint.RuleContext<string, unknown[]>,
576+
node: TSESTree.Node,
577+
) => {
578+
const sourceCode =
579+
'sourceCode' in context
580+
? (context.sourceCode as TSESLint.SourceCode)
581+
: context.getSourceCode();
582+
583+
if ('getScope' in sourceCode) {
584+
return sourceCode.getScope(node);
585+
}
586+
587+
return context.getScope();
588+
};

0 commit comments

Comments
 (0)