|
8 | 8 | import { createTestingLibraryRule } from '../create-testing-library-rule';
|
9 | 9 | import { TestingLibrarySettings } from '../create-testing-library-rule/detect-testing-library-utils';
|
10 | 10 | import { isCallExpression, isMemberExpression } from '../node-utils';
|
11 |
| -import { PRESENCE_MATCHERS, ABSENCE_MATCHERS } from '../utils'; |
12 | 11 |
|
13 | 12 | export const RULE_NAME = 'prefer-implicit-assert';
|
14 | 13 | export type MessageIds = 'preferImplicitAssert';
|
@@ -39,54 +38,6 @@ const isCalledInExpect = (
|
39 | 38 | );
|
40 | 39 | };
|
41 | 40 |
|
42 |
| -const usesPresenceAssertion = ( |
43 |
| - node: TSESTree.Identifier | TSESTree.Node, |
44 |
| - isAsyncQuery: boolean |
45 |
| -) => { |
46 |
| - if (isAsyncQuery) { |
47 |
| - return ( |
48 |
| - isMemberExpression(node.parent?.parent?.parent?.parent) && |
49 |
| - node.parent?.parent?.parent?.parent.property.type === |
50 |
| - AST_NODE_TYPES.Identifier && |
51 |
| - PRESENCE_MATCHERS.includes(node.parent.parent.parent.parent.property.name) |
52 |
| - ); |
53 |
| - } |
54 |
| - return ( |
55 |
| - isMemberExpression(node.parent?.parent?.parent) && |
56 |
| - node.parent?.parent?.parent.property.type === AST_NODE_TYPES.Identifier && |
57 |
| - PRESENCE_MATCHERS.includes(node.parent.parent.parent.property.name) |
58 |
| - ); |
59 |
| -}; |
60 |
| - |
61 |
| -const usesNotPresenceAssertion = ( |
62 |
| - node: TSESTree.Identifier | TSESTree.Node, |
63 |
| - isAsyncQuery: boolean |
64 |
| -) => { |
65 |
| - if (isAsyncQuery) { |
66 |
| - return ( |
67 |
| - isMemberExpression(node.parent?.parent?.parent?.parent) && |
68 |
| - node.parent?.parent?.parent?.parent.property.type === |
69 |
| - AST_NODE_TYPES.Identifier && |
70 |
| - node.parent.parent.parent.parent.property.name === 'not' && |
71 |
| - isMemberExpression(node.parent.parent.parent.parent.parent) && |
72 |
| - node.parent.parent.parent.parent.parent.property.type === |
73 |
| - AST_NODE_TYPES.Identifier && |
74 |
| - ABSENCE_MATCHERS.includes( |
75 |
| - node.parent.parent.parent.parent.parent.property.name |
76 |
| - ) |
77 |
| - ); |
78 |
| - } |
79 |
| - return ( |
80 |
| - isMemberExpression(node.parent?.parent?.parent) && |
81 |
| - node.parent?.parent?.parent.property.type === AST_NODE_TYPES.Identifier && |
82 |
| - node.parent.parent.parent.property.name === 'not' && |
83 |
| - isMemberExpression(node.parent.parent.parent.parent) && |
84 |
| - node.parent.parent.parent.parent.property.type === |
85 |
| - AST_NODE_TYPES.Identifier && |
86 |
| - ABSENCE_MATCHERS.includes(node.parent.parent.parent.parent.property.name) |
87 |
| - ); |
88 |
| -}; |
89 |
| - |
90 | 41 | const reportError = (
|
91 | 42 | context: Readonly<
|
92 | 43 | TSESLint.RuleContext<'preferImplicitAssert', []> & {
|
@@ -143,31 +94,65 @@ export default createTestingLibraryRule<Options, MessageIds>({
|
143 | 94 | }
|
144 | 95 | },
|
145 | 96 | 'Program:exit'() {
|
146 |
| - let isAsyncQuery = true; |
147 | 97 | findQueryCalls.forEach((queryCall) => {
|
| 98 | + const isAsyncQuery = true; |
148 | 99 | const node: TSESTree.Identifier | TSESTree.Node | undefined =
|
149 | 100 | isCalledUsingSomeObject(queryCall) ? queryCall.parent : queryCall;
|
150 | 101 |
|
151 | 102 | if (node) {
|
152 | 103 | if (isCalledInExpect(node, isAsyncQuery)) {
|
153 |
| - if (usesPresenceAssertion(node, isAsyncQuery)) |
| 104 | + if ( |
| 105 | + isMemberExpression(node.parent?.parent?.parent?.parent) && |
| 106 | + node.parent?.parent?.parent?.parent.property.type === |
| 107 | + AST_NODE_TYPES.Identifier && |
| 108 | + helpers.isPresenceAssert(node.parent.parent.parent.parent) |
| 109 | + ) { |
154 | 110 | return reportError(context, node, 'findBy*');
|
155 |
| - if (usesNotPresenceAssertion(node, isAsyncQuery)) |
| 111 | + } |
| 112 | + |
| 113 | + if ( |
| 114 | + isMemberExpression(node.parent?.parent?.parent?.parent) && |
| 115 | + node.parent?.parent?.parent?.parent.property.type === |
| 116 | + AST_NODE_TYPES.Identifier && |
| 117 | + node.parent.parent.parent.parent.property.name === 'not' && |
| 118 | + isMemberExpression(node.parent.parent.parent.parent.parent) && |
| 119 | + node.parent.parent.parent.parent.parent.property.type === |
| 120 | + AST_NODE_TYPES.Identifier && |
| 121 | + helpers.isAbsenceAssert(node.parent.parent.parent.parent.parent) |
| 122 | + ) { |
156 | 123 | return reportError(context, node, 'findBy*');
|
| 124 | + } |
157 | 125 | }
|
158 | 126 | }
|
159 | 127 | });
|
160 | 128 |
|
161 | 129 | getQueryCalls.forEach((queryCall) => {
|
162 |
| - isAsyncQuery = false; |
| 130 | + const isAsyncQuery = false; |
163 | 131 | const node: TSESTree.Identifier | TSESTree.Node | undefined =
|
164 | 132 | isCalledUsingSomeObject(queryCall) ? queryCall.parent : queryCall;
|
165 | 133 | if (node) {
|
166 | 134 | if (isCalledInExpect(node, isAsyncQuery)) {
|
167 |
| - if (usesPresenceAssertion(node, isAsyncQuery)) |
| 135 | + if ( |
| 136 | + isMemberExpression(node.parent?.parent?.parent) && |
| 137 | + node.parent?.parent?.parent.property.type === |
| 138 | + AST_NODE_TYPES.Identifier && |
| 139 | + helpers.isPresenceAssert(node.parent.parent.parent) |
| 140 | + ) { |
168 | 141 | return reportError(context, node, 'getBy*');
|
169 |
| - if (usesNotPresenceAssertion(node, isAsyncQuery)) |
| 142 | + } |
| 143 | + |
| 144 | + if ( |
| 145 | + isMemberExpression(node.parent?.parent?.parent) && |
| 146 | + node.parent?.parent?.parent.property.type === |
| 147 | + AST_NODE_TYPES.Identifier && |
| 148 | + node.parent.parent.parent.property.name === 'not' && |
| 149 | + isMemberExpression(node.parent.parent.parent.parent) && |
| 150 | + node.parent.parent.parent.parent.property.type === |
| 151 | + AST_NODE_TYPES.Identifier && |
| 152 | + helpers.isAbsenceAssert(node.parent.parent.parent.parent) |
| 153 | + ) { |
170 | 154 | return reportError(context, node, 'getBy*');
|
| 155 | + } |
171 | 156 | }
|
172 | 157 | }
|
173 | 158 | });
|
|
0 commit comments