prefer-find-by.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getFindByQueryVariant = exports.WAIT_METHODS = exports.RULE_NAME = void 0;
  4. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  5. var node_utils_1 = require("../node-utils");
  6. var utils_1 = require("../utils");
  7. exports.RULE_NAME = 'prefer-find-by';
  8. exports.WAIT_METHODS = ['waitFor', 'waitForElement', 'wait'];
  9. function getFindByQueryVariant(queryMethod) {
  10. return queryMethod.includes('All') ? 'findAllBy' : 'findBy';
  11. }
  12. exports.getFindByQueryVariant = getFindByQueryVariant;
  13. function findRenderDefinitionDeclaration(scope, query) {
  14. if (!scope) {
  15. return null;
  16. }
  17. var variable = scope.variables.find(function (v) { return v.name === query; });
  18. if (variable) {
  19. var def = variable.defs.find(function (_a) {
  20. var name = _a.name;
  21. return name.name === query;
  22. });
  23. return def.name;
  24. }
  25. return findRenderDefinitionDeclaration(scope.upper, query);
  26. }
  27. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  28. name: exports.RULE_NAME,
  29. meta: {
  30. type: 'suggestion',
  31. docs: {
  32. description: 'Suggest using find* instead of waitFor to wait for elements',
  33. category: 'Best Practices',
  34. recommended: 'warn',
  35. },
  36. messages: {
  37. preferFindBy: 'Prefer {{queryVariant}}{{queryMethod}} method over using await {{fullQuery}}',
  38. },
  39. fixable: 'code',
  40. schema: [],
  41. },
  42. defaultOptions: [],
  43. create: function (context) {
  44. var sourceCode = context.getSourceCode();
  45. function reportInvalidUsage(node, _a) {
  46. var queryVariant = _a.queryVariant, queryMethod = _a.queryMethod, fix = _a.fix;
  47. context.report({
  48. node: node,
  49. messageId: 'preferFindBy',
  50. data: {
  51. queryVariant: queryVariant,
  52. queryMethod: queryMethod,
  53. fullQuery: sourceCode.getText(node),
  54. },
  55. fix: fix,
  56. });
  57. }
  58. return {
  59. 'AwaitExpression > CallExpression': function (node) {
  60. if (!node_utils_1.isIdentifier(node.callee) ||
  61. !exports.WAIT_METHODS.includes(node.callee.name)) {
  62. return;
  63. }
  64. var argument = node.arguments[0];
  65. if (!node_utils_1.isArrowFunctionExpression(argument)) {
  66. return;
  67. }
  68. if (!node_utils_1.isCallExpression(argument.body)) {
  69. return;
  70. }
  71. if (node_utils_1.isMemberExpression(argument.body.callee) &&
  72. node_utils_1.isIdentifier(argument.body.callee.property) &&
  73. node_utils_1.isIdentifier(argument.body.callee.object) &&
  74. utils_1.SYNC_QUERIES_COMBINATIONS.includes(argument.body.callee.property.name)) {
  75. var fullQueryMethod = argument.body.callee.property.name;
  76. var caller_1 = argument.body.callee.object.name;
  77. var queryVariant_1 = getFindByQueryVariant(fullQueryMethod);
  78. var callArguments_1 = argument.body.arguments;
  79. var queryMethod_1 = fullQueryMethod.split('By')[1];
  80. reportInvalidUsage(node, {
  81. queryMethod: queryMethod_1,
  82. queryVariant: queryVariant_1,
  83. fix: function (fixer) {
  84. var newCode = caller_1 + "." + queryVariant_1 + queryMethod_1 + "(" + callArguments_1
  85. .map(function (node) { return sourceCode.getText(node); })
  86. .join(', ') + ")";
  87. return fixer.replaceText(node, newCode);
  88. },
  89. });
  90. return;
  91. }
  92. if (node_utils_1.isIdentifier(argument.body.callee) &&
  93. utils_1.SYNC_QUERIES_COMBINATIONS.includes(argument.body.callee.name)) {
  94. var fullQueryMethod_1 = argument.body.callee.name;
  95. var queryMethod_2 = fullQueryMethod_1.split('By')[1];
  96. var queryVariant_2 = getFindByQueryVariant(fullQueryMethod_1);
  97. var callArguments_2 = argument.body.arguments;
  98. reportInvalidUsage(node, {
  99. queryMethod: queryMethod_2,
  100. queryVariant: queryVariant_2,
  101. fix: function (fixer) {
  102. var findByMethod = "" + queryVariant_2 + queryMethod_2;
  103. var allFixes = [];
  104. var newCode = findByMethod + "(" + callArguments_2
  105. .map(function (node) { return sourceCode.getText(node); })
  106. .join(', ') + ")";
  107. allFixes.push(fixer.replaceText(node, newCode));
  108. var definition = findRenderDefinitionDeclaration(context.getScope(), fullQueryMethod_1);
  109. if (!definition) {
  110. return allFixes;
  111. }
  112. if (node_utils_1.isObjectPattern(definition.parent.parent)) {
  113. var allVariableDeclarations = definition.parent.parent;
  114. if (allVariableDeclarations.properties.some(function (p) {
  115. return node_utils_1.isProperty(p) &&
  116. node_utils_1.isIdentifier(p.key) &&
  117. p.key.name === findByMethod;
  118. })) {
  119. return allFixes;
  120. }
  121. var textDestructuring = sourceCode.getText(allVariableDeclarations);
  122. var text = textDestructuring.substring(0, textDestructuring.length - 2) +
  123. (", " + findByMethod + " }");
  124. allFixes.push(fixer.replaceText(allVariableDeclarations, text));
  125. }
  126. return allFixes;
  127. },
  128. });
  129. return;
  130. }
  131. },
  132. };
  133. },
  134. });