Skip to content

Commit df3d647

Browse files
authored
fix (prefer-query-by-disappearance): error line and column to the beginning of queries (#639)
* fix: prefer-query-by-disappearance error location Refactor reporting an error deeper, where the get/find query variants are found. Refactor function names to convey that they also check/report the error. Update unit tests. * fix: review feedback Make function name and JSDoc comment more clear
1 parent 501322d commit df3d647

File tree

2 files changed

+83
-77
lines changed

2 files changed

+83
-77
lines changed

lib/rules/prefer-query-by-disappearance.ts

+28-22
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
5050
return helpers.isAsyncUtil(identifierNode, ['waitForElementToBeRemoved']);
5151
}
5252

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 {
5460
const argumentProperty = isMemberExpression(node)
5561
? getPropertyIdentifierNode(node.property)
5662
: getPropertyIdentifierNode(node);
@@ -59,13 +65,20 @@ export default createTestingLibraryRule<Options, MessageIds>({
5965
return false;
6066
}
6167

62-
return (
68+
if (
6369
helpers.isGetQueryVariant(argumentProperty) ||
6470
helpers.isFindQueryVariant(argumentProperty)
65-
);
71+
) {
72+
context.report({
73+
node: argumentProperty,
74+
messageId: 'preferQueryByDisappearance',
75+
});
76+
return true;
77+
}
78+
return false;
6679
}
6780

68-
function isNonCallbackViolation(node: TSESTree.CallExpressionArgument) {
81+
function checkNonCallbackViolation(node: TSESTree.CallExpressionArgument) {
6982
if (!isCallExpression(node)) {
7083
return false;
7184
}
@@ -77,15 +90,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
7790
return false;
7891
}
7992

80-
return isReportableExpression(node.callee);
93+
return reportExpression(node.callee);
8194
}
8295

8396
function isReturnViolation(node: TSESTree.Statement) {
8497
if (!isReturnStatement(node) || !isCallExpression(node.argument)) {
8598
return false;
8699
}
87100

88-
return isReportableExpression(node.argument.callee);
101+
return reportExpression(node.argument.callee);
89102
}
90103

91104
function isNonReturnViolation(node: TSESTree.Statement) {
@@ -100,14 +113,14 @@ export default createTestingLibraryRule<Options, MessageIds>({
100113
return false;
101114
}
102115

103-
return isReportableExpression(node.expression.callee);
116+
return reportExpression(node.expression.callee);
104117
}
105118

106119
function isStatementViolation(statement: TSESTree.Statement) {
107120
return isReturnViolation(statement) || isNonReturnViolation(statement);
108121
}
109122

110-
function isFunctionExpressionViolation(
123+
function checkFunctionExpressionViolation(
111124
node: TSESTree.CallExpressionArgument
112125
) {
113126
if (!isFunctionExpression(node)) {
@@ -145,10 +158,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
145158
return false;
146159
}
147160

148-
return isReportableExpression(node.body.callee);
161+
return reportExpression(node.body.callee);
149162
}
150163

151-
function isArrowFunctionViolation(node: TSESTree.CallExpressionArgument) {
164+
function checkArrowFunctionViolation(
165+
node: TSESTree.CallExpressionArgument
166+
) {
152167
return (
153168
isArrowFunctionBodyViolation(node) ||
154169
isArrowFunctionImplicitReturnViolation(node)
@@ -162,18 +177,9 @@ export default createTestingLibraryRule<Options, MessageIds>({
162177

163178
const argumentNode = node.arguments[0];
164179

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);
177183
}
178184

179185
return {

0 commit comments

Comments
 (0)