Skip to content

Commit

Permalink
fix(no-done-callback): fix regression with it.each (#708)
Browse files Browse the repository at this point in the history
* fix(no-done-callback): fix regression with it.each

* chore: apply suggestions from code review

* chore: apply another suggestion from code review

Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>

Co-authored-by: Gareth Jones <Jones258@Gmail.com>
Co-authored-by: Simen Bekkhus <sbekkhus91@gmail.com>
  • Loading branch information
3 people authored Nov 12, 2020
1 parent ce08024 commit 2f032f8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
44 changes: 44 additions & 0 deletions src/rules/__tests__/no-done-callback.test.ts
Original file line number Diff line number Diff line change
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,
},
],
},
],
});
23 changes: 19 additions & 4 deletions src/rules/no-done-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,22 @@ 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];
}

if (isHook(node) && node.arguments.length >= 1) {
return node.arguments[0];
}
Expand Down Expand Up @@ -41,17 +52,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') ?? false;

const callback = findCallbackArg(node, isJestEach);
const callbackArgIndex = Number(isJestEach);

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

0 comments on commit 2f032f8

Please # to comment.