Skip to content

Commit d0a8428

Browse files
G-RathSimenB
authored andcommitted
chore(no-test-callback): migrate to TS (#321)
1 parent ccbe766 commit d0a8428

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/rules/__tests__/no-test-callback.test.js renamed to src/rules/__tests__/no-test-callback.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { RuleTester } from 'eslint';
1+
import { TSESLint } from '@typescript-eslint/experimental-utils';
22
import rule from '../no-test-callback';
33

4-
const ruleTester = new RuleTester({
4+
const ruleTester = new TSESLint.RuleTester({
55
parserOptions: {
66
ecmaVersion: 8,
77
},

src/rules/no-test-callback.js renamed to src/rules/no-test-callback.ts

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { getDocsUrl, isTestCase } from './util';
1+
import { createRule, isFunction, isTestCase } from './tsUtils';
22

3-
export default {
3+
export default createRule({
4+
name: __filename,
45
meta: {
56
docs: {
6-
url: getDocsUrl(__filename),
7+
category: 'Best Practices',
8+
description: 'Avoid using a callback in asynchronous tests',
9+
recommended: false,
710
},
811
messages: {
912
illegalTestCallback: 'Illegal usage of test callback',
1013
},
1114
fixable: 'code',
1215
schema: [],
16+
type: 'suggestion',
1317
},
18+
defaultOptions: [],
1419
create(context) {
1520
return {
1621
CallExpression(node) {
@@ -20,24 +25,44 @@ export default {
2025

2126
const [, callback] = node.arguments;
2227

23-
if (
24-
!/^(Arrow)?FunctionExpression$/.test(callback.type) ||
25-
callback.params.length !== 1
26-
) {
28+
if (!isFunction(callback) || callback.params.length !== 1) {
2729
return;
2830
}
2931

3032
const [argument] = callback.params;
33+
3134
context.report({
3235
node: argument,
3336
messageId: 'illegalTestCallback',
3437
fix(fixer) {
35-
const sourceCode = context.getSourceCode();
3638
const { body } = callback;
39+
40+
/* istanbul ignore if */
41+
if (!body) {
42+
throw new Error(
43+
`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
44+
);
45+
}
46+
47+
const sourceCode = context.getSourceCode();
3748
const firstBodyToken = sourceCode.getFirstToken(body);
3849
const lastBodyToken = sourceCode.getLastToken(body);
3950
const tokenBeforeArgument = sourceCode.getTokenBefore(argument);
4051
const tokenAfterArgument = sourceCode.getTokenAfter(argument);
52+
53+
/* istanbul ignore if */
54+
if (
55+
!('name' in argument) ||
56+
!firstBodyToken ||
57+
!lastBodyToken ||
58+
!tokenBeforeArgument ||
59+
!tokenAfterArgument
60+
) {
61+
throw new Error(
62+
`Unexpected null when attempting to fix ${context.getFilename()} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
63+
);
64+
}
65+
4166
const argumentInParens =
4267
tokenBeforeArgument.value === '(' &&
4368
tokenAfterArgument.value === ')';
@@ -78,4 +103,4 @@ export default {
78103
},
79104
};
80105
},
81-
};
106+
});

0 commit comments

Comments
 (0)