-
Notifications
You must be signed in to change notification settings - Fork 227
/
prefer-todo.ts
119 lines (108 loc) · 2.94 KB
/
prefer-todo.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import {
AST_NODE_TYPES,
TSESLint,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
FunctionExpression,
JestFunctionCallExpression,
StringLiteral,
TestCaseName,
createRule,
getNodeName,
isFunction,
isStringNode,
isTestCase,
} from './tsUtils';
function isOnlyTestTitle(node: TSESTree.CallExpression) {
return node.arguments.length === 1;
}
function isFunctionBodyEmpty(node: FunctionExpression) {
/* istanbul ignore next https://github.com/typescript-eslint/typescript-eslint/issues/734 */
if (!node.body) {
throw new Error(
`Unexpected null while performing prefer-todo - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
);
}
return (
node.body.type === AST_NODE_TYPES.BlockStatement &&
node.body.body &&
!node.body.body.length
);
}
function isTestBodyEmpty(node: TSESTree.CallExpression) {
const [, fn] = node.arguments;
return fn && isFunction(fn) && isFunctionBodyEmpty(fn);
}
function addTodo(
node: JestFunctionCallExpression<TestCaseName>,
fixer: TSESLint.RuleFixer,
) {
const testName = getNodeName(node.callee)
.split('.')
.shift();
return fixer.replaceText(node.callee, `${testName}.todo`);
}
interface CallExpressionWithStringArgument extends TSESTree.CallExpression {
arguments: [StringLiteral | TSESTree.TemplateLiteral];
}
function isFirstArgString(
node: TSESTree.CallExpression,
): node is CallExpressionWithStringArgument {
return node.arguments[0] && isStringNode(node.arguments[0]);
}
const isTargetedTestCase = (
node: TSESTree.CallExpression,
): node is JestFunctionCallExpression<TestCaseName> =>
isTestCase(node) &&
(['it', 'test', 'it.skip', 'test.skip'] as Array<string | null>).includes(
getNodeName(node.callee),
);
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Suggest using `test.todo`',
recommended: false,
},
messages: {
todoOverEmpty: 'Prefer todo test case over empty test case',
todoOverUnimplemented:
'Prefer todo test case over unimplemented test case',
},
fixable: 'code',
schema: [],
type: 'layout',
},
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isTargetedTestCase(node) || !isFirstArgString(node)) {
return;
}
if (isTestBodyEmpty(node)) {
context.report({
messageId: 'todoOverEmpty',
node,
fix: fixer => [
fixer.removeRange([
node.arguments[0].range[1],
node.arguments[1].range[1],
]),
addTodo(node, fixer),
],
});
}
if (isOnlyTestTitle(node)) {
context.report({
messageId: 'todoOverUnimplemented',
node,
fix: fixer => [addTodo(node, fixer)],
});
}
},
};
},
});