From 954676fbf247f85e7130af6d0b43a9d1197f6078 Mon Sep 17 00:00:00 2001 From: timdeschryver <28659384+timdeschryver@users.noreply.github.com> Date: Tue, 9 Jun 2020 20:07:44 +0200 Subject: [PATCH] fix(prefer-wait-for): only report when imported from TL --- lib/rules/prefer-wait-for.ts | 66 +++++++++++++------------ tests/lib/rules/prefer-wait-for.test.ts | 19 +++++++ 2 files changed, 54 insertions(+), 31 deletions(-) diff --git a/lib/rules/prefer-wait-for.ts b/lib/rules/prefer-wait-for.ts index 3494bb3d..cf5b6967 100644 --- a/lib/rules/prefer-wait-for.ts +++ b/lib/rules/prefer-wait-for.ts @@ -34,9 +34,6 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({ defaultOptions: [], create(context) { - const importNodes: TSESTree.ImportDeclaration[] = []; - const waitNodes: TSESTree.Identifier[] = []; - const reportImport = (node: TSESTree.ImportDeclaration) => { context.report({ node: node, @@ -44,7 +41,6 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({ 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( @@ -119,36 +115,44 @@ export default ESLintUtils.RuleCreator(getDocsUrl)({ '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) + ) + ); }); }, + '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); + } + }) + ); + }, }; }, }); diff --git a/tests/lib/rules/prefer-wait-for.test.ts b/tests/lib/rules/prefer-wait-for.test.ts index a23286f3..243a6ada 100644 --- a/tests/lib/rules/prefer-wait-for.test.ts +++ b/tests/lib/rules/prefer-wait-for.test.ts @@ -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(); + `, + }, ], invalid: [