await-async-query.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RULE_NAME = void 0;
  4. var experimental_utils_1 = require("@typescript-eslint/experimental-utils");
  5. var utils_1 = require("../utils");
  6. var node_utils_1 = require("../node-utils");
  7. exports.RULE_NAME = 'await-async-query';
  8. var ASYNC_QUERIES_REGEXP = /^find(All)?By(LabelText|PlaceholderText|Text|AltText|Title|DisplayValue|Role|TestId)$/;
  9. function hasClosestExpectResolvesRejects(node) {
  10. if (!node.parent) {
  11. return false;
  12. }
  13. if (node_utils_1.isCallExpression(node) &&
  14. node_utils_1.isIdentifier(node.callee) &&
  15. node_utils_1.isMemberExpression(node.parent) &&
  16. node.callee.name === 'expect') {
  17. var expectMatcher = node.parent.property;
  18. return (node_utils_1.isIdentifier(expectMatcher) &&
  19. (expectMatcher.name === 'resolves' || expectMatcher.name === 'rejects'));
  20. }
  21. else {
  22. return hasClosestExpectResolvesRejects(node.parent);
  23. }
  24. }
  25. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  26. name: exports.RULE_NAME,
  27. meta: {
  28. type: 'problem',
  29. docs: {
  30. description: 'Enforce async queries to have proper `await`',
  31. category: 'Best Practices',
  32. recommended: 'warn',
  33. },
  34. messages: {
  35. awaitAsyncQuery: '`{{ name }}` must have `await` operator',
  36. },
  37. fixable: null,
  38. schema: [],
  39. },
  40. defaultOptions: [],
  41. create: function (context) {
  42. var _a;
  43. var testingLibraryQueryUsage = [];
  44. var isQueryUsage = function (node) {
  45. return !node_utils_1.isAwaited(node.parent.parent) &&
  46. !node_utils_1.isPromiseResolved(node) &&
  47. !hasClosestExpectResolvesRejects(node);
  48. };
  49. var hasImportedFromTestingLibraryModule = false;
  50. function report(params) {
  51. if (hasImportedFromTestingLibraryModule) {
  52. context.report(params);
  53. }
  54. }
  55. return _a = {
  56. 'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier': function (node) {
  57. var importDeclaration = node.parent;
  58. var module = importDeclaration.source.value.toString();
  59. if (utils_1.LIBRARY_MODULES.includes(module)) {
  60. hasImportedFromTestingLibraryModule = true;
  61. }
  62. }
  63. },
  64. _a["CallExpression > Identifier[name=" + ASYNC_QUERIES_REGEXP + "]"] = function (node) {
  65. if (isQueryUsage(node)) {
  66. testingLibraryQueryUsage.push({ node: node, queryName: node.name });
  67. }
  68. },
  69. _a["MemberExpression > Identifier[name=" + ASYNC_QUERIES_REGEXP + "]"] = function (node) {
  70. var parent = node.parent;
  71. if (isQueryUsage(parent)) {
  72. testingLibraryQueryUsage.push({ node: parent, queryName: node.name });
  73. }
  74. },
  75. _a['Program:exit'] = function () {
  76. testingLibraryQueryUsage.forEach(function (_a) {
  77. var node = _a.node, queryName = _a.queryName;
  78. var references = node_utils_1.getVariableReferences(context, node.parent.parent);
  79. if (references && references.length === 0) {
  80. report({
  81. node: node,
  82. messageId: 'awaitAsyncQuery',
  83. data: {
  84. name: queryName,
  85. },
  86. });
  87. }
  88. else {
  89. for (var _i = 0, references_1 = references; _i < references_1.length; _i++) {
  90. var reference = references_1[_i];
  91. var referenceNode = reference.identifier;
  92. if (!node_utils_1.isAwaited(referenceNode.parent) &&
  93. !node_utils_1.isPromiseResolved(referenceNode)) {
  94. report({
  95. node: node,
  96. messageId: 'awaitAsyncQuery',
  97. data: {
  98. name: queryName,
  99. },
  100. });
  101. break;
  102. }
  103. }
  104. }
  105. });
  106. },
  107. _a;
  108. },
  109. });