From 4d6398ebff0df4d048997062778332ec9b5ae238 Mon Sep 17 00:00:00 2001 From: Gareth Jones Date: Mon, 25 Jul 2022 07:20:19 +1200 Subject: [PATCH] perf: optimize `no-standalone-expect` & `prefer-expect-assertions` --- src/rules/no-standalone-expect.ts | 14 +++++++------- src/rules/prefer-expect-assertions.ts | 7 +++++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/rules/no-standalone-expect.ts b/src/rules/no-standalone-expect.ts index eae687759..6e4b6b0d2 100644 --- a/src/rules/no-standalone-expect.ts +++ b/src/rules/no-standalone-expect.ts @@ -5,6 +5,7 @@ import { getNodeName, isFunction, isTypeOfJestFnCall, + parseJestFnCall, } from './utils'; const getBlockType = ( @@ -83,13 +84,11 @@ export default createRule< ): boolean => additionalTestBlockFunctions.includes(getNodeName(node) || ''); - const isTestBlock = (node: TSESTree.CallExpression): boolean => - isTypeOfJestFnCall(node, context, ['test']) || - isCustomTestBlockFunction(node); - return { CallExpression(node) { - if (isTypeOfJestFnCall(node, context, ['expect'])) { + const { type: jestFnCallType } = parseJestFnCall(node, context) ?? {}; + + if (jestFnCallType === 'expect') { const parent = callStack[callStack.length - 1]; if (!parent || parent === DescribeAlias.describe) { @@ -99,7 +98,7 @@ export default createRule< return; } - if (isTestBlock(node)) { + if (jestFnCallType === 'test' || isCustomTestBlockFunction(node)) { callStack.push('test'); } @@ -112,7 +111,8 @@ export default createRule< if ( (top === 'test' && - isTestBlock(node) && + (isTypeOfJestFnCall(node, context, ['test']) || + isCustomTestBlockFunction(node)) && node.callee.type !== AST_NODE_TYPES.MemberExpression) || (top === 'template' && node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression) diff --git a/src/rules/prefer-expect-assertions.ts b/src/rules/prefer-expect-assertions.ts index 4d20c1428..4252d056e 100644 --- a/src/rules/prefer-expect-assertions.ts +++ b/src/rules/prefer-expect-assertions.ts @@ -7,6 +7,7 @@ import { isFunction, isSupportedAccessor, isTypeOfJestFnCall, + parseJestFnCall, } from './utils'; const isExpectAssertionsOrHasAssertionsCall = ( @@ -156,13 +157,15 @@ export default createRule<[RuleOptions], MessageIds>({ ForOfStatement: enterForLoop, 'ForOfStatement:exit': exitForLoop, CallExpression(node) { - if (isTypeOfJestFnCall(node, context, ['test'])) { + const jestFnCall = parseJestFnCall(node, context); + + if (jestFnCall?.type === 'test') { inTestCaseCall = true; return; } - if (isTypeOfJestFnCall(node, context, ['expect']) && inTestCaseCall) { + if (jestFnCall?.type === 'expect' && inTestCaseCall) { if (inForLoop) { hasExpectInLoop = true; }