Skip to content

Commit c37dce9

Browse files
committed
perf: optimize no-standalone-expect & prefer-expect-assertions
1 parent c0fadf6 commit c37dce9

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

src/rules/no-standalone-expect.ts

+7-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
getNodeName,
66
isFunction,
77
isTypeOfJestFnCall,
8+
parseJestFnCall,
89
} from './utils';
910

1011
const getBlockType = (
@@ -83,13 +84,11 @@ export default createRule<
8384
): boolean =>
8485
additionalTestBlockFunctions.includes(getNodeName(node) || '');
8586

86-
const isTestBlock = (node: TSESTree.CallExpression): boolean =>
87-
isTypeOfJestFnCall(node, context, ['test']) ||
88-
isCustomTestBlockFunction(node);
89-
9087
return {
9188
CallExpression(node) {
92-
if (isTypeOfJestFnCall(node, context, ['expect'])) {
89+
const jestFnCall = parseJestFnCall(node, context);
90+
91+
if (jestFnCall?.type === 'expect') {
9392
const parent = callStack[callStack.length - 1];
9493

9594
if (!parent || parent === DescribeAlias.describe) {
@@ -99,7 +98,7 @@ export default createRule<
9998
return;
10099
}
101100

102-
if (isTestBlock(node)) {
101+
if (jestFnCall?.type === 'test' || isCustomTestBlockFunction(node)) {
103102
callStack.push('test');
104103
}
105104

@@ -112,7 +111,8 @@ export default createRule<
112111

113112
if (
114113
(top === 'test' &&
115-
isTestBlock(node) &&
114+
(isTypeOfJestFnCall(node, context, ['test']) ||
115+
isCustomTestBlockFunction(node)) &&
116116
node.callee.type !== AST_NODE_TYPES.MemberExpression) ||
117117
(top === 'template' &&
118118
node.callee.type === AST_NODE_TYPES.TaggedTemplateExpression)

src/rules/prefer-expect-assertions.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
isFunction,
88
isSupportedAccessor,
99
isTypeOfJestFnCall,
10+
parseJestFnCall,
1011
} from './utils';
1112

1213
const isExpectAssertionsOrHasAssertionsCall = (
@@ -156,13 +157,15 @@ export default createRule<[RuleOptions], MessageIds>({
156157
ForOfStatement: enterForLoop,
157158
'ForOfStatement:exit': exitForLoop,
158159
CallExpression(node) {
159-
if (isTypeOfJestFnCall(node, context, ['test'])) {
160+
const jestFnCall = parseJestFnCall(node, context);
161+
162+
if (jestFnCall?.type === 'test') {
160163
inTestCaseCall = true;
161164

162165
return;
163166
}
164167

165-
if (isTypeOfJestFnCall(node, context, ['expect']) && inTestCaseCall) {
168+
if (jestFnCall?.type === 'expect' && inTestCaseCall) {
166169
if (inForLoop) {
167170
hasExpectInLoop = true;
168171
}

0 commit comments

Comments
 (0)