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