-
Notifications
You must be signed in to change notification settings - Fork 230
/
max-nested-describe.ts
55 lines (52 loc) · 1.3 KB
/
max-nested-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
import type { TSESTree } from '@typescript-eslint/utils';
import { createRule, isTypeOfJestFnCall } from './utils';
export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Enforces a maximum depth to nested describe calls',
recommended: false,
},
messages: {
exceededMaxDepth:
'Too many nested describe calls ({{ depth }}). Maximum allowed is {{ max }}.',
},
type: 'suggestion',
schema: [
{
type: 'object',
properties: {
max: {
type: 'integer',
minimum: 0,
},
},
additionalProperties: false,
},
],
},
defaultOptions: [{ max: 5 }],
create(context, [{ max }]) {
const describes: TSESTree.CallExpression[] = [];
return {
CallExpression(node) {
if (isTypeOfJestFnCall(node, context, ['describe'])) {
describes.unshift(node);
if (describes.length > max) {
context.report({
node,
messageId: 'exceededMaxDepth',
data: { depth: describes.length, max },
});
}
}
},
'CallExpression:exit'(node) {
if (describes[0] === node) {
describes.shift();
}
},
};
},
});