Skip to content

Commit 74c9523

Browse files
committed
refactor(no-standalone-expect): improve code readability
1 parent bd6690b commit 74c9523

File tree

1 file changed

+32
-29
lines changed

1 file changed

+32
-29
lines changed

src/rules/no-standalone-expect.ts

+32-29
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,33 @@ import {
1414
} from './utils';
1515

1616
const getBlockType = (
17-
stmt: TSESTree.BlockStatement,
18-
): 'function' | DescribeAlias.describe | null => {
19-
const func = stmt.parent;
17+
statement: TSESTree.BlockStatement,
18+
): 'function' | 'describe' | null => {
19+
const func = statement.parent;
2020

2121
/* istanbul ignore if */
2222
if (!func) {
2323
throw new Error(
2424
`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
2525
);
2626
}
27+
2728
// functionDeclaration: function func() {}
2829
if (func.type === AST_NODE_TYPES.FunctionDeclaration) {
2930
return 'function';
3031
}
32+
3133
if (isFunction(func) && func.parent) {
3234
const expr = func.parent;
3335

34-
// arrowfunction or function expr
36+
// arrow function or function expr
3537
if (expr.type === AST_NODE_TYPES.VariableDeclarator) {
3638
return 'function';
3739
}
40+
3841
// if it's not a variable, it will be callExpr, we only care about describe
3942
if (expr.type === AST_NODE_TYPES.CallExpression && isDescribe(expr)) {
40-
return DescribeAlias.describe;
43+
return 'describe';
4144
}
4245
}
4346

@@ -52,12 +55,7 @@ const isEach = (node: TSESTree.CallExpression): boolean =>
5255
node.callee.callee.object.type === AST_NODE_TYPES.Identifier &&
5356
TestCaseName.hasOwnProperty(node.callee.callee.object.name);
5457

55-
type callStackEntry =
56-
| TestCaseName.test
57-
| 'function'
58-
| DescribeAlias.describe
59-
| 'arrowFunc'
60-
| 'template';
58+
type BlockType = 'test' | 'function' | 'describe' | 'arrow' | 'template';
6159

6260
export default createRule<
6361
[{ additionalTestBlockFunctions: string[] }],
@@ -88,13 +86,16 @@ export default createRule<
8886
},
8987
defaultOptions: [{ additionalTestBlockFunctions: [] }],
9088
create(context, [{ additionalTestBlockFunctions = [] }]) {
91-
const callStack: callStackEntry[] = [];
89+
const callStack: BlockType[] = [];
9290

9391
const isCustomTestBlockFunction = (
9492
node: TSESTree.CallExpression,
9593
): boolean =>
9694
additionalTestBlockFunctions.includes(getNodeName(node) || '');
9795

96+
const isTestBlock = (node: TSESTree.CallExpression): boolean =>
97+
isTestCase(node) || isCustomTestBlockFunction(node);
98+
9899
return {
99100
CallExpression(node) {
100101
if (isExpectCall(node)) {
@@ -106,9 +107,11 @@ export default createRule<
106107

107108
return;
108109
}
109-
if (isTestCase(node) || isCustomTestBlockFunction(node)) {
110-
callStack.push(TestCaseName.test);
110+
111+
if (isTestBlock(node)) {
112+
callStack.push('test');
111113
}
114+
112115
if (node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression) {
113116
callStack.push('template');
114117
}
@@ -117,37 +120,37 @@ export default createRule<
117120
const top = callStack[callStack.length - 1];
118121

119122
if (
120-
((((isTestCase(node) || isCustomTestBlockFunction(node)) &&
121-
node.callee.type !== AST_NODE_TYPES.MemberExpression) ||
122-
isEach(node)) &&
123-
top === TestCaseName.test) ||
124-
(node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
125-
top === 'template')
123+
(top === 'test' &&
124+
(isEach(node) ||
125+
(isTestBlock(node) &&
126+
node.callee.type !== AST_NODE_TYPES.MemberExpression))) ||
127+
(top === 'template' &&
128+
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression)
126129
) {
127130
callStack.pop();
128131
}
129132
},
130-
BlockStatement(stmt) {
131-
const blockType = getBlockType(stmt);
133+
134+
BlockStatement(statement) {
135+
const blockType = getBlockType(statement);
132136

133137
if (blockType) {
134138
callStack.push(blockType);
135139
}
136140
},
137-
'BlockStatement:exit'(stmt: TSESTree.BlockStatement) {
138-
const blockType = getBlockType(stmt);
139-
140-
if (blockType && blockType === callStack[callStack.length - 1]) {
141+
'BlockStatement:exit'(statement: TSESTree.BlockStatement) {
142+
if (callStack[callStack.length - 1] === getBlockType(statement)) {
141143
callStack.pop();
142144
}
143145
},
146+
144147
ArrowFunctionExpression(node) {
145-
if (node.parent && node.parent.type !== AST_NODE_TYPES.CallExpression) {
146-
callStack.push('arrowFunc');
148+
if (node.parent?.type !== AST_NODE_TYPES.CallExpression) {
149+
callStack.push('arrow');
147150
}
148151
},
149152
'ArrowFunctionExpression:exit'() {
150-
if (callStack[callStack.length - 1] === 'arrowFunc') {
153+
if (callStack[callStack.length - 1] === 'arrow') {
151154
callStack.pop();
152155
}
153156
},

0 commit comments

Comments
 (0)