unbound-method.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _utils = require("./utils");
  7. const toThrowMatchers = ['toThrow', 'toThrowError', 'toThrowErrorMatchingSnapshot', 'toThrowErrorMatchingInlineSnapshot'];
  8. const isJestExpectToThrowCall = node => {
  9. if (!(0, _utils.isExpectCall)(node)) {
  10. return false;
  11. }
  12. const {
  13. matcher
  14. } = (0, _utils.parseExpectCall)(node);
  15. if (!matcher) {
  16. return false;
  17. }
  18. return !toThrowMatchers.includes(matcher.name);
  19. };
  20. const baseRule = (() => {
  21. try {
  22. // eslint-disable-next-line @typescript-eslint/no-require-imports
  23. const TSESLintPlugin = require('@typescript-eslint/eslint-plugin');
  24. return TSESLintPlugin.rules['unbound-method'];
  25. } catch (e) {
  26. const error = e;
  27. if (error.code === 'MODULE_NOT_FOUND') {
  28. return null;
  29. }
  30. throw error;
  31. }
  32. })();
  33. const tryCreateBaseRule = context => {
  34. try {
  35. return baseRule === null || baseRule === void 0 ? void 0 : baseRule.create(context);
  36. } catch {
  37. return null;
  38. }
  39. };
  40. const DEFAULT_MESSAGE = 'This rule requires `@typescript-eslint/eslint-plugin`';
  41. var _default = (0, _utils.createRule)({
  42. defaultOptions: [{
  43. ignoreStatic: false
  44. }],
  45. ...baseRule,
  46. name: __filename,
  47. meta: {
  48. messages: {
  49. unbound: DEFAULT_MESSAGE,
  50. unboundWithoutThisAnnotation: DEFAULT_MESSAGE
  51. },
  52. schema: [],
  53. type: 'problem',
  54. ...(baseRule === null || baseRule === void 0 ? void 0 : baseRule.meta),
  55. docs: {
  56. category: 'Best Practices',
  57. description: 'Enforces unbound methods are called with their expected scope',
  58. requiresTypeChecking: true,
  59. ...(baseRule === null || baseRule === void 0 ? void 0 : baseRule.meta.docs),
  60. recommended: false
  61. }
  62. },
  63. create(context) {
  64. const baseSelectors = tryCreateBaseRule(context);
  65. if (!baseSelectors) {
  66. return {};
  67. }
  68. let inExpectToThrowCall = false;
  69. return { ...baseSelectors,
  70. CallExpression(node) {
  71. inExpectToThrowCall = isJestExpectToThrowCall(node);
  72. },
  73. 'CallExpression:exit'(node) {
  74. if (inExpectToThrowCall && isJestExpectToThrowCall(node)) {
  75. inExpectToThrowCall = false;
  76. }
  77. },
  78. MemberExpression(node) {
  79. var _baseSelectors$Member;
  80. if (inExpectToThrowCall) {
  81. return;
  82. }
  83. (_baseSelectors$Member = baseSelectors.MemberExpression) === null || _baseSelectors$Member === void 0 ? void 0 : _baseSelectors$Member.call(baseSelectors, node);
  84. }
  85. };
  86. }
  87. });
  88. exports.default = _default;