From 8ad3c29e3a0ac04475eaca358c57f4d0cbd9a324 Mon Sep 17 00:00:00 2001 From: Kyle Hensel Date: Thu, 12 Nov 2020 22:06:11 +1300 Subject: [PATCH 1/3] fix(no-done-callback): fix regression with it.each --- src/rules/__tests__/no-done-callback.test.ts | 44 ++++++++++++++++++++ src/rules/no-done-callback.ts | 21 ++++++++-- 2 files changed, 61 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..c010dbee1 100644 --- a/src/rules/no-done-callback.ts +++ b/src/rules/no-done-callback.ts @@ -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]; + if (isHook(node) && node.arguments.length >= 1) { return node.arguments[0]; } @@ -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'); + + const callback = findCallbackArg(node, isJestEach); + const callbackArgIndex = +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({ From ea665bd76d08474ac316979f4255ca4f5f864bed Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Thu, 12 Nov 2020 23:11:13 +1300 Subject: [PATCH 2/3] chore: apply suggestions from code review --- src/rules/no-done-callback.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/rules/no-done-callback.ts b/src/rules/no-done-callback.ts index c010dbee1..d6baa2433 100644 --- a/src/rules/no-done-callback.ts +++ b/src/rules/no-done-callback.ts @@ -14,7 +14,9 @@ const findCallbackArg = ( node: TSESTree.CallExpression, isJestEach: boolean, ): TSESTree.CallExpression['arguments'][0] | null => { - if (isJestEach) return node.arguments[1]; + if (isJestEach) { + return node.arguments[1]; + } if (isHook(node) && node.arguments.length >= 1) { return node.arguments[0]; @@ -54,7 +56,7 @@ export default createRule({ const isJestEach = !!getNodeName(node.callee)?.endsWith('.each'); const callback = findCallbackArg(node, isJestEach); - const callbackArgIndex = +isJestEach; + const callbackArgIndex = Number(isJestEach); if ( !callback || From 40e2962fa0b1343455a80b0df260caed7c7e0d30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ky=E2=84=93e=20Hensel?= Date: Thu, 12 Nov 2020 23:23:23 +1300 Subject: [PATCH 3/3] chore: apply another suggestion from code review Co-authored-by: Simen Bekkhus --- src/rules/no-done-callback.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/rules/no-done-callback.ts b/src/rules/no-done-callback.ts index d6baa2433..345722dae 100644 --- a/src/rules/no-done-callback.ts +++ b/src/rules/no-done-callback.ts @@ -53,7 +53,7 @@ export default createRule({ return { CallExpression(node) { // done is the second argument for it.each, not the first - const isJestEach = !!getNodeName(node.callee)?.endsWith('.each'); + const isJestEach = getNodeName(node.callee)?.endsWith('.each') ?? false; const callback = findCallbackArg(node, isJestEach); const callbackArgIndex = Number(isJestEach);