Skip to content

Commit c0fadf6

Browse files
committed
refactor: optimize some code for full coverage & cleanup wip comments
1 parent e0dfb74 commit c0fadf6

File tree

2 files changed

+38
-53
lines changed

2 files changed

+38
-53
lines changed

src/rules/__tests__/valid-expect.test.ts

+30
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,36 @@ ruleTester.run('valid-expect', rule, {
355355
},
356356
],
357357
},
358+
{
359+
code: 'expect(true).not.resolves.toBeDefined();',
360+
errors: [
361+
{
362+
endColumn: 40,
363+
column: 1,
364+
messageId: 'modifierUnknown',
365+
},
366+
],
367+
},
368+
{
369+
code: 'expect(true).not.not.toBeDefined();',
370+
errors: [
371+
{
372+
endColumn: 35,
373+
column: 1,
374+
messageId: 'modifierUnknown',
375+
},
376+
],
377+
},
378+
{
379+
code: 'expect(true).resolves.not.exactly.toBeDefined();',
380+
errors: [
381+
{
382+
endColumn: 48,
383+
column: 1,
384+
messageId: 'modifierUnknown',
385+
},
386+
],
387+
},
358388
{
359389
code: 'expect(true).resolves;',
360390
errors: [

src/rules/utils/parseJestFnCall.ts

+8-53
Original file line numberDiff line numberDiff line change
@@ -263,58 +263,17 @@ export const parseJestFnCallWithReason = (
263263

264264
const type = determineJestFnType(name);
265265

266-
// if (
267-
// node.parent?.type === AST_NODE_TYPES.CallExpression
268-
// // node.parent?.type === AST_NODE_TYPES.MemberExpression
269-
// ) {
270-
// return null;
271-
// }
272-
273266
if (type === 'expect') {
274-
// const findTopMostMemberExpression = (
275-
// node: TSESTree.MemberExpression,
276-
// ): TSESTree.MemberExpression => {
277-
// let topMostMemberExpression = node;
278-
// let { parent } = node;
279-
//
280-
// while (parent) {
281-
// if (parent.type !== AST_NODE_TYPES.MemberExpression) {
282-
// break;
283-
// }
284-
//
285-
// topMostMemberExpression = parent;
286-
// parent = parent.parent;
287-
// }
288-
//
289-
// return topMostMemberExpression;
290-
// };
291267
const result = parseJestExpectCall(parsedJestFnCall);
292268

293-
const topMost = findTopMostCallExpression(node);
294-
295-
// if (topMost !== node) {
296-
// return null;
297-
// }
298-
299-
// console.log(
300-
// typeof result,
301-
// 'topmost',
302-
// topMost === node,
303-
// parsedJestFnCall.members.length,
304-
// );
305-
if (typeof result === 'string' && topMost !== node) {
269+
// if the `expect` call chain is not valid, only report on the topmost node
270+
// since all members in the chain are likely to get flagged for some reason
271+
if (
272+
typeof result === 'string' &&
273+
findTopMostCallExpression(node) !== node
274+
) {
306275
return null;
307276
}
308-
// console.log(result);
309-
310-
// if (typeof result !== 'string') {
311-
// if (
312-
// node.parent?.type === AST_NODE_TYPES.CallExpression ||
313-
// node.parent?.type === AST_NODE_TYPES.MemberExpression
314-
// ) {
315-
// return null;
316-
// }
317-
// }
318277

319278
if (result === 'matcher-not-found') {
320279
if (node.parent?.type === AST_NODE_TYPES.MemberExpression) {
@@ -361,10 +320,6 @@ const findModifiersAndMatcher = (
361320
): ModifiersAndMatcher | string => {
362321
const modifiers: KnownMemberExpressionProperty[] = [];
363322

364-
if (members.length === 0) {
365-
return 'matcher-not-found';
366-
}
367-
368323
for (const member of members) {
369324
// check if the member is being called, which means it is the matcher
370325
// (and also the end of the entire "expect" call chain)
@@ -377,7 +332,6 @@ const findModifiersAndMatcher = (
377332
matcherArgs: member.parent.parent.arguments,
378333
modifiers,
379334
};
380-
// return 'matcher-not-called';
381335
}
382336

383337
// otherwise, it should be a modifier
@@ -410,7 +364,8 @@ const findModifiersAndMatcher = (
410364
modifiers.push(member);
411365
}
412366

413-
return 'matcher-not-called';
367+
// this will only really happen if there are no members
368+
return 'matcher-not-found';
414369
};
415370

416371
const parseJestExpectCall = (

0 commit comments

Comments
 (0)