Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { readdirSync } from 'fs';
import { join, parse } from 'path';
import { TSESLint } from '@typescript-eslint/utils';
import { TSESLint, TSESTree } from '@typescript-eslint/utils';

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

declare module '@typescript-eslint/utils/dist/ts-eslint/SourceCode' {
export interface SourceCode {
/**
* Returns the scope of the given node.
* This information can be used track references to variables.
* @since 8.37.0
*/
getScope(node: TSESTree.Node): TSESLint.Scope.Scope;
}
}

// copied from https://github.com/babel/babel/blob/d8da63c929f2d28c401571e2a43166678c555bc4/packages/babel-helpers/src/helpers.js#L602-L606
/* istanbul ignore next */
const interopRequireDefault = (obj: any): { default: any } =>
Expand Down
34 changes: 28 additions & 6 deletions src/rules/utils/parseJestFnCall.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { AST_NODE_TYPES, TSESLint, TSESTree } from '@typescript-eslint/utils';
import {
AccessorNode,
AST_NODE_TYPES,
type TSESLint,
type TSESTree,
} from '@typescript-eslint/utils';
import {
type AccessorNode,
DescribeAlias,
HookName,
KnownMemberExpression,
type KnownMemberExpression,
ModifierName,
TestCaseName,
findTopMostCallExpression,
Expand Down Expand Up @@ -238,7 +242,7 @@ const parseJestFnCallWithReasonInner = (
return null;
}

const resolved = resolveToJestFn(context, getAccessorValue(first));
const resolved = resolveToJestFn(context, first);

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

const resolveToJestFn = (
context: TSESLint.RuleContext<string, unknown[]>,
identifier: string,
accessor: AccessorNode,
): ResolvedJestFn | null => {
const maybeImport = resolveScope(context.getScope(), identifier);
const identifier = getAccessorValue(accessor);
const maybeImport = resolveScope(getScope(context, accessor), identifier);

// the identifier was found as a local variable or function declaration
// meaning it's not a function from jest
Expand Down Expand Up @@ -564,3 +569,20 @@ const resolveToJestFn = (
type: 'global',
};
};

/* istanbul ignore next */
const getScope = (
context: TSESLint.RuleContext<string, unknown[]>,
node: TSESTree.Node,
) => {
const sourceCode =
'sourceCode' in context
? (context.sourceCode as TSESLint.SourceCode)
: context.getSourceCode();

if ('getScope' in sourceCode) {
return sourceCode.getScope(node);
}

return context.getScope();
};