From 3a2687e6706f4406cc4a2dc28a753fff22bf77f3 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sun, 21 Jul 2019 22:00:01 +1200 Subject: [PATCH] chore(no-test-callback): convert to typescript (#321) --- ...lback.test.js => no-test-callback.test.ts} | 4 +- ...o-test-callback.js => no-test-callback.ts} | 43 +++++++++++++++---- 2 files changed, 36 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} (65%) 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 65% rename from src/rules/no-test-callback.js rename to src/rules/no-test-callback.ts index 3370c9ed7..250b61a66 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,44 @@ 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; + context.report({ node: argument, messageId: 'illegalTestCallback', fix(fixer) { - const sourceCode = context.getSourceCode(); const { body } = callback; + + /* istanbul ignore if */ + if (!body) { + 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(); const firstBodyToken = sourceCode.getFirstToken(body); const lastBodyToken = sourceCode.getLastToken(body); const tokenBeforeArgument = sourceCode.getTokenBefore(argument); const tokenAfterArgument = sourceCode.getTokenAfter(argument); + + /* istanbul ignore if */ + if ( + !('name' in argument) || + !firstBodyToken || + !lastBodyToken || + !tokenBeforeArgument || + !tokenAfterArgument + ) { + 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 = tokenBeforeArgument.value === '(' && tokenAfterArgument.value === ')'; @@ -78,4 +103,4 @@ export default { }, }; }, -}; +});