Skip to content

Commit

Permalink
refactor(max-nested-describe): simplify implementation (#1442)
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath authored Sep 29, 2023
1 parent c846f7f commit 1ee0087
Showing 1 changed file with 20 additions and 42 deletions.
62 changes: 20 additions & 42 deletions src/rules/max-nested-describe.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import type { TSESTree } from '@typescript-eslint/utils';
import { createRule, isTypeOfJestFnCall } from './utils';

export default createRule({
Expand Down Expand Up @@ -29,49 +29,27 @@ export default createRule({
},
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 ||
!isTypeOfJestFnCall(parent, context, ['describe'])
) {
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 &&
isTypeOfJestFnCall(parent, context, ['describe'])
) {
describeCallbackStack.pop();
}
}
const describes: TSESTree.CallExpression[] = [];

return {
FunctionExpression: pushDescribeCallback,
'FunctionExpression:exit': popDescribeCallback,
ArrowFunctionExpression: pushDescribeCallback,
'ArrowFunctionExpression:exit': popDescribeCallback,
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();
}
},
};
},
});

0 comments on commit 1ee0087

Please sign in to comment.