/
valid-describe.ts
124 lines (116 loc) · 3.57 KB
/
valid-describe.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
120
121
122
123
124
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import {
FunctionExpression,
JestFunctionCallExpression,
createRule,
isDescribe,
isFunction,
} from './tsUtils';
const isAsync = (node: FunctionExpression): boolean => node.async;
const isString = (node: TSESTree.Node): boolean =>
(node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string') ||
node.type === AST_NODE_TYPES.TemplateLiteral;
const hasParams = (node: FunctionExpression): boolean => node.params.length > 0;
const paramsLocation = (
params: TSESTree.Expression[] | TSESTree.Parameter[],
) => {
const [first] = params;
const last = params[params.length - 1];
return {
start: first.loc.start,
end: last.loc.end,
};
};
const isDescribeEach = (node: JestFunctionCallExpression) =>
node.callee.type === AST_NODE_TYPES.MemberExpression &&
node.callee.property.type === AST_NODE_TYPES.Identifier &&
node.callee.property.name === 'each';
export default createRule({
name: __filename,
meta: {
type: 'problem',
docs: {
description:
'Using an improper `describe()` callback function can lead to unexpected test errors.',
category: 'Possible Errors',
recommended: 'warn',
},
messages: {
nameAndCallback: 'Describe requires name and callback arguments',
firstArgumentMustBeName: 'First argument must be name',
secondArgumentMustBeFunction: 'Second argument must be function',
noAsyncDescribeCallback: 'No async describe callback',
unexpectedDescribeArgument: 'Unexpected argument(s) in describe callback',
unexpectedReturnInDescribe:
'Unexpected return statement in describe callback',
},
schema: [],
} as const,
defaultOptions: [],
create(context) {
return {
CallExpression(node) {
if (!isDescribe(node) || isDescribeEach(node)) {
return;
}
if (node.arguments.length === 0) {
return context.report({
messageId: 'nameAndCallback',
loc: node.loc,
});
}
const [name] = node.arguments;
const [, callbackFunction] = node.arguments;
if (!isString(name)) {
context.report({
messageId: 'firstArgumentMustBeName',
loc: paramsLocation(node.arguments),
});
}
if (!callbackFunction) {
context.report({
messageId: 'nameAndCallback',
loc: paramsLocation(node.arguments),
});
return;
}
if (isFunction(callbackFunction)) {
if (isAsync(callbackFunction)) {
context.report({
messageId: 'noAsyncDescribeCallback',
node: callbackFunction,
});
}
if (hasParams(callbackFunction)) {
context.report({
messageId: 'unexpectedDescribeArgument',
loc: paramsLocation(callbackFunction.params),
});
}
if (
callbackFunction.body &&
callbackFunction.body.type === AST_NODE_TYPES.BlockStatement
) {
callbackFunction.body.body.forEach(node => {
if (node.type === 'ReturnStatement') {
context.report({
messageId: 'unexpectedReturnInDescribe',
node,
});
}
});
}
} else {
context.report({
messageId: 'secondArgumentMustBeFunction',
loc: paramsLocation(node.arguments),
});
return;
}
},
};
},
});