From fbfdf91d39ce51ba55139af4b78f42f1c24b0313 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 20:38:50 +1200 Subject: [PATCH 1/3] chore(no-test-callback): convert to typescript --- ...lback.test.js => no-test-callback.test.ts} | 4 +-- ...o-test-callback.js => no-test-callback.ts} | 35 ++++++++++++++----- 2 files changed, 28 insertions(+), 11 deletions(-) rename src/rules/__tests__/{no-test-callback.test.js => no-test-callback.test.ts} (95%) rename src/rules/{no-test-callback.js => no-test-callback.ts} (74%) diff --git a/src/rules/__tests__/no-test-callback.test.js b/src/rules/__tests__/no-test-callback.test.ts similarity index 95% rename from src/rules/__tests__/no-test-callback.test.js rename to src/rules/__tests__/no-test-callback.test.ts index 70573537c..129640dea 100644 --- a/src/rules/__tests__/no-test-callback.test.js +++ b/src/rules/__tests__/no-test-callback.test.ts @@ -1,7 +1,7 @@ -import { RuleTester } from 'eslint'; +import { TSESLint } from '@typescript-eslint/experimental-utils'; import rule from '../no-test-callback'; -const ruleTester = new RuleTester({ +const ruleTester = new TSESLint.RuleTester({ parserOptions: { ecmaVersion: 8, }, diff --git a/src/rules/no-test-callback.js b/src/rules/no-test-callback.ts similarity index 74% rename from src/rules/no-test-callback.js rename to src/rules/no-test-callback.ts index 3370c9ed7..41c0d2497 100644 --- a/src/rules/no-test-callback.js +++ b/src/rules/no-test-callback.ts @@ -1,16 +1,21 @@ -import { getDocsUrl, isTestCase } from './util'; +import { createRule, isFunction, isTestCase } from './tsUtils'; -export default { +export default createRule({ + name: __filename, meta: { docs: { - url: getDocsUrl(__filename), + category: 'Best Practices', + description: 'Avoid using a callback in asynchronous tests', + recommended: false, }, messages: { illegalTestCallback: 'Illegal usage of test callback', }, fixable: 'code', schema: [], + type: 'suggestion', }, + defaultOptions: [], create(context) { return { CallExpression(node) { @@ -20,24 +25,36 @@ export default { const [, callback] = node.arguments; - if ( - !/^(Arrow)?FunctionExpression$/.test(callback.type) || - callback.params.length !== 1 - ) { + if (!isFunction(callback) || callback.params.length !== 1) { return; } const [argument] = callback.params; + const { body } = callback; + + if (!body || !('name' in argument)) { + return; + } + context.report({ node: argument, messageId: 'illegalTestCallback', fix(fixer) { const sourceCode = context.getSourceCode(); - const { body } = callback; const firstBodyToken = sourceCode.getFirstToken(body); const lastBodyToken = sourceCode.getLastToken(body); const tokenBeforeArgument = sourceCode.getTokenBefore(argument); const tokenAfterArgument = sourceCode.getTokenAfter(argument); + + if ( + !firstBodyToken || + !lastBodyToken || + !tokenBeforeArgument || + !tokenAfterArgument + ) { + throw new Error('Unexpected null - please file a github issue'); + } + const argumentInParens = tokenBeforeArgument.value === '(' && tokenAfterArgument.value === ')'; @@ -78,4 +95,4 @@ export default { }, }; }, -}; +}); From 72840eb0168a3badf5cf9aad3562ae722143c319 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 20 Jul 2019 21:07:12 +1200 Subject: [PATCH 2/3] chore(no-test-callback): move existence checks into fixer body --- src/rules/no-test-callback.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/rules/no-test-callback.ts b/src/rules/no-test-callback.ts index 41c0d2497..2b1c1239e 100644 --- a/src/rules/no-test-callback.ts +++ b/src/rules/no-test-callback.ts @@ -30,16 +30,17 @@ export default createRule({ } const [argument] = callback.params; - const { body } = callback; - - if (!body || !('name' in argument)) { - return; - } context.report({ node: argument, messageId: 'illegalTestCallback', fix(fixer) { + const { body } = callback; + + if (!body) { + throw new Error('Unexpected null - please file a github issue'); + } + const sourceCode = context.getSourceCode(); const firstBodyToken = sourceCode.getFirstToken(body); const lastBodyToken = sourceCode.getLastToken(body); @@ -47,6 +48,7 @@ export default createRule({ const tokenAfterArgument = sourceCode.getTokenAfter(argument); if ( + !('name' in argument) || !firstBodyToken || !lastBodyToken || !tokenBeforeArgument || From 9c52ed9146ae144c3beae50fed6ab3945d93fb77 Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 21 Jul 2019 11:56:58 +0200 Subject: [PATCH 3/3] chore: tweak error message and ignore it for coverage --- src/rules/no-test-callback.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/rules/no-test-callback.ts b/src/rules/no-test-callback.ts index 2b1c1239e..250b61a66 100644 --- a/src/rules/no-test-callback.ts +++ b/src/rules/no-test-callback.ts @@ -37,8 +37,11 @@ export default createRule({ fix(fixer) { const { body } = callback; + /* istanbul ignore if */ if (!body) { - throw new Error('Unexpected null - please file a github issue'); + throw new Error( + `Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`, + ); } const sourceCode = context.getSourceCode(); @@ -47,6 +50,7 @@ export default createRule({ const tokenBeforeArgument = sourceCode.getTokenBefore(argument); const tokenAfterArgument = sourceCode.getTokenAfter(argument); + /* istanbul ignore if */ if ( !('name' in argument) || !firstBodyToken || @@ -54,7 +58,9 @@ export default createRule({ !tokenBeforeArgument || !tokenAfterArgument ) { - throw new Error('Unexpected null - please file a github issue'); + throw new Error( + `Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`, + ); } const argumentInParens =