-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathprefer-wait-for.ts
158 lines (140 loc) · 5 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
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils';
import { getDocsUrl } from '../utils';
import {
isImportSpecifier,
isMemberExpression,
isIdentifier,
findClosestCallExpressionNode,
} from '../node-utils';
export const RULE_NAME = 'prefer-wait-for';
export type MessageIds = 'preferWaitForMethod' | 'preferWaitForImport';
type Options = [];
const DEPRECATED_METHODS = ['wait', 'waitForElement', 'waitForDomChange'];
export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
name: RULE_NAME,
meta: {
type: 'suggestion',
docs: {
description: 'Use `waitFor` instead of deprecated wait methods',
category: 'Best Practices',
recommended: false,
},
messages: {
preferWaitForMethod:
'`{{ methodName }}` is deprecated in favour of `waitFor`',
preferWaitForImport: 'import `waitFor` instead of deprecated async utils',
},
fixable: 'code',
schema: [],
},
defaultOptions: [],
create(context) {
const reportImport = (node: TSESTree.ImportDeclaration) => {
context.report({
node: node,
messageId: 'preferWaitForImport',
fix(fixer) {
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];
// get all import names excluding all testing library `wait*` utils...
const newImports = node.specifiers
.filter(
specifier =>
isImportSpecifier(specifier) &&
!excludedImports.includes(specifier.imported.name)
)
.map(
(specifier: TSESTree.ImportSpecifier) => specifier.imported.name
);
// ... 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) => {
context.report({
node: node,
messageId: 'preferWaitForMethod',
data: {
methodName: node.name,
},
fix(fixer) {
const callExpressionNode = findClosestCallExpressionNode(node);
const [arg] = callExpressionNode.arguments;
const fixers = [];
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) &&
isIdentifier(node.parent.object)
) {
methodReplacement = `${node.parent.object.name}.${methodReplacement}`;
}
const newText = methodReplacement;
fixers.push(fixer.replaceText(callExpressionNode, newText));
}
return fixers;
},
});
};
return {
'ImportDeclaration[source.value=/testing-library/]'(
node: TSESTree.ImportDeclaration
) {
const deprecatedImportSpecifiers = node.specifiers.filter(
specifier =>
isImportSpecifier(specifier) &&
specifier.imported &&
DEPRECATED_METHODS.includes(specifier.imported.name)
);
deprecatedImportSpecifiers.forEach((importSpecifier, i) => {
if (i === 0) {
reportImport(node);
}
context
.getDeclaredVariables(importSpecifier)
.forEach(variable =>
variable.references.forEach(reference =>
reportWait(reference.identifier)
)
);
});
},
'ImportDeclaration[source.value=/testing-library/] > ImportNamespaceSpecifier'(
node: TSESTree.ImportNamespaceSpecifier
) {
context.getDeclaredVariables(node).forEach(variable =>
variable.references.forEach(reference => {
if (
isMemberExpression(reference.identifier.parent) &&
isIdentifier(reference.identifier.parent.property) &&
DEPRECATED_METHODS.includes(
reference.identifier.parent.property.name
)
) {
reportWait(reference.identifier.parent.property);
}
})
);
},
};
},
});