unbound-method.js 2.6 KB

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