Skip to content

Commit 0c44edf

Browse files
committed
feat(prefer-implicit-assert): use existing utils
1 parent 7feb82d commit 0c44edf

File tree

1 file changed

+40
-55
lines changed

1 file changed

+40
-55
lines changed

lib/rules/prefer-implicit-assert.ts

Lines changed: 40 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
import { createTestingLibraryRule } from '../create-testing-library-rule';
99
import { TestingLibrarySettings } from '../create-testing-library-rule/detect-testing-library-utils';
1010
import { isCallExpression, isMemberExpression } from '../node-utils';
11-
import { PRESENCE_MATCHERS, ABSENCE_MATCHERS } from '../utils';
1211

1312
export const RULE_NAME = 'prefer-implicit-assert';
1413
export type MessageIds = 'preferImplicitAssert';
@@ -39,54 +38,6 @@ const isCalledInExpect = (
3938
);
4039
};
4140

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-
9041
const reportError = (
9142
context: Readonly<
9243
TSESLint.RuleContext<'preferImplicitAssert', []> & {
@@ -143,31 +94,65 @@ export default createTestingLibraryRule<Options, MessageIds>({
14394
}
14495
},
14596
'Program:exit'() {
146-
let isAsyncQuery = true;
14797
findQueryCalls.forEach((queryCall) => {
98+
const isAsyncQuery = true;
14899
const node: TSESTree.Identifier | TSESTree.Node | undefined =
149100
isCalledUsingSomeObject(queryCall) ? queryCall.parent : queryCall;
150101

151102
if (node) {
152103
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+
) {
154110
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+
) {
156123
return reportError(context, node, 'findBy*');
124+
}
157125
}
158126
}
159127
});
160128

161129
getQueryCalls.forEach((queryCall) => {
162-
isAsyncQuery = false;
130+
const isAsyncQuery = false;
163131
const node: TSESTree.Identifier | TSESTree.Node | undefined =
164132
isCalledUsingSomeObject(queryCall) ? queryCall.parent : queryCall;
165133
if (node) {
166134
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+
) {
168141
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+
) {
170154
return reportError(context, node, 'getBy*');
155+
}
171156
}
172157
}
173158
});

0 commit comments

Comments
 (0)