no-deprecated-functions.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = exports._clearCachedJestVersion = void 0;
  6. var _experimentalUtils = require("@typescript-eslint/experimental-utils");
  7. var _utils = require("./utils");
  8. let cachedJestVersion = null;
  9. /** @internal */
  10. const _clearCachedJestVersion = () => cachedJestVersion = null;
  11. exports._clearCachedJestVersion = _clearCachedJestVersion;
  12. const detectJestVersion = () => {
  13. if (cachedJestVersion) {
  14. return cachedJestVersion;
  15. }
  16. try {
  17. const jestPath = require.resolve('jest/package.json', {
  18. paths: [process.cwd()]
  19. }); // eslint-disable-next-line @typescript-eslint/no-require-imports
  20. const jestPackageJson = require(jestPath);
  21. if (jestPackageJson.version) {
  22. const [majorVersion] = jestPackageJson.version.split('.');
  23. return cachedJestVersion = parseInt(majorVersion, 10);
  24. }
  25. } catch {}
  26. throw new Error('Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly');
  27. };
  28. var _default = (0, _utils.createRule)({
  29. name: __filename,
  30. meta: {
  31. docs: {
  32. category: 'Best Practices',
  33. description: 'Disallow use of deprecated functions',
  34. recommended: 'error'
  35. },
  36. messages: {
  37. deprecatedFunction: '`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`'
  38. },
  39. type: 'suggestion',
  40. schema: [],
  41. fixable: 'code'
  42. },
  43. defaultOptions: [],
  44. create(context) {
  45. var _context$settings, _context$settings$jes;
  46. const jestVersion = ((_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) || detectJestVersion();
  47. const deprecations = { ...(jestVersion >= 15 && {
  48. 'jest.resetModuleRegistry': 'jest.resetModules'
  49. }),
  50. ...(jestVersion >= 17 && {
  51. 'jest.addMatchers': 'expect.extend'
  52. }),
  53. ...(jestVersion >= 21 && {
  54. 'require.requireMock': 'jest.requireMock',
  55. 'require.requireActual': 'jest.requireActual'
  56. }),
  57. ...(jestVersion >= 22 && {
  58. 'jest.runTimersToTime': 'jest.advanceTimersByTime'
  59. }),
  60. ...(jestVersion >= 26 && {
  61. 'jest.genMockFromModule': 'jest.createMockFromModule'
  62. })
  63. };
  64. return {
  65. CallExpression(node) {
  66. if (node.callee.type !== _experimentalUtils.AST_NODE_TYPES.MemberExpression) {
  67. return;
  68. }
  69. const deprecation = (0, _utils.getNodeName)(node);
  70. if (!deprecation || !(deprecation in deprecations)) {
  71. return;
  72. }
  73. const replacement = deprecations[deprecation];
  74. const {
  75. callee
  76. } = node;
  77. context.report({
  78. messageId: 'deprecatedFunction',
  79. data: {
  80. deprecation,
  81. replacement
  82. },
  83. node,
  84. fix(fixer) {
  85. let [name, func] = replacement.split('.');
  86. if (callee.property.type === _experimentalUtils.AST_NODE_TYPES.Literal) {
  87. func = `'${func}'`;
  88. }
  89. return [fixer.replaceText(callee.object, name), fixer.replaceText(callee.property, func)];
  90. }
  91. });
  92. }
  93. };
  94. }
  95. });
  96. exports.default = _default;