/
max-nested-describe.ts
77 lines (69 loc) · 1.88 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import { createRule, isDescribeCall } 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 describeCallbackStack: number[] = [];
function pushDescribeCallback(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
) {
const { parent } = node;
if (
parent?.type !== AST_NODE_TYPES.CallExpression ||
!isDescribeCall(parent)
) {
return;
}
describeCallbackStack.push(0);
if (describeCallbackStack.length > max) {
context.report({
node: parent,
messageId: 'exceededMaxDepth',
data: { depth: describeCallbackStack.length, max },
});
}
}
function popDescribeCallback(
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
) {
const { parent } = node;
if (
parent?.type === AST_NODE_TYPES.CallExpression &&
isDescribeCall(parent)
) {
describeCallbackStack.pop();
}
}
return {
FunctionExpression: pushDescribeCallback,
'FunctionExpression:exit': popDescribeCallback,
ArrowFunctionExpression: pushDescribeCallback,
'ArrowFunctionExpression:exit': popDescribeCallback,
};
},
});