|
1 |
| -import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils'; |
| 1 | +import type { TSESTree } from '@typescript-eslint/utils'; |
2 | 2 | import { createRule, isTypeOfJestFnCall } from './utils';
|
3 | 3 |
|
4 | 4 | export default createRule({
|
@@ -29,49 +29,27 @@ export default createRule({
|
29 | 29 | },
|
30 | 30 | defaultOptions: [{ max: 5 }],
|
31 | 31 | 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[] = []; |
69 | 33 |
|
70 | 34 | 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 | + }, |
75 | 53 | };
|
76 | 54 | },
|
77 | 55 | });
|
0 commit comments