prefer-expect-resolves.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. var _default = (0, _utils.createRule)({
  9. name: __filename,
  10. meta: {
  11. docs: {
  12. category: 'Best Practices',
  13. description: 'Prefer `await expect(...).resolves` over `expect(await ...)` syntax',
  14. recommended: false
  15. },
  16. fixable: 'code',
  17. messages: {
  18. expectResolves: 'Use `await expect(...).resolves instead.'
  19. },
  20. schema: [],
  21. type: 'suggestion'
  22. },
  23. defaultOptions: [],
  24. create: context => ({
  25. CallExpression(node) {
  26. const [awaitNode] = node.arguments;
  27. if ((0, _utils.isExpectCall)(node) && (awaitNode === null || awaitNode === void 0 ? void 0 : awaitNode.type) === _experimentalUtils.AST_NODE_TYPES.AwaitExpression) {
  28. context.report({
  29. node: node.arguments[0],
  30. messageId: 'expectResolves',
  31. fix(fixer) {
  32. return [fixer.insertTextBefore(node, 'await '), fixer.removeRange([awaitNode.range[0], awaitNode.argument.range[0]]), fixer.insertTextAfter(node, '.resolves')];
  33. }
  34. });
  35. }
  36. }
  37. })
  38. });
  39. exports.default = _default;