Skip to content

fix: prefer-query-by-disappearance error line and column to the beginning of get or find queries #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading