From 2f032f8d890e3717359d099b1e93e0cc6b52996a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ky=E2=84=93e=20Hensel?= Date: Thu, 12 Nov 2020 23:30:32 +1300 Subject: [PATCH] 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 Co-authored-by: Gareth Jones Co-authored-by: Simen Bekkhus --- src/rules/__tests__/no-done-callback.test.ts | 44 ++++++++++++++++++++ src/rules/no-done-callback.ts | 23 ++++++++-- 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/rules/__tests__/no-done-callback.test.ts b/src/rules/__tests__/no-done-callback.test.ts index f4fc63903..d91c19d28 100644 --- a/src/rules/__tests__/no-done-callback.test.ts +++ b/src/rules/__tests__/no-done-callback.test.ts @@ -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(() => {})', @@ -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, + }, + ], + }, ], }); diff --git a/src/rules/no-done-callback.ts b/src/rules/no-done-callback.ts index 70fde6caa..345722dae 100644 --- a/src/rules/no-done-callback.ts +++ b/src/rules/no-done-callback.ts @@ -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]; } @@ -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({