Skip to content

Commit

Permalink
perf: optimize no-standalone-expect & prefer-expect-assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Jul 25, 2022
1 parent c0fadf6 commit 4d6398e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 7 additions & 7 deletions src/rules/no-standalone-expect.ts
Expand Up @@ -5,6 +5,7 @@ import {
getNodeName,
isFunction,
isTypeOfJestFnCall,
parseJestFnCall,
} from './utils';

const getBlockType = (
Expand Down Expand Up @@ -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) {
Expand All @@ -99,7 +98,7 @@ export default createRule<
return;
}

if (isTestBlock(node)) {
if (jestFnCallType === 'test' || isCustomTestBlockFunction(node)) {
callStack.push('test');
}

Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions src/rules/prefer-expect-assertions.ts
Expand Up @@ -7,6 +7,7 @@ import {
isFunction,
isSupportedAccessor,
isTypeOfJestFnCall,
parseJestFnCall,
} from './utils';

const isExpectAssertionsOrHasAssertionsCall = (
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 4d6398e

Please sign in to comment.