prefer-wait-for.js
6.11 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.RULE_NAME = void 0;
const utils_1 = require("@typescript-eslint/utils");
const create_testing_library_rule_1 = require("../create-testing-library-rule");
const node_utils_1 = require("../node-utils");
exports.RULE_NAME = 'prefer-wait-for';
const DEPRECATED_METHODS = ['wait', 'waitForElement', 'waitForDomChange'];
exports.default = (0, create_testing_library_rule_1.createTestingLibraryRule)({
name: exports.RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Use `waitFor` instead of deprecated wait methods',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: false,
marko: false,
},
},
messages: {
preferWaitForMethod: '`{{ methodName }}` is deprecated in favour of `waitFor`',
preferWaitForImport: 'import `waitFor` instead of deprecated async utils',
preferWaitForRequire: 'require `waitFor` instead of deprecated async utils',
},
fixable: 'code',
schema: [],
},
defaultOptions: [],
create(context, _, helpers) {
let addWaitFor = false;
const reportRequire = (node) => {
context.report({
node,
messageId: 'preferWaitForRequire',
fix(fixer) {
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];
const newAllRequired = node.properties
.filter((s) => (0, node_utils_1.isProperty)(s) &&
utils_1.ASTUtils.isIdentifier(s.key) &&
!excludedImports.includes(s.key.name))
.map((s) => s.key.name);
newAllRequired.push('waitFor');
return fixer.replaceText(node, `{ ${newAllRequired.join(',')} }`);
},
});
};
const reportImport = (node) => {
context.report({
node,
messageId: 'preferWaitForImport',
fix(fixer) {
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];
const newImports = node.specifiers
.map((specifier) => (0, node_utils_1.isImportSpecifier)(specifier) &&
!excludedImports.includes(specifier.imported.name) &&
specifier.imported.name)
.filter(Boolean);
newImports.push('waitFor');
const newNode = `import { ${newImports.join(',')} } from '${node.source.value}';`;
return fixer.replaceText(node, newNode);
},
});
};
const reportWait = (node) => {
context.report({
node,
messageId: 'preferWaitForMethod',
data: {
methodName: node.name,
},
fix(fixer) {
const callExpressionNode = (0, node_utils_1.findClosestCallExpressionNode)(node);
if (!callExpressionNode) {
return null;
}
const [arg] = callExpressionNode.arguments;
const fixers = [];
if (arg) {
fixers.push(fixer.replaceText(node, 'waitFor'));
if (node.name === 'waitForDomChange') {
fixers.push(fixer.insertTextBefore(arg, '() => {}, '));
}
}
else {
let methodReplacement = 'waitFor(() => {})';
if ((0, node_utils_1.isMemberExpression)(node.parent) &&
utils_1.ASTUtils.isIdentifier(node.parent.object)) {
methodReplacement = `${node.parent.object.name}.${methodReplacement}`;
}
const newText = methodReplacement;
fixers.push(fixer.replaceText(callExpressionNode, newText));
}
return fixers;
},
});
};
return {
'CallExpression > MemberExpression'(node) {
const isDeprecatedMethod = utils_1.ASTUtils.isIdentifier(node.property) &&
DEPRECATED_METHODS.includes(node.property.name);
if (!isDeprecatedMethod) {
return;
}
if (!helpers.isNodeComingFromTestingLibrary(node)) {
return;
}
addWaitFor = true;
reportWait(node.property);
},
'CallExpression > Identifier'(node) {
if (!DEPRECATED_METHODS.includes(node.name)) {
return;
}
if (!helpers.isNodeComingFromTestingLibrary(node)) {
return;
}
addWaitFor = true;
reportWait(node);
},
'Program:exit'() {
var _a;
if (!addWaitFor) {
return;
}
const testingLibraryNode = (_a = helpers.getCustomModuleImportNode()) !== null && _a !== void 0 ? _a : helpers.getTestingLibraryImportNode();
if ((0, node_utils_1.isCallExpression)(testingLibraryNode)) {
const parent = testingLibraryNode.parent;
if (!(0, node_utils_1.isObjectPattern)(parent.id)) {
return;
}
reportRequire(parent.id);
}
else if (testingLibraryNode) {
if (testingLibraryNode.specifiers.length === 1 &&
(0, node_utils_1.isImportNamespaceSpecifier)(testingLibraryNode.specifiers[0])) {
return;
}
reportImport(testingLibraryNode);
}
},
};
},
});