|
1 |
| -import { ASTUtils, TSESTree } from '@typescript-eslint/experimental-utils'; |
2 |
| -import { ABSENCE_MATCHERS, PRESENCE_MATCHERS } from '../utils'; |
| 1 | +import { TSESTree } from '@typescript-eslint/experimental-utils'; |
3 | 2 | import { findClosestCallNode, isMemberExpression } from '../node-utils';
|
4 | 3 | import { createTestingLibraryRule } from '../create-testing-library-rule';
|
5 | 4 |
|
@@ -37,46 +36,27 @@ export default createTestingLibraryRule<Options, MessageIds>({
|
37 | 36 | return;
|
38 | 37 | }
|
39 | 38 |
|
| 39 | + // Sync queries (getBy and queryBy) are corresponding ones used |
| 40 | + // to check presence or absence. If none found, stop the rule. |
40 | 41 | if (!helpers.isSyncQuery(node)) {
|
41 | 42 | return;
|
42 | 43 | }
|
43 | 44 |
|
44 | 45 | const isPresenceQuery = helpers.isGetByQuery(node);
|
45 | 46 | const expectStatement = expectCallNode.parent;
|
46 |
| - let matcher = |
47 |
| - ASTUtils.isIdentifier(expectStatement.property) && |
48 |
| - expectStatement.property.name; |
49 |
| - let isNegatedMatcher = false; |
| 47 | + const isPresenceAssert = helpers.isPresenceAssert(expectStatement); |
| 48 | + const isAbsenceAssert = helpers.isAbsenceAssert(expectStatement); |
50 | 49 |
|
51 |
| - if ( |
52 |
| - matcher === 'not' && |
53 |
| - isMemberExpression(expectStatement.parent) && |
54 |
| - ASTUtils.isIdentifier(expectStatement.parent.property) |
55 |
| - ) { |
56 |
| - isNegatedMatcher = true; |
57 |
| - matcher = expectStatement.parent.property.name; |
| 50 | + if (!isPresenceAssert && !isAbsenceAssert) { |
| 51 | + return; |
58 | 52 | }
|
59 | 53 |
|
60 |
| - const validMatchers = isPresenceQuery |
61 |
| - ? PRESENCE_MATCHERS |
62 |
| - : ABSENCE_MATCHERS; |
63 |
| - |
64 |
| - const invalidMatchers = isPresenceQuery |
65 |
| - ? ABSENCE_MATCHERS |
66 |
| - : PRESENCE_MATCHERS; |
67 |
| - |
68 |
| - const messageId = isPresenceQuery |
69 |
| - ? 'wrongAbsenceQuery' |
70 |
| - : 'wrongPresenceQuery'; |
| 54 | + if (isPresenceAssert && !isPresenceQuery) { |
| 55 | + return context.report({ node, messageId: 'wrongPresenceQuery' }); |
| 56 | + } |
71 | 57 |
|
72 |
| - if ( |
73 |
| - (!isNegatedMatcher && invalidMatchers.includes(matcher)) || |
74 |
| - (isNegatedMatcher && validMatchers.includes(matcher)) |
75 |
| - ) { |
76 |
| - context.report({ |
77 |
| - node, |
78 |
| - messageId, |
79 |
| - }); |
| 58 | + if (isAbsenceAssert && isPresenceQuery) { |
| 59 | + return context.report({ node, messageId: 'wrongAbsenceQuery' }); |
80 | 60 | }
|
81 | 61 | },
|
82 | 62 | };
|
|
0 commit comments