Skip to content

Commit

Permalink
refactor(no-standalone-expect): improve code readability
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed May 16, 2020
1 parent d8d4071 commit 12de4cd
Showing 1 changed file with 37 additions and 28 deletions.
65 changes: 37 additions & 28 deletions src/rules/no-standalone-expect.ts
Expand Up @@ -14,31 +14,36 @@ import {
} from './utils';

const getBlockType = (
stmt: TSESTree.BlockStatement,
): 'function' | DescribeAlias.describe | null => {
const func = stmt.parent;
statement: TSESTree.BlockStatement,
): 'function' | 'describe' | null => {
const func = statement.parent;

/* istanbul ignore if */
if (!func) {
throw new Error(
`Unexpected BlockStatement. No parent defined. - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
);
}

// functionDeclaration: function func() {}
if (func.type === AST_NODE_TYPES.FunctionDeclaration) {
return 'function';
}

if (isFunction(func) && func.parent) {
const expr = func.parent;
// arrowfunction or function expr

// arrow function or function expr
if (expr.type === AST_NODE_TYPES.VariableDeclarator) {
return 'function';
}

// if it's not a variable, it will be callExpr, we only care about describe
if (expr.type === AST_NODE_TYPES.CallExpression && isDescribe(expr)) {
return DescribeAlias.describe;
return 'describe';
}
}

return null;
};

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

type callStackEntry =
| TestCaseName.test
| 'function'
| DescribeAlias.describe
| 'arrowFunc'
| 'template';
type BlockType = 'test' | 'function' | 'describe' | 'arrow' | 'template';

export default createRule<
[{ additionalTestBlockFunctions: string[] }],
Expand Down Expand Up @@ -86,25 +86,32 @@ export default createRule<
},
defaultOptions: [{ additionalTestBlockFunctions: [] }],
create(context, [{ additionalTestBlockFunctions = [] }]) {
const callStack: callStackEntry[] = [];
const callStack: BlockType[] = [];

const isCustomTestBlockFunction = (
node: TSESTree.CallExpression,
): boolean =>
additionalTestBlockFunctions.includes(getNodeName(node) || '');

const isTestBlock = (node: TSESTree.CallExpression): boolean =>
isTestCase(node) || isCustomTestBlockFunction(node);

return {
CallExpression(node) {
if (isExpectCall(node)) {
const parent = callStack[callStack.length - 1];

if (!parent || parent === DescribeAlias.describe) {
context.report({ node, messageId: 'unexpectedExpect' });
}

return;
}
if (isTestCase(node) || isCustomTestBlockFunction(node)) {
callStack.push(TestCaseName.test);

if (isTestBlock(node)) {
callStack.push('test');
}

if (node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression) {
callStack.push('template');
}
Expand All @@ -113,35 +120,37 @@ export default createRule<
const top = callStack[callStack.length - 1];

if (
((((isTestCase(node) || isCustomTestBlockFunction(node)) &&
node.callee.type !== AST_NODE_TYPES.MemberExpression) ||
isEach(node)) &&
top === TestCaseName.test) ||
(node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression &&
top === 'template')
(top === 'test' &&
(isEach(node) ||
(isTestBlock(node) &&
node.callee.type !== AST_NODE_TYPES.MemberExpression))) ||
(top === 'template' &&
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression)
) {
callStack.pop();
}
},
BlockStatement(stmt) {
const blockType = getBlockType(stmt);

BlockStatement(statement) {
const blockType = getBlockType(statement);

if (blockType) {
callStack.push(blockType);
}
},
'BlockStatement:exit'(stmt: TSESTree.BlockStatement) {
const blockType = getBlockType(stmt);
if (blockType && blockType === callStack[callStack.length - 1]) {
'BlockStatement:exit'(statement: TSESTree.BlockStatement) {
if (callStack[callStack.length - 1] === getBlockType(statement)) {
callStack.pop();
}
},

ArrowFunctionExpression(node) {
if (node.parent && node.parent.type !== AST_NODE_TYPES.CallExpression) {
callStack.push('arrowFunc');
if (node.parent?.type !== AST_NODE_TYPES.CallExpression) {
callStack.push('arrow');
}
},
'ArrowFunctionExpression:exit'() {
if (callStack[callStack.length - 1] === 'arrowFunc') {
if (callStack[callStack.length - 1] === 'arrow') {
callStack.pop();
}
},
Expand Down

0 comments on commit 12de4cd

Please sign in to comment.