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

fix: don't consider method calls on literals or new to be jest functions #1132

Merged
merged 1 commit into from May 29, 2022
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
4 changes: 4 additions & 0 deletions src/rules/utils/__tests__/parseJestFnCall.test.ts
Expand Up @@ -114,6 +114,10 @@ ruleTester.run('nonexistent methods', rule, {
'describe.concurrent()()',
'describe.concurrent``()',
'describe.every``()',
'/regex/.test()',
'"something".describe()',
'[].describe()',
'new describe().only()',
],
invalid: [],
});
Expand Down
14 changes: 9 additions & 5 deletions src/rules/utils/parseJestFnCall.ts
Expand Up @@ -21,7 +21,12 @@ export const isTypeOfJestFnCall = (
return jestFnCall !== null && types.includes(jestFnCall.type);
};

export function getNodeChain(node: TSESTree.Node): AccessorNode[] {
const joinChains = (
a: AccessorNode[] | null,
b: AccessorNode[] | null,
): AccessorNode[] | null => (a && b ? [...a, ...b] : null);

export function getNodeChain(node: TSESTree.Node): AccessorNode[] | null {
if (isSupportedAccessor(node)) {
return [node];
}
Expand All @@ -30,13 +35,12 @@ export function getNodeChain(node: TSESTree.Node): AccessorNode[] {
case AST_NODE_TYPES.TaggedTemplateExpression:
return getNodeChain(node.tag);
case AST_NODE_TYPES.MemberExpression:
return [...getNodeChain(node.object), ...getNodeChain(node.property)];
case AST_NODE_TYPES.NewExpression:
return joinChains(getNodeChain(node.object), getNodeChain(node.property));
case AST_NODE_TYPES.CallExpression:
return getNodeChain(node.callee);
}

return [];
return null;
}

export interface ResolvedJestFnWithNode extends ResolvedJestFn {
Expand Down Expand Up @@ -152,7 +156,7 @@ export const parseJestFnCall = (

const chain = getNodeChain(node);

if (chain.length === 0) {
if (!chain?.length) {
return null;
}

Expand Down