no-alias-methods.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. const jestFnCall = (0, _utils.parseJestFnCall)(node, context);
  41. if ((jestFnCall === null || jestFnCall === void 0 ? void 0 : jestFnCall.type) !== 'expect') {
  42. return;
  43. }
  44. const {
  45. matcher
  46. } = jestFnCall;
  47. const alias = (0, _utils.getAccessorValue)(matcher);
  48. if (alias in methodNames) {
  49. const canonical = methodNames[alias];
  50. context.report({
  51. messageId: 'replaceAlias',
  52. data: {
  53. alias,
  54. canonical
  55. },
  56. node: matcher,
  57. fix: fixer => [(0, _utils.replaceAccessorFixer)(fixer, matcher, canonical)]
  58. });
  59. }
  60. }
  61. };
  62. }
  63. });
  64. exports.default = _default;