Skip to content

Commit b80accf

Browse files
fix(prefer-wait-for): only report when imported from TL (#152)
1 parent a783036 commit b80accf

File tree

2 files changed

+54
-31
lines changed

2 files changed

+54
-31
lines changed

lib/rules/prefer-wait-for.ts

+35-31
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,13 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
3434
defaultOptions: [],
3535

3636
create(context) {
37-
const importNodes: TSESTree.ImportDeclaration[] = [];
38-
const waitNodes: TSESTree.Identifier[] = [];
39-
4037
const reportImport = (node: TSESTree.ImportDeclaration) => {
4138
context.report({
4239
node: node,
4340
messageId: 'preferWaitForImport',
4441
fix(fixer) {
4542
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];
4643

47-
// TODO: refactor `importNodes` to TSESTree.ImportSpecifier[] ? (to not have to traverse the list twice)
4844
// get all import names excluding all testing library `wait*` utils...
4945
const newImports = node.specifiers
5046
.filter(
@@ -119,36 +115,44 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
119115
'ImportDeclaration[source.value=/testing-library/]'(
120116
node: TSESTree.ImportDeclaration
121117
) {
122-
const importedNames = node.specifiers
123-
.filter(
124-
specifier => isImportSpecifier(specifier) && specifier.imported
125-
)
126-
.map(
127-
(specifier: TSESTree.ImportSpecifier) => specifier.imported.name
128-
);
129-
130-
if (
131-
importedNames.some(importedName =>
132-
DEPRECATED_METHODS.includes(importedName)
133-
)
134-
) {
135-
importNodes.push(node);
136-
}
137-
},
138-
'CallExpression Identifier[name=/^(wait|waitForElement|waitForDomChange)$/]'(
139-
node: TSESTree.Identifier
140-
) {
141-
waitNodes.push(node);
142-
},
143-
'Program:exit'() {
144-
waitNodes.forEach(waitNode => {
145-
reportWait(waitNode);
146-
});
118+
const deprecatedImportSpecifiers = node.specifiers.filter(
119+
specifier =>
120+
isImportSpecifier(specifier) &&
121+
specifier.imported &&
122+
DEPRECATED_METHODS.includes(specifier.imported.name)
123+
);
124+
125+
deprecatedImportSpecifiers.forEach((importSpecifier, i) => {
126+
if (i === 0) {
127+
reportImport(node);
128+
}
147129

148-
importNodes.forEach(importNode => {
149-
reportImport(importNode);
130+
context
131+
.getDeclaredVariables(importSpecifier)
132+
.forEach(variable =>
133+
variable.references.forEach(reference =>
134+
reportWait(reference.identifier)
135+
)
136+
);
150137
});
151138
},
139+
'ImportDeclaration[source.value=/testing-library/] > ImportNamespaceSpecifier'(
140+
node: TSESTree.ImportNamespaceSpecifier
141+
) {
142+
context.getDeclaredVariables(node).forEach(variable =>
143+
variable.references.forEach(reference => {
144+
if (
145+
isMemberExpression(reference.identifier.parent) &&
146+
isIdentifier(reference.identifier.parent.property) &&
147+
DEPRECATED_METHODS.includes(
148+
reference.identifier.parent.property.name
149+
)
150+
) {
151+
reportWait(reference.identifier.parent.property);
152+
}
153+
})
154+
);
155+
},
152156
};
153157
},
154158
});

tests/lib/rules/prefer-wait-for.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,25 @@ ruleTester.run(RULE_NAME, rule, {
4141
await testingLibrary.waitFor(() => {}, { timeout: 500 });
4242
}`,
4343
},
44+
{
45+
code: `import { wait } from 'imNoTestingLibrary';
46+
47+
async () => {
48+
await wait();
49+
}`,
50+
},
51+
{
52+
code: `import * as foo from 'imNoTestingLibrary';
53+
54+
async () => {
55+
await foo.wait();
56+
}`,
57+
},
58+
{
59+
code: `
60+
cy.wait();
61+
`,
62+
},
4463
],
4564

4665
invalid: [

0 commit comments

Comments
 (0)