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