Skip to content

Commit 2f032f8

Browse files
k-yleG-RathSimenB
authored
fix(no-done-callback): fix regression with it.each (#708)
* 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 <[email protected]> Co-authored-by: Gareth Jones <[email protected]> Co-authored-by: Simen Bekkhus <[email protected]>
1 parent ce08024 commit 2f032f8

File tree

2 files changed

+63
-4
lines changed

2 files changed

+63
-4
lines changed

src/rules/__tests__/no-done-callback.test.ts

+44
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ ruleTester.run('no-done-callback', rule, {
1515
'test("something", () => {})',
1616
'test("something", async () => {})',
1717
'test("something", function() {})',
18+
'test.each``("something", ({ a, b }) => {})',
19+
'test.each()("something", ({ a, b }) => {})',
20+
'it.each()("something", ({ a, b }) => {})',
21+
'it.each``("something", ({ a, b }) => {})',
1822
'test("something", async function () {})',
1923
'test("something", someArg)',
2024
'beforeEach(() => {})',
@@ -385,5 +389,45 @@ ruleTester.run('no-done-callback', rule, {
385389
},
386390
],
387391
},
392+
{
393+
code: 'test.each``("something", ({ a, b }, done) => { done(); })',
394+
errors: [
395+
{
396+
messageId: 'noDoneCallback',
397+
line: 1,
398+
column: 37,
399+
},
400+
],
401+
},
402+
{
403+
code: 'test.each()("something", ({ a, b }, done) => { done(); })',
404+
errors: [
405+
{
406+
messageId: 'noDoneCallback',
407+
line: 1,
408+
column: 37,
409+
},
410+
],
411+
},
412+
{
413+
code: 'it.each``("something", ({ a, b }, done) => { done(); })',
414+
errors: [
415+
{
416+
messageId: 'noDoneCallback',
417+
line: 1,
418+
column: 35,
419+
},
420+
],
421+
},
422+
{
423+
code: 'it.each()("something", ({ a, b }, done) => { done(); })',
424+
errors: [
425+
{
426+
messageId: 'noDoneCallback',
427+
line: 1,
428+
column: 35,
429+
},
430+
],
431+
},
388432
],
389433
});

src/rules/no-done-callback.ts

+19-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@ import {
22
AST_NODE_TYPES,
33
TSESTree,
44
} from '@typescript-eslint/experimental-utils';
5-
import { createRule, isFunction, isHook, isTestCase } from './utils';
5+
import {
6+
createRule,
7+
getNodeName,
8+
isFunction,
9+
isHook,
10+
isTestCase,
11+
} from './utils';
612

713
const findCallbackArg = (
814
node: TSESTree.CallExpression,
15+
isJestEach: boolean,
916
): TSESTree.CallExpression['arguments'][0] | null => {
17+
if (isJestEach) {
18+
return node.arguments[1];
19+
}
20+
1021
if (isHook(node) && node.arguments.length >= 1) {
1122
return node.arguments[0];
1223
}
@@ -41,17 +52,21 @@ export default createRule({
4152
create(context) {
4253
return {
4354
CallExpression(node) {
44-
const callback = findCallbackArg(node);
55+
// done is the second argument for it.each, not the first
56+
const isJestEach = getNodeName(node.callee)?.endsWith('.each') ?? false;
57+
58+
const callback = findCallbackArg(node, isJestEach);
59+
const callbackArgIndex = Number(isJestEach);
4560

4661
if (
4762
!callback ||
4863
!isFunction(callback) ||
49-
callback.params.length !== 1
64+
callback.params.length !== 1 + callbackArgIndex
5065
) {
5166
return;
5267
}
5368

54-
const [argument] = callback.params;
69+
const argument = callback.params[callbackArgIndex];
5570

5671
if (argument.type !== AST_NODE_TYPES.Identifier) {
5772
context.report({

0 commit comments

Comments
 (0)