Skip to content

Commit

Permalink
fix (prefer-query-by-disappearance): error line and column to the b…
Browse files Browse the repository at this point in the history
…eginning 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
  • Loading branch information
sjarva committed Sep 3, 2022
1 parent 501322d commit df3d647
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 77 deletions.
50 changes: 28 additions & 22 deletions lib/rules/prefer-query-by-disappearance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export default createTestingLibraryRule<Options, MessageIds>({
return helpers.isAsyncUtil(identifierNode, ['waitForElementToBeRemoved']);
}

function isReportableExpression(node: TSESTree.LeftHandSideExpression) {
/**
* Checks if node is reportable (starts with "get" or "find") and if it is, reports it with `context.report()`.
*
* @param {TSESTree.LeftHandSideExpression} node - Node to be tested
* @returns {Boolean} Boolean indicating if expression was reported
*/
function reportExpression(node: TSESTree.LeftHandSideExpression): boolean {
const argumentProperty = isMemberExpression(node)
? getPropertyIdentifierNode(node.property)
: getPropertyIdentifierNode(node);
Expand All @@ -59,13 +65,20 @@ export default createTestingLibraryRule<Options, MessageIds>({
return false;
}

return (
if (
helpers.isGetQueryVariant(argumentProperty) ||
helpers.isFindQueryVariant(argumentProperty)
);
) {
context.report({
node: argumentProperty,
messageId: 'preferQueryByDisappearance',
});
return true;
}
return false;
}

function isNonCallbackViolation(node: TSESTree.CallExpressionArgument) {
function checkNonCallbackViolation(node: TSESTree.CallExpressionArgument) {
if (!isCallExpression(node)) {
return false;
}
Expand All @@ -77,15 +90,15 @@ export default createTestingLibraryRule<Options, MessageIds>({
return false;
}

return isReportableExpression(node.callee);
return reportExpression(node.callee);
}

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

return isReportableExpression(node.argument.callee);
return reportExpression(node.argument.callee);
}

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

return isReportableExpression(node.expression.callee);
return reportExpression(node.expression.callee);
}

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

function isFunctionExpressionViolation(
function checkFunctionExpressionViolation(
node: TSESTree.CallExpressionArgument
) {
if (!isFunctionExpression(node)) {
Expand Down Expand Up @@ -145,10 +158,12 @@ export default createTestingLibraryRule<Options, MessageIds>({
return false;
}

return isReportableExpression(node.body.callee);
return reportExpression(node.body.callee);
}

function isArrowFunctionViolation(node: TSESTree.CallExpressionArgument) {
function checkArrowFunctionViolation(
node: TSESTree.CallExpressionArgument
) {
return (
isArrowFunctionBodyViolation(node) ||
isArrowFunctionImplicitReturnViolation(node)
Expand All @@ -162,18 +177,9 @@ export default createTestingLibraryRule<Options, MessageIds>({

const argumentNode = node.arguments[0];

if (
!isNonCallbackViolation(argumentNode) &&
!isArrowFunctionViolation(argumentNode) &&
!isFunctionExpressionViolation(argumentNode)
) {
return;
}

context.report({
node: argumentNode,
messageId: 'preferQueryByDisappearance',
});
checkNonCallbackViolation(argumentNode);
checkArrowFunctionViolation(argumentNode);
checkFunctionExpressionViolation(argumentNode);
}

return {
Expand Down

0 comments on commit df3d647

Please sign in to comment.