From 6c3e0b9b6e954b363b90bcc928be73eb421ce6c7 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Sat, 16 May 2020 21:22:48 +1200 Subject: [PATCH] refactor(no-standalone-expect): improve code readability --- src/rules/no-standalone-expect.ts | 65 ++++++++++++++++++------------- 1 file changed, 37 insertions(+), 28 deletions(-) diff --git a/src/rules/no-standalone-expect.ts b/src/rules/no-standalone-expect.ts index 5d8bd4586..c09ca0480 100644 --- a/src/rules/no-standalone-expect.ts +++ b/src/rules/no-standalone-expect.ts @@ -14,9 +14,9 @@ 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) { @@ -24,21 +24,26 @@ const getBlockType = ( `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; }; @@ -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[] }], @@ -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'); } @@ -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(); } },