no-deprecated-functions.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 _detectJestVersion = require("./detectJestVersion");
  8. var _utils = require("./utils");
  9. const parseJestVersion = rawVersion => {
  10. if (typeof rawVersion === 'number') {
  11. return rawVersion;
  12. }
  13. const [majorVersion] = rawVersion.split('.');
  14. return parseInt(majorVersion, 10);
  15. };
  16. var _default = (0, _utils.createRule)({
  17. name: __filename,
  18. meta: {
  19. docs: {
  20. category: 'Best Practices',
  21. description: 'Disallow use of deprecated functions',
  22. recommended: 'error'
  23. },
  24. messages: {
  25. deprecatedFunction: '`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`'
  26. },
  27. type: 'suggestion',
  28. schema: [],
  29. fixable: 'code'
  30. },
  31. defaultOptions: [],
  32. create(context) {
  33. var _context$settings, _context$settings$jes;
  34. const jestVersion = parseJestVersion(((_context$settings = context.settings) === null || _context$settings === void 0 ? void 0 : (_context$settings$jes = _context$settings.jest) === null || _context$settings$jes === void 0 ? void 0 : _context$settings$jes.version) || (0, _detectJestVersion.detectJestVersion)());
  35. const deprecations = { ...(jestVersion >= 15 && {
  36. 'jest.resetModuleRegistry': 'jest.resetModules'
  37. }),
  38. ...(jestVersion >= 17 && {
  39. 'jest.addMatchers': 'expect.extend'
  40. }),
  41. ...(jestVersion >= 21 && {
  42. 'require.requireMock': 'jest.requireMock',
  43. 'require.requireActual': 'jest.requireActual'
  44. }),
  45. ...(jestVersion >= 22 && {
  46. 'jest.runTimersToTime': 'jest.advanceTimersByTime'
  47. }),
  48. ...(jestVersion >= 26 && {
  49. 'jest.genMockFromModule': 'jest.createMockFromModule'
  50. })
  51. };
  52. return {
  53. CallExpression(node) {
  54. if (node.callee.type !== _experimentalUtils.AST_NODE_TYPES.MemberExpression) {
  55. return;
  56. }
  57. const deprecation = (0, _utils.getNodeName)(node);
  58. if (!deprecation || !(deprecation in deprecations)) {
  59. return;
  60. }
  61. const replacement = deprecations[deprecation];
  62. const {
  63. callee
  64. } = node;
  65. context.report({
  66. messageId: 'deprecatedFunction',
  67. data: {
  68. deprecation,
  69. replacement
  70. },
  71. node,
  72. fix(fixer) {
  73. let [name, func] = replacement.split('.');
  74. if (callee.property.type === _experimentalUtils.AST_NODE_TYPES.Literal) {
  75. func = `'${func}'`;
  76. }
  77. return [fixer.replaceText(callee.object, name), fixer.replaceText(callee.property, func)];
  78. }
  79. });
  80. }
  81. };
  82. }
  83. });
  84. exports.default = _default;