-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathprefer-wait-for.ts
213 lines (190 loc) · 6.77 KB
/
prefer-wait-for.ts
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { TSESTree, ASTUtils } from '@typescript-eslint/experimental-utils';
import { createTestingLibraryRule } from '../create-testing-library-rule';
import {
isImportSpecifier,
isMemberExpression,
findClosestCallExpressionNode,
isCallExpression,
isImportNamespaceSpecifier,
isObjectPattern,
isProperty,
} from '../node-utils';
export const RULE_NAME = 'prefer-wait-for';
export type MessageIds =
| 'preferWaitForImport'
| 'preferWaitForMethod'
| 'preferWaitForRequire';
type Options = [];
const DEPRECATED_METHODS = ['wait', 'waitForElement', 'waitForDomChange'];
export default createTestingLibraryRule<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Use `waitFor` instead of deprecated wait methods',
recommendedConfig: {
dom: false,
angular: false,
react: false,
vue: 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: TSESTree.ObjectPattern) => {
context.report({
node,
messageId: 'preferWaitForRequire',
fix(fixer) {
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];
const newAllRequired = node.properties
.filter(
(s) =>
isProperty(s) &&
ASTUtils.isIdentifier(s.key) &&
!excludedImports.includes(s.key.name)
)
.map(
(s) => ((s as TSESTree.Property).key as TSESTree.Identifier).name
);
newAllRequired.push('waitFor');
return fixer.replaceText(node, `{ ${newAllRequired.join(',')} }`);
},
});
};
const reportImport = (node: TSESTree.ImportDeclaration) => {
context.report({
node,
messageId: 'preferWaitForImport',
fix(fixer) {
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];
// get all import names excluding all testing library `wait*` utils...
const newImports = node.specifiers
.map(
(specifier) =>
isImportSpecifier(specifier) &&
!excludedImports.includes(specifier.imported.name) &&
specifier.imported.name
)
.filter(Boolean) as string[];
// ... and append `waitFor`
newImports.push('waitFor');
// build new node with new imports and previous source value
const newNode = `import { ${newImports.join(',')} } from '${
node.source.value
}';`;
return fixer.replaceText(node, newNode);
},
});
};
const reportWait = (node: TSESTree.Identifier | TSESTree.JSXIdentifier) => {
context.report({
node,
messageId: 'preferWaitForMethod',
data: {
methodName: node.name,
},
fix(fixer) {
const callExpressionNode = findClosestCallExpressionNode(node);
if (!callExpressionNode) {
return null;
}
const [arg] = callExpressionNode.arguments;
const fixers = [];
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
if (arg) {
// if method been fixed already had a callback
// then we just replace the method name.
fixers.push(fixer.replaceText(node, 'waitFor'));
if (node.name === 'waitForDomChange') {
// if method been fixed is `waitForDomChange`
// then the arg received was options object so we need to insert
// empty callback before.
fixers.push(fixer.insertTextBefore(arg, '() => {}, '));
}
} else {
// if wait method been fixed didn't have any callback
// then we replace the method name and include an empty callback.
let methodReplacement = 'waitFor(() => {})';
// if wait method used like `foo.wait()` then we need to keep the
// member expression to get `foo.waitFor(() => {})`
if (
isMemberExpression(node.parent) &&
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: TSESTree.MemberExpression) {
const isDeprecatedMethod =
ASTUtils.isIdentifier(node.property) &&
DEPRECATED_METHODS.includes(node.property.name);
if (!isDeprecatedMethod) {
// the method does not match a deprecated method
return;
}
if (!helpers.isNodeComingFromTestingLibrary(node)) {
// the method does not match from the imported elements from TL (even from custom)
return;
}
addWaitFor = true;
reportWait(node.property as TSESTree.Identifier); // compiler is not picking up correctly, it should have inferred it is an identifier
},
'CallExpression > Identifier'(node: TSESTree.Identifier) {
if (!DEPRECATED_METHODS.includes(node.name)) {
return;
}
if (!helpers.isNodeComingFromTestingLibrary(node)) {
return;
}
addWaitFor = true;
reportWait(node);
},
'Program:exit'() {
if (!addWaitFor) {
return;
}
// now that all usages of deprecated methods were replaced, remove the extra imports
const testingLibraryNode =
helpers.getCustomModuleImportNode() ??
helpers.getTestingLibraryImportNode();
if (isCallExpression(testingLibraryNode)) {
const parent =
testingLibraryNode.parent as TSESTree.VariableDeclarator;
if (!isObjectPattern(parent.id)) {
// if there is no destructuring, there is nothing to replace
return;
}
reportRequire(parent.id);
} else if (testingLibraryNode) {
if (
testingLibraryNode.specifiers.length === 1 &&
isImportNamespaceSpecifier(testingLibraryNode.specifiers[0])
) {
// if we import everything, there is nothing to replace
return;
}
reportImport(testingLibraryNode);
}
},
};
},
});