|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +const { getDocsUrl } = require('../utils'); |
| 4 | + |
| 5 | +const DEPRECATED_METHODS = ['wait', 'waitForElement', 'waitForDomChange']; |
| 6 | + |
| 7 | +module.exports = { |
| 8 | + meta: { |
| 9 | + type: 'suggestion', |
| 10 | + docs: { |
| 11 | + description: 'Use `waitFor` instead of deprecated wait methods', |
| 12 | + category: 'Best Practices', |
| 13 | + recommended: false, |
| 14 | + url: getDocsUrl('prefer-wait-for'), |
| 15 | + }, |
| 16 | + messages: { |
| 17 | + preferWaitForMethod: |
| 18 | + '`{{ methodName }}` is deprecated in favour of `waitFor`', |
| 19 | + preferWaitForImport: 'import `waitFor` instead of deprecated async utils', |
| 20 | + }, |
| 21 | + fixable: 'code', |
| 22 | + schema: [], |
| 23 | + }, |
| 24 | + |
| 25 | + create: function(context) { |
| 26 | + const importNodes = []; |
| 27 | + const waitNodes = []; |
| 28 | + |
| 29 | + const reportImport = node => { |
| 30 | + context.report({ |
| 31 | + node: node, |
| 32 | + messageId: 'preferWaitForImport', |
| 33 | + fix(fixer) { |
| 34 | + const excludedImports = [...DEPRECATED_METHODS, 'waitFor']; |
| 35 | + |
| 36 | + // get all import names excluding all testing library `wait*` utils... |
| 37 | + const newImports = node.specifiers |
| 38 | + .filter( |
| 39 | + specifier => !excludedImports.includes(specifier.imported.name) |
| 40 | + ) |
| 41 | + .map(specifier => specifier.imported.name); |
| 42 | + |
| 43 | + // ... and append `waitFor` |
| 44 | + newImports.push('waitFor'); |
| 45 | + |
| 46 | + // build new node with new imports and previous source value |
| 47 | + const newNode = `import { ${newImports.join(',')} } from '${ |
| 48 | + node.source.value |
| 49 | + }';`; |
| 50 | + |
| 51 | + return fixer.replaceText(node, newNode); |
| 52 | + }, |
| 53 | + }); |
| 54 | + }; |
| 55 | + |
| 56 | + const reportWait = node => { |
| 57 | + context.report({ |
| 58 | + node: node, |
| 59 | + messageId: 'preferWaitForMethod', |
| 60 | + data: { |
| 61 | + methodName: node.name, |
| 62 | + }, |
| 63 | + fix(fixer) { |
| 64 | + const { parent } = node; |
| 65 | + const [arg] = parent.arguments; |
| 66 | + const fixers = []; |
| 67 | + |
| 68 | + if (arg) { |
| 69 | + // if method been fixed already had a callback |
| 70 | + // then we just replace the method name. |
| 71 | + fixers.push(fixer.replaceText(node, 'waitFor')); |
| 72 | + |
| 73 | + if (node.name === 'waitForDomChange') { |
| 74 | + // if method been fixed is `waitForDomChange` |
| 75 | + // then the arg received was options object so we need to insert |
| 76 | + // empty callback before. |
| 77 | + fixers.push(fixer.insertTextBefore(arg, `() => {}, `)); |
| 78 | + } |
| 79 | + } else { |
| 80 | + // if wait method been fixed didn't have any callback |
| 81 | + // then we replace the method name and include an empty callback. |
| 82 | + fixers.push(fixer.replaceText(parent, 'waitFor(() => {})')); |
| 83 | + } |
| 84 | + |
| 85 | + return fixers; |
| 86 | + }, |
| 87 | + }); |
| 88 | + }; |
| 89 | + |
| 90 | + return { |
| 91 | + 'ImportDeclaration[source.value=/testing-library/]'(node) { |
| 92 | + const importedNames = node.specifiers |
| 93 | + .map(specifier => specifier.imported && specifier.imported.name) |
| 94 | + .filter(Boolean); |
| 95 | + |
| 96 | + if ( |
| 97 | + importedNames.some(importedName => |
| 98 | + DEPRECATED_METHODS.includes(importedName) |
| 99 | + ) |
| 100 | + ) { |
| 101 | + importNodes.push(node); |
| 102 | + } |
| 103 | + }, |
| 104 | + 'CallExpression > Identifier[name=/^(wait|waitForElement|waitForDomChange)$/]'( |
| 105 | + node |
| 106 | + ) { |
| 107 | + waitNodes.push(node); |
| 108 | + }, |
| 109 | + 'Program:exit'() { |
| 110 | + waitNodes.forEach(waitNode => { |
| 111 | + reportWait(waitNode); |
| 112 | + }); |
| 113 | + |
| 114 | + importNodes.forEach(importNode => { |
| 115 | + reportImport(importNode); |
| 116 | + }); |
| 117 | + }, |
| 118 | + }; |
| 119 | + }, |
| 120 | +}; |
0 commit comments