Skip to content

Commit 6c3e0b9

Browse files
committed
refactor(no-standalone-expect): improve code readability
1 parent c4f907b commit 6c3e0b9

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

src/rules/no-standalone-expect.ts

+37-28
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,36 @@ 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;
33-
// arrowfunction or function expr
35+
36+
// arrow function or function expr
3437
if (expr.type === AST_NODE_TYPES.VariableDeclarator) {
3538
return 'function';
3639
}
40+
3741
// if it's not a variable, it will be callExpr, we only care about describe
3842
if (expr.type === AST_NODE_TYPES.CallExpression && isDescribe(expr)) {
39-
return DescribeAlias.describe;
43+
return 'describe';
4044
}
4145
}
46+
4247
return null;
4348
};
4449

@@ -50,12 +55,7 @@ const isEach = (node: TSESTree.CallExpression): boolean =>
5055
node.callee.callee.object.type === AST_NODE_TYPES.Identifier &&
5156
TestCaseName.hasOwnProperty(node.callee.callee.object.name);
5257

53-
type callStackEntry =
54-
| TestCaseName.test
55-
| 'function'
56-
| DescribeAlias.describe
57-
| 'arrowFunc'
58-
| 'template';
58+
type BlockType = 'test' | 'function' | 'describe' | 'arrow' | 'template';
5959

6060
export default createRule<
6161
[{ additionalTestBlockFunctions: string[] }],
@@ -86,25 +86,32 @@ export default createRule<
8686
},
8787
defaultOptions: [{ additionalTestBlockFunctions: [] }],
8888
create(context, [{ additionalTestBlockFunctions = [] }]) {
89-
const callStack: callStackEntry[] = [];
89+
const callStack: BlockType[] = [];
9090

9191
const isCustomTestBlockFunction = (
9292
node: TSESTree.CallExpression,
9393
): boolean =>
9494
additionalTestBlockFunctions.includes(getNodeName(node) || '');
9595

96+
const isTestBlock = (node: TSESTree.CallExpression): boolean =>
97+
isTestCase(node) || isCustomTestBlockFunction(node);
98+
9699
return {
97100
CallExpression(node) {
98101
if (isExpectCall(node)) {
99102
const parent = callStack[callStack.length - 1];
103+
100104
if (!parent || parent === DescribeAlias.describe) {
101105
context.report({ node, messageId: 'unexpectedExpect' });
102106
}
107+
103108
return;
104109
}
105-
if (isTestCase(node) || isCustomTestBlockFunction(node)) {
106-
callStack.push(TestCaseName.test);
110+
111+
if (isTestBlock(node)) {
112+
callStack.push('test');
107113
}
114+
108115
if (node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression) {
109116
callStack.push('template');
110117
}
@@ -113,35 +120,37 @@ export default createRule<
113120
const top = callStack[callStack.length - 1];
114121

115122
if (
116-
((((isTestCase(node) || isCustomTestBlockFunction(node)) &&
117-
node.callee.type !== AST_NODE_TYPES.MemberExpression) ||
118-
isEach(node)) &&
119-
top === TestCaseName.test) ||
120-
(node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
121-
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)
122129
) {
123130
callStack.pop();
124131
}
125132
},
126-
BlockStatement(stmt) {
127-
const blockType = getBlockType(stmt);
133+
134+
BlockStatement(statement) {
135+
const blockType = getBlockType(statement);
136+
128137
if (blockType) {
129138
callStack.push(blockType);
130139
}
131140
},
132-
'BlockStatement:exit'(stmt: TSESTree.BlockStatement) {
133-
const blockType = getBlockType(stmt);
134-
if (blockType && blockType === callStack[callStack.length - 1]) {
141+
'BlockStatement:exit'(statement: TSESTree.BlockStatement) {
142+
if (callStack[callStack.length - 1] === getBlockType(statement)) {
135143
callStack.pop();
136144
}
137145
},
146+
138147
ArrowFunctionExpression(node) {
139-
if (node.parent && node.parent.type !== AST_NODE_TYPES.CallExpression) {
140-
callStack.push('arrowFunc');
148+
if (node.parent?.type !== AST_NODE_TYPES.CallExpression) {
149+
callStack.push('arrow');
141150
}
142151
},
143152
'ArrowFunctionExpression:exit'() {
144-
if (callStack[callStack.length - 1] === 'arrowFunc') {
153+
if (callStack[callStack.length - 1] === 'arrow') {
145154
callStack.pop();
146155
}
147156
},

0 commit comments

Comments
 (0)