prefer-mock-promise-shorthand.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("@typescript-eslint/utils");
  7. var _utils2 = require("./utils");
  8. const withOnce = (name, addOnce) => {
  9. return `${name}${addOnce ? 'Once' : ''}`;
  10. };
  11. const findSingleReturnArgumentNode = fnNode => {
  12. var _fnNode$body$body$;
  13. if (fnNode.body.type !== _utils.AST_NODE_TYPES.BlockStatement) {
  14. return fnNode.body;
  15. }
  16. if (((_fnNode$body$body$ = fnNode.body.body[0]) === null || _fnNode$body$body$ === void 0 ? void 0 : _fnNode$body$body$.type) === _utils.AST_NODE_TYPES.ReturnStatement) {
  17. return fnNode.body.body[0].argument;
  18. }
  19. return null;
  20. };
  21. var _default = (0, _utils2.createRule)({
  22. name: __filename,
  23. meta: {
  24. docs: {
  25. category: 'Best Practices',
  26. description: 'Prefer mock resolved/rejected shorthands for promises',
  27. recommended: false
  28. },
  29. messages: {
  30. useMockShorthand: 'Prefer {{ replacement }}'
  31. },
  32. schema: [],
  33. type: 'suggestion',
  34. fixable: 'code'
  35. },
  36. defaultOptions: [],
  37. create(context) {
  38. const report = (property, isOnce, outerArgNode, innerArgNode = outerArgNode) => {
  39. if ((innerArgNode === null || innerArgNode === void 0 ? void 0 : innerArgNode.type) !== _utils.AST_NODE_TYPES.CallExpression) {
  40. return;
  41. }
  42. const argName = (0, _utils2.getNodeName)(innerArgNode);
  43. if (argName !== 'Promise.resolve' && argName !== 'Promise.reject') {
  44. return;
  45. }
  46. const replacement = withOnce(argName.endsWith('reject') ? 'mockRejectedValue' : 'mockResolvedValue', isOnce);
  47. context.report({
  48. node: property,
  49. messageId: 'useMockShorthand',
  50. data: {
  51. replacement
  52. },
  53. fix(fixer) {
  54. const sourceCode = context.getSourceCode(); // there shouldn't be more than one argument, but if there is don't try
  55. // fixing since we have no idea what to do with the extra arguments
  56. if (innerArgNode.arguments.length > 1) {
  57. return null;
  58. }
  59. return [fixer.replaceText(property, replacement), fixer.replaceText(outerArgNode, // the value argument for both Promise methods is optional,
  60. // whereas for Jest they're required so use an explicit undefined
  61. // if no argument is being passed to the call we're replacing
  62. innerArgNode.arguments.length === 1 ? sourceCode.getText(innerArgNode.arguments[0]) : 'undefined')];
  63. }
  64. });
  65. };
  66. return {
  67. CallExpression(node) {
  68. if (node.callee.type !== _utils.AST_NODE_TYPES.MemberExpression || !(0, _utils2.isSupportedAccessor)(node.callee.property) || node.arguments.length === 0) {
  69. return;
  70. }
  71. const mockFnName = (0, _utils2.getAccessorValue)(node.callee.property);
  72. const isOnce = mockFnName.endsWith('Once');
  73. if (mockFnName === withOnce('mockReturnValue', isOnce)) {
  74. report(node.callee.property, isOnce, node.arguments[0]);
  75. } else if (mockFnName === withOnce('mockImplementation', isOnce)) {
  76. const [arg] = node.arguments;
  77. if (!(0, _utils2.isFunction)(arg) || arg.params.length !== 0) {
  78. return;
  79. }
  80. report(node.callee.property, isOnce, arg, findSingleReturnArgumentNode(arg));
  81. }
  82. }
  83. };
  84. }
  85. });
  86. exports.default = _default;