Skip to content

Commit 1ee0087

Browse files
authored
refactor(max-nested-describe): simplify implementation (#1442)
1 parent c846f7f commit 1ee0087

File tree

1 file changed

+20
-42
lines changed

1 file changed

+20
-42
lines changed

src/rules/max-nested-describe.ts

Lines changed: 20 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
1+
import type { TSESTree } from '@typescript-eslint/utils';
22
import { createRule, isTypeOfJestFnCall } from './utils';
33

44
export default createRule({
@@ -29,49 +29,27 @@ export default createRule({
2929
},
3030
defaultOptions: [{ max: 5 }],
3131
create(context, [{ max }]) {
32-
const describeCallbackStack: number[] = [];
33-
34-
function pushDescribeCallback(
35-
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
36-
) {
37-
const { parent } = node;
38-
39-
if (
40-
parent?.type !== AST_NODE_TYPES.CallExpression ||
41-
!isTypeOfJestFnCall(parent, context, ['describe'])
42-
) {
43-
return;
44-
}
45-
46-
describeCallbackStack.push(0);
47-
48-
if (describeCallbackStack.length > max) {
49-
context.report({
50-
node: parent,
51-
messageId: 'exceededMaxDepth',
52-
data: { depth: describeCallbackStack.length, max },
53-
});
54-
}
55-
}
56-
57-
function popDescribeCallback(
58-
node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression,
59-
) {
60-
const { parent } = node;
61-
62-
if (
63-
parent?.type === AST_NODE_TYPES.CallExpression &&
64-
isTypeOfJestFnCall(parent, context, ['describe'])
65-
) {
66-
describeCallbackStack.pop();
67-
}
68-
}
32+
const describes: TSESTree.CallExpression[] = [];
6933

7034
return {
71-
FunctionExpression: pushDescribeCallback,
72-
'FunctionExpression:exit': popDescribeCallback,
73-
ArrowFunctionExpression: pushDescribeCallback,
74-
'ArrowFunctionExpression:exit': popDescribeCallback,
35+
CallExpression(node) {
36+
if (isTypeOfJestFnCall(node, context, ['describe'])) {
37+
describes.unshift(node);
38+
39+
if (describes.length > max) {
40+
context.report({
41+
node,
42+
messageId: 'exceededMaxDepth',
43+
data: { depth: describes.length, max },
44+
});
45+
}
46+
}
47+
},
48+
'CallExpression:exit'(node) {
49+
if (describes[0] === node) {
50+
describes.shift();
51+
}
52+
},
7553
};
7654
},
7755
});

0 commit comments

Comments
 (0)