Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts-migration/no-test-callbacks #321

Merged
merged 3 commits into from Jul 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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,
},
Expand Down
43 changes: 34 additions & 9 deletions src/rules/no-test-callback.js → 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) {
Expand All @@ -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) {
SimenB marked this conversation as resolved.
Show resolved Hide resolved
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 === ')';
Expand Down Expand Up @@ -78,4 +103,4 @@ export default {
},
};
},
};
});