@@ -50,7 +50,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
50
50
return helpers . isAsyncUtil ( identifierNode , [ 'waitForElementToBeRemoved' ] ) ;
51
51
}
52
52
53
- function isReportableExpression ( node : TSESTree . LeftHandSideExpression ) {
53
+ /**
54
+ * Checks if node is reportable (starts with "get" or "find") and if it is, reports it with `context.report()`.
55
+ *
56
+ * @param {TSESTree.LeftHandSideExpression } node - Node to be tested
57
+ * @returns {Boolean } Boolean indicating if expression was reported
58
+ */
59
+ function reportExpression ( node : TSESTree . LeftHandSideExpression ) : boolean {
54
60
const argumentProperty = isMemberExpression ( node )
55
61
? getPropertyIdentifierNode ( node . property )
56
62
: getPropertyIdentifierNode ( node ) ;
@@ -59,13 +65,20 @@ export default createTestingLibraryRule<Options, MessageIds>({
59
65
return false ;
60
66
}
61
67
62
- return (
68
+ if (
63
69
helpers . isGetQueryVariant ( argumentProperty ) ||
64
70
helpers . isFindQueryVariant ( argumentProperty )
65
- ) ;
71
+ ) {
72
+ context . report ( {
73
+ node : argumentProperty ,
74
+ messageId : 'preferQueryByDisappearance' ,
75
+ } ) ;
76
+ return true ;
77
+ }
78
+ return false ;
66
79
}
67
80
68
- function isNonCallbackViolation ( node : TSESTree . CallExpressionArgument ) {
81
+ function checkNonCallbackViolation ( node : TSESTree . CallExpressionArgument ) {
69
82
if ( ! isCallExpression ( node ) ) {
70
83
return false ;
71
84
}
@@ -77,15 +90,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
77
90
return false ;
78
91
}
79
92
80
- return isReportableExpression ( node . callee ) ;
93
+ return reportExpression ( node . callee ) ;
81
94
}
82
95
83
96
function isReturnViolation ( node : TSESTree . Statement ) {
84
97
if ( ! isReturnStatement ( node ) || ! isCallExpression ( node . argument ) ) {
85
98
return false ;
86
99
}
87
100
88
- return isReportableExpression ( node . argument . callee ) ;
101
+ return reportExpression ( node . argument . callee ) ;
89
102
}
90
103
91
104
function isNonReturnViolation ( node : TSESTree . Statement ) {
@@ -100,14 +113,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
100
113
return false ;
101
114
}
102
115
103
- return isReportableExpression ( node . expression . callee ) ;
116
+ return reportExpression ( node . expression . callee ) ;
104
117
}
105
118
106
119
function isStatementViolation ( statement : TSESTree . Statement ) {
107
120
return isReturnViolation ( statement ) || isNonReturnViolation ( statement ) ;
108
121
}
109
122
110
- function isFunctionExpressionViolation (
123
+ function checkFunctionExpressionViolation (
111
124
node : TSESTree . CallExpressionArgument
112
125
) {
113
126
if ( ! isFunctionExpression ( node ) ) {
@@ -145,10 +158,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
145
158
return false ;
146
159
}
147
160
148
- return isReportableExpression ( node . body . callee ) ;
161
+ return reportExpression ( node . body . callee ) ;
149
162
}
150
163
151
- function isArrowFunctionViolation ( node : TSESTree . CallExpressionArgument ) {
164
+ function checkArrowFunctionViolation (
165
+ node : TSESTree . CallExpressionArgument
166
+ ) {
152
167
return (
153
168
isArrowFunctionBodyViolation ( node ) ||
154
169
isArrowFunctionImplicitReturnViolation ( node )
@@ -162,18 +177,9 @@ export default createTestingLibraryRule<Options, MessageIds>({
162
177
163
178
const argumentNode = node . arguments [ 0 ] ;
164
179
165
- if (
166
- ! isNonCallbackViolation ( argumentNode ) &&
167
- ! isArrowFunctionViolation ( argumentNode ) &&
168
- ! isFunctionExpressionViolation ( argumentNode )
169
- ) {
170
- return ;
171
- }
172
-
173
- context . report ( {
174
- node : argumentNode ,
175
- messageId : 'preferQueryByDisappearance' ,
176
- } ) ;
180
+ checkNonCallbackViolation ( argumentNode ) ;
181
+ checkArrowFunctionViolation ( argumentNode ) ;
182
+ checkFunctionExpressionViolation ( argumentNode ) ;
177
183
}
178
184
179
185
return {
0 commit comments