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(no-done-callback): fix regression with it.each #708

Merged
merged 3 commits into from Nov 12, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 44 additions & 0 deletions src/rules/__tests__/no-done-callback.test.ts
Expand Up @@ -15,6 +15,10 @@ ruleTester.run('no-done-callback', rule, {
'test("something", () => {})',
'test("something", async () => {})',
'test("something", function() {})',
'test.each``("something", ({ a, b }) => {})',
'test.each()("something", ({ a, b }) => {})',
'it.each()("something", ({ a, b }) => {})',
'it.each``("something", ({ a, b }) => {})',
'test("something", async function () {})',
'test("something", someArg)',
'beforeEach(() => {})',
Expand Down Expand Up @@ -385,5 +389,45 @@ ruleTester.run('no-done-callback', rule, {
},
],
},
{
code: 'test.each``("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 37,
},
],
},
{
code: 'test.each()("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 37,
},
],
},
{
code: 'it.each``("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 35,
},
],
},
{
code: 'it.each()("something", ({ a, b }, done) => { done(); })',
errors: [
{
messageId: 'noDoneCallback',
line: 1,
column: 35,
},
],
},
],
});
21 changes: 17 additions & 4 deletions src/rules/no-done-callback.ts
Expand Up @@ -2,11 +2,20 @@ import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { createRule, isFunction, isHook, isTestCase } from './utils';
import {
createRule,
getNodeName,
isFunction,
isHook,
isTestCase,
} from './utils';

const findCallbackArg = (
node: TSESTree.CallExpression,
isJestEach: boolean,
): TSESTree.CallExpression['arguments'][0] | null => {
if (isJestEach) return node.arguments[1];
k-yle marked this conversation as resolved.
Show resolved Hide resolved

if (isHook(node) && node.arguments.length >= 1) {
return node.arguments[0];
}
Expand Down Expand Up @@ -41,17 +50,21 @@ export default createRule({
create(context) {
return {
CallExpression(node) {
const callback = findCallbackArg(node);
// done is the second argument for it.each, not the first
const isJestEach = !!getNodeName(node.callee)?.endsWith('.each');
k-yle marked this conversation as resolved.
Show resolved Hide resolved

const callback = findCallbackArg(node, isJestEach);
const callbackArgIndex = +isJestEach;
k-yle marked this conversation as resolved.
Show resolved Hide resolved

if (
!callback ||
!isFunction(callback) ||
callback.params.length !== 1
callback.params.length !== 1 + callbackArgIndex
) {
return;
}

const [argument] = callback.params;
const argument = callback.params[callbackArgIndex];

if (argument.type !== AST_NODE_TYPES.Identifier) {
context.report({
Expand Down