await-async-utils.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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-utils';
  8. var ASYNC_UTILS_REGEXP = new RegExp("^(" + utils_1.ASYNC_UTILS.join('|') + ")$");
  9. function isPromiseAll(node) {
  10. return node_utils_1.isMemberExpression(node.callee) && node_utils_1.isIdentifier(node.callee.object) && node.callee.object.name === 'Promise' && node_utils_1.isIdentifier(node.callee.property) && node.callee.property.name === 'all';
  11. }
  12. function isInPromiseAll(node) {
  13. var parent = node.parent;
  14. return node_utils_1.isCallExpression(parent) && node_utils_1.isArrayExpression(parent.parent) && node_utils_1.isCallExpression(parent.parent.parent) && isPromiseAll(parent.parent.parent);
  15. }
  16. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  17. name: exports.RULE_NAME,
  18. meta: {
  19. type: 'problem',
  20. docs: {
  21. description: 'Enforce async utils to be awaited properly',
  22. category: 'Best Practices',
  23. recommended: 'warn',
  24. },
  25. messages: {
  26. awaitAsyncUtil: 'Promise returned from `{{ name }}` must be handled',
  27. },
  28. fixable: null,
  29. schema: [],
  30. },
  31. defaultOptions: [],
  32. create: function (context) {
  33. var _a;
  34. var asyncUtilsUsage = [];
  35. var importedAsyncUtils = [];
  36. return _a = {
  37. 'ImportDeclaration > ImportSpecifier,ImportNamespaceSpecifier': function (node) {
  38. var parent = node.parent;
  39. if (!utils_1.LIBRARY_MODULES.includes(parent.source.value.toString()))
  40. return;
  41. if (node_utils_1.isImportSpecifier(node)) {
  42. importedAsyncUtils.push(node.imported.name);
  43. }
  44. if (node_utils_1.isImportNamespaceSpecifier(node)) {
  45. importedAsyncUtils.push(node.local.name);
  46. }
  47. }
  48. },
  49. _a["CallExpression > Identifier[name=" + ASYNC_UTILS_REGEXP + "]"] = function (node) {
  50. asyncUtilsUsage.push({ node: node, name: node.name });
  51. },
  52. _a["CallExpression > MemberExpression > Identifier[name=" + ASYNC_UTILS_REGEXP + "]"] = function (node) {
  53. var memberExpression = node.parent;
  54. var identifier = memberExpression.object;
  55. var memberExpressionName = identifier.name;
  56. asyncUtilsUsage.push({
  57. node: memberExpression,
  58. name: memberExpressionName,
  59. });
  60. },
  61. _a['Program:exit'] = function () {
  62. var testingLibraryUtilUsage = asyncUtilsUsage.filter(function (usage) {
  63. if (node_utils_1.isMemberExpression(usage.node)) {
  64. var object = usage.node.object;
  65. return importedAsyncUtils.includes(object.name);
  66. }
  67. return importedAsyncUtils.includes(usage.name);
  68. });
  69. testingLibraryUtilUsage.forEach(function (_a) {
  70. var node = _a.node, name = _a.name;
  71. var references = node_utils_1.getVariableReferences(context, node.parent.parent);
  72. if (references &&
  73. references.length === 0 &&
  74. !node_utils_1.isAwaited(node.parent.parent) &&
  75. !node_utils_1.isPromiseResolved(node) &&
  76. !isInPromiseAll(node)) {
  77. context.report({
  78. node: node,
  79. messageId: 'awaitAsyncUtil',
  80. data: {
  81. name: name,
  82. },
  83. });
  84. }
  85. else {
  86. for (var _i = 0, references_1 = references; _i < references_1.length; _i++) {
  87. var reference = references_1[_i];
  88. var referenceNode = reference.identifier;
  89. if (!node_utils_1.isAwaited(referenceNode.parent) &&
  90. !node_utils_1.isPromiseResolved(referenceNode)) {
  91. context.report({
  92. node: node,
  93. messageId: 'awaitAsyncUtil',
  94. data: {
  95. name: name,
  96. },
  97. });
  98. break;
  99. }
  100. }
  101. }
  102. });
  103. },
  104. _a;
  105. },
  106. });