prefer-wait-for.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. var __spreadArray = (this && this.__spreadArray) || function (to, from) {
  3. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  4. to[j] = from[i];
  5. return to;
  6. };
  7. Object.defineProperty(exports, "__esModule", { value: true });
  8. exports.RULE_NAME = void 0;
  9. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  10. var utils_1 = require("../utils");
  11. var node_utils_1 = require("../node-utils");
  12. exports.RULE_NAME = 'prefer-wait-for';
  13. var DEPRECATED_METHODS = ['wait', 'waitForElement', 'waitForDomChange'];
  14. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  15. name: exports.RULE_NAME,
  16. meta: {
  17. type: 'suggestion',
  18. docs: {
  19. description: 'Use `waitFor` instead of deprecated wait methods',
  20. category: 'Best Practices',
  21. recommended: false,
  22. },
  23. messages: {
  24. preferWaitForMethod: '`{{ methodName }}` is deprecated in favour of `waitFor`',
  25. preferWaitForImport: 'import `waitFor` instead of deprecated async utils',
  26. },
  27. fixable: 'code',
  28. schema: [],
  29. },
  30. defaultOptions: [],
  31. create: function (context) {
  32. var reportImport = function (node) {
  33. context.report({
  34. node: node,
  35. messageId: 'preferWaitForImport',
  36. fix: function (fixer) {
  37. var excludedImports = __spreadArray(__spreadArray([], DEPRECATED_METHODS), ['waitFor']);
  38. var newImports = node.specifiers
  39. .filter(function (specifier) {
  40. return node_utils_1.isImportSpecifier(specifier) &&
  41. !excludedImports.includes(specifier.imported.name);
  42. })
  43. .map(function (specifier) { return specifier.imported.name; });
  44. newImports.push('waitFor');
  45. var newNode = "import { " + newImports.join(',') + " } from '" + node.source.value + "';";
  46. return fixer.replaceText(node, newNode);
  47. },
  48. });
  49. };
  50. var reportWait = function (node) {
  51. context.report({
  52. node: node,
  53. messageId: 'preferWaitForMethod',
  54. data: {
  55. methodName: node.name,
  56. },
  57. fix: function (fixer) {
  58. var callExpressionNode = node_utils_1.findClosestCallExpressionNode(node);
  59. var arg = callExpressionNode.arguments[0];
  60. var fixers = [];
  61. if (arg) {
  62. fixers.push(fixer.replaceText(node, 'waitFor'));
  63. if (node.name === 'waitForDomChange') {
  64. fixers.push(fixer.insertTextBefore(arg, '() => {}, '));
  65. }
  66. }
  67. else {
  68. var methodReplacement = 'waitFor(() => {})';
  69. if (node_utils_1.isMemberExpression(node.parent) &&
  70. node_utils_1.isIdentifier(node.parent.object)) {
  71. methodReplacement = node.parent.object.name + "." + methodReplacement;
  72. }
  73. var newText = methodReplacement;
  74. fixers.push(fixer.replaceText(callExpressionNode, newText));
  75. }
  76. return fixers;
  77. },
  78. });
  79. };
  80. return {
  81. 'ImportDeclaration[source.value=/testing-library/]': function (node) {
  82. var deprecatedImportSpecifiers = node.specifiers.filter(function (specifier) {
  83. return node_utils_1.isImportSpecifier(specifier) &&
  84. specifier.imported &&
  85. DEPRECATED_METHODS.includes(specifier.imported.name);
  86. });
  87. deprecatedImportSpecifiers.forEach(function (importSpecifier, i) {
  88. if (i === 0) {
  89. reportImport(node);
  90. }
  91. context
  92. .getDeclaredVariables(importSpecifier)
  93. .forEach(function (variable) {
  94. return variable.references.forEach(function (reference) {
  95. return reportWait(reference.identifier);
  96. });
  97. });
  98. });
  99. },
  100. 'ImportDeclaration[source.value=/testing-library/] > ImportNamespaceSpecifier': function (node) {
  101. context.getDeclaredVariables(node).forEach(function (variable) {
  102. return variable.references.forEach(function (reference) {
  103. if (node_utils_1.isMemberExpression(reference.identifier.parent) &&
  104. node_utils_1.isIdentifier(reference.identifier.parent.property) &&
  105. DEPRECATED_METHODS.includes(reference.identifier.parent.property.name)) {
  106. reportWait(reference.identifier.parent.property);
  107. }
  108. });
  109. });
  110. },
  111. };
  112. },
  113. });