no-alias-methods.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. var _default = (0, _utils.createRule)({
  8. name: __filename,
  9. meta: {
  10. docs: {
  11. category: 'Best Practices',
  12. description: 'Disallow alias methods',
  13. recommended: false
  14. },
  15. messages: {
  16. replaceAlias: `Replace {{ alias }}() with its canonical name of {{ canonical }}()`
  17. },
  18. fixable: 'code',
  19. type: 'suggestion',
  20. schema: []
  21. },
  22. defaultOptions: [],
  23. create(context) {
  24. // map of jest matcher aliases & their canonical names
  25. const methodNames = {
  26. toBeCalled: 'toHaveBeenCalled',
  27. toBeCalledTimes: 'toHaveBeenCalledTimes',
  28. toBeCalledWith: 'toHaveBeenCalledWith',
  29. lastCalledWith: 'toHaveBeenLastCalledWith',
  30. nthCalledWith: 'toHaveBeenNthCalledWith',
  31. toReturn: 'toHaveReturned',
  32. toReturnTimes: 'toHaveReturnedTimes',
  33. toReturnWith: 'toHaveReturnedWith',
  34. lastReturnedWith: 'toHaveLastReturnedWith',
  35. nthReturnedWith: 'toHaveNthReturnedWith',
  36. toThrowError: 'toThrow'
  37. };
  38. return {
  39. CallExpression(node) {
  40. if (!(0, _utils.isExpectCall)(node)) {
  41. return;
  42. }
  43. const {
  44. matcher
  45. } = (0, _utils.parseExpectCall)(node);
  46. if (!matcher) {
  47. return;
  48. }
  49. const alias = matcher.name;
  50. if (alias in methodNames) {
  51. const canonical = methodNames[alias];
  52. context.report({
  53. messageId: 'replaceAlias',
  54. data: {
  55. alias,
  56. canonical
  57. },
  58. node: matcher.node.property,
  59. fix: fixer => [fixer.replaceText(matcher.node.property, canonical)]
  60. });
  61. }
  62. }
  63. };
  64. }
  65. });
  66. exports.default = _default;