prefer-query-by-disappearance.js
4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
const create_testing_library_rule_1 = require("../create-testing-library-rule");
const node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'prefer-query-by-disappearance';
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
type: 'problem',
docs: {
description: 'Suggest using `queryBy*` queries when waiting for disappearance',
recommendedConfig: {
dom: 'error',
angular: 'error',
react: 'error',
vue: 'error',
marko: 'error',
},
},
messages: {
preferQueryByDisappearance: 'Prefer using queryBy* when waiting for disappearance',
},
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
function isWaitForElementToBeRemoved(node) {
const identifierNode = (0, node_utils_1.getPropertyIdentifierNode)(node);
if (!identifierNode) {
return false;
}
return helpers.isAsyncUtil(identifierNode, ['waitForElementToBeRemoved']);
}
function reportExpression(node) {
const argumentProperty = (0, node_utils_1.isMemberExpression)(node)
? (0, node_utils_1.getPropertyIdentifierNode)(node.property)
: (0, node_utils_1.getPropertyIdentifierNode)(node);
if (!argumentProperty) {
return false;
}
if (helpers.isGetQueryVariant(argumentProperty) ||
helpers.isFindQueryVariant(argumentProperty)) {
context.report({
node: argumentProperty,
messageId: 'preferQueryByDisappearance',
});
return true;
}
return false;
}
function checkNonCallbackViolation(node) {
if (!(0, node_utils_1.isCallExpression)(node)) {
return false;
}
if (!(0, node_utils_1.isMemberExpression)(node.callee) &&
!(0, node_utils_1.getPropertyIdentifierNode)(node.callee)) {
return false;
}
return reportExpression(node.callee);
}
function isReturnViolation(node) {
if (!(0, node_utils_1.isReturnStatement)(node) || !(0, node_utils_1.isCallExpression)(node.argument)) {
return false;
}
return reportExpression(node.argument.callee);
}
function isNonReturnViolation(node) {
if (!(0, node_utils_1.isExpressionStatement)(node) || !(0, node_utils_1.isCallExpression)(node.expression)) {
return false;
}
if (!(0, node_utils_1.isMemberExpression)(node.expression.callee) &&
!(0, node_utils_1.getPropertyIdentifierNode)(node.expression.callee)) {
return false;
}
return reportExpression(node.expression.callee);
}
function isStatementViolation(statement) {
return isReturnViolation(statement) || isNonReturnViolation(statement);
}
function checkFunctionExpressionViolation(node) {
if (!(0, node_utils_1.isFunctionExpression)(node)) {
return false;
}
return node.body.body.some((statement) => isStatementViolation(statement));
}
function isArrowFunctionBodyViolation(node) {
if (!(0, node_utils_1.isArrowFunctionExpression)(node) || !(0, node_utils_1.isBlockStatement)(node.body)) {
return false;
}
return node.body.body.some((statement) => isStatementViolation(statement));
}
function isArrowFunctionImplicitReturnViolation(node) {
if (!(0, node_utils_1.isArrowFunctionExpression)(node) || !(0, node_utils_1.isCallExpression)(node.body)) {
return false;
}
if (!(0, node_utils_1.isMemberExpression)(node.body.callee) &&
!(0, node_utils_1.getPropertyIdentifierNode)(node.body.callee)) {
return false;
}
return reportExpression(node.body.callee);
}
function checkArrowFunctionViolation(node) {
return (isArrowFunctionBodyViolation(node) ||
isArrowFunctionImplicitReturnViolation(node));
}
function check(node) {
if (!isWaitForElementToBeRemoved(node)) {
return;
}
const argumentNode = node.arguments[0];
checkNonCallbackViolation(argumentNode);
checkArrowFunctionViolation(argumentNode);
checkFunctionExpressionViolation(argumentNode);
}
return {
CallExpression: check,
};
},
});