no-await-sync-events.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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 = 'no-await-sync-events';
  8. var SYNC_EVENTS_REGEXP = new RegExp("^(" + utils_1.SYNC_EVENTS.join('|') + ")$");
  9. exports.default = experimental_utils_1.ESLintUtils.RuleCreator(utils_1.getDocsUrl)({
  10. name: exports.RULE_NAME,
  11. meta: {
  12. type: 'problem',
  13. docs: {
  14. description: 'Disallow unnecessary `await` for sync events',
  15. category: 'Best Practices',
  16. recommended: 'error',
  17. },
  18. messages: {
  19. noAwaitSyncEvents: '`{{ name }}` does not need `await` operator',
  20. },
  21. fixable: null,
  22. schema: [],
  23. },
  24. defaultOptions: [],
  25. create: function (context) {
  26. var _a;
  27. return _a = {},
  28. _a["AwaitExpression > CallExpression > MemberExpression > Identifier[name=" + SYNC_EVENTS_REGEXP + "]"] = function (node) {
  29. var memberExpression = node.parent;
  30. var methodNode = memberExpression.property;
  31. var callExpression = memberExpression.parent;
  32. var lastArg = callExpression.arguments[callExpression.arguments.length - 1];
  33. var withDelay = node_utils_1.isObjectExpression(lastArg) &&
  34. lastArg.properties.some(function (property) {
  35. return node_utils_1.isProperty(property) &&
  36. node_utils_1.isIdentifier(property.key) &&
  37. property.key.name === 'delay';
  38. });
  39. if (!(node.name === 'userEvent' && ['type', 'keyboard'].includes(methodNode.name) && withDelay)) {
  40. context.report({
  41. node: methodNode,
  42. messageId: 'noAwaitSyncEvents',
  43. data: {
  44. name: node.name + "." + methodNode.name,
  45. },
  46. });
  47. }
  48. },
  49. _a;
  50. },
  51. });