Skip to content

fix(prefer-wait-for): only report when imported from TL #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 35 additions & 31 deletions lib/rules/prefer-wait-for.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
defaultOptions: [],

create(context) {
const importNodes: TSESTree.ImportDeclaration[] = [];
const waitNodes: TSESTree.Identifier[] = [];

const reportImport = (node: TSESTree.ImportDeclaration) => {
context.report({
node: node,
messageId: 'preferWaitForImport',
fix(fixer) {
const excludedImports = [...DEPRECATED_METHODS, 'waitFor'];

// TODO: refactor `importNodes` to TSESTree.ImportSpecifier[] ? (to not have to traverse the list twice)
// get all import names excluding all testing library `wait*` utils...
const newImports = node.specifiers
.filter(
Expand Down Expand Up @@ -119,36 +115,44 @@ export default ESLintUtils.RuleCreator(getDocsUrl)<Options, MessageIds>({
'ImportDeclaration[source.value=/testing-library/]'(
node: TSESTree.ImportDeclaration
) {
const importedNames = node.specifiers
.filter(
specifier => isImportSpecifier(specifier) && specifier.imported
)
.map(
(specifier: TSESTree.ImportSpecifier) => specifier.imported.name
);

if (
importedNames.some(importedName =>
DEPRECATED_METHODS.includes(importedName)
)
) {
importNodes.push(node);
}
},
'CallExpression Identifier[name=/^(wait|waitForElement|waitForDomChange)$/]'(
node: TSESTree.Identifier
) {
waitNodes.push(node);
},
'Program:exit'() {
waitNodes.forEach(waitNode => {
reportWait(waitNode);
});
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);
}

importNodes.forEach(importNode => {
reportImport(importNode);
context
.getDeclaredVariables(importSpecifier)
.forEach(variable =>
variable.references.forEach(reference =>
reportWait(reference.identifier)
)
);
Comment on lines +130 to +136
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we could make this cleaner? 👀

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps

context
  .getDeclaredVariables(importSpecifier)
  .flatMap(variable => variable.references)
  .forEach(({ identifier })=> reportWait(identifier))

? But IMO it's not too much of a difference - I think this is fine

And I think flatMap does not work in node <= 10

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, we can't use flatMap in node 10

Copy link
Member

@Belco90 Belco90 Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we could make the code cleaner by extracting the first forEach callback to its own function? It's the only thing I can think of.

});
},
'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);
}
})
);
},
};
},
});
19 changes: 19 additions & 0 deletions tests/lib/rules/prefer-wait-for.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ ruleTester.run(RULE_NAME, rule, {
await testingLibrary.waitFor(() => {}, { timeout: 500 });
}`,
},
{
code: `import { wait } from 'imNoTestingLibrary';

async () => {
await wait();
}`,
},
{
code: `import * as foo from 'imNoTestingLibrary';

async () => {
await foo.wait();
}`,
},
{
code: `
cy.wait();
`,
},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should consider testing require scenarios as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice if we can have an additional test for it, indeed.

],

invalid: [
Expand Down