|
| 1 | +import type { TSESTree } from '@typescript-eslint/utils'; |
| 2 | + |
| 3 | +import { AST_NODE_TYPES } from '@typescript-eslint/utils'; |
| 4 | + |
| 5 | +import { createRule, getNameFromMember, getParserServices } from '../util'; |
| 6 | + |
| 7 | +type Method = TSESTree.MethodDefinition | TSESTree.TSMethodSignature; |
| 8 | + |
| 9 | +type GetMethod = { |
| 10 | + kind: 'get'; |
| 11 | + returnType: TSESTree.TSTypeAnnotation; |
| 12 | +} & Method; |
| 13 | + |
| 14 | +type GetMethodRaw = { |
| 15 | + returnType: TSESTree.TSTypeAnnotation | undefined; |
| 16 | +} & GetMethod; |
| 17 | + |
| 18 | +type SetMethod = { kind: 'set'; params: [TSESTree.Node] } & Method; |
| 19 | + |
| 20 | +interface MethodPair { |
| 21 | + get?: GetMethod; |
| 22 | + set?: SetMethod; |
| 23 | +} |
| 24 | + |
| 25 | +export default createRule({ |
| 26 | + name: 'related-getter-setter-pairs', |
| 27 | + meta: { |
| 28 | + type: 'problem', |
| 29 | + docs: { |
| 30 | + description: |
| 31 | + 'Enforce that `get()` types should be assignable to their equivalent `set()` type', |
| 32 | + recommended: 'strict', |
| 33 | + requiresTypeChecking: true, |
| 34 | + }, |
| 35 | + messages: { |
| 36 | + mismatch: |
| 37 | + '`get()` type should be assignable to its equivalent `set()` type.', |
| 38 | + }, |
| 39 | + schema: [], |
| 40 | + }, |
| 41 | + defaultOptions: [], |
| 42 | + create(context) { |
| 43 | + const services = getParserServices(context); |
| 44 | + const checker = services.program.getTypeChecker(); |
| 45 | + const methodPairsStack: Map<string, MethodPair>[] = []; |
| 46 | + |
| 47 | + function addPropertyNode( |
| 48 | + member: GetMethod | SetMethod, |
| 49 | + inner: TSESTree.Node, |
| 50 | + kind: 'get' | 'set', |
| 51 | + ): void { |
| 52 | + const methodPairs = methodPairsStack[methodPairsStack.length - 1]; |
| 53 | + const { name } = getNameFromMember(member, context.sourceCode); |
| 54 | + |
| 55 | + methodPairs.set(name, { |
| 56 | + ...methodPairs.get(name), |
| 57 | + [kind]: inner, |
| 58 | + }); |
| 59 | + } |
| 60 | + |
| 61 | + return { |
| 62 | + ':matches(ClassBody, TSInterfaceBody, TSTypeLiteral):exit'(): void { |
| 63 | + const methodPairs = methodPairsStack[methodPairsStack.length - 1]; |
| 64 | + |
| 65 | + for (const pair of methodPairs.values()) { |
| 66 | + if (!pair.get || !pair.set) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + const getter = pair.get; |
| 71 | + |
| 72 | + const getType = services.getTypeAtLocation(getter); |
| 73 | + const setType = services.getTypeAtLocation(pair.set.params[0]); |
| 74 | + |
| 75 | + if (!checker.isTypeAssignableTo(getType, setType)) { |
| 76 | + context.report({ |
| 77 | + node: getter.returnType.typeAnnotation, |
| 78 | + messageId: 'mismatch', |
| 79 | + }); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + methodPairsStack.pop(); |
| 84 | + }, |
| 85 | + ':matches(MethodDefinition, TSMethodSignature)[kind=get]'( |
| 86 | + node: GetMethodRaw, |
| 87 | + ): void { |
| 88 | + const getter = getMethodFromNode(node); |
| 89 | + |
| 90 | + if (getter.returnType) { |
| 91 | + addPropertyNode(node, getter, 'get'); |
| 92 | + } |
| 93 | + }, |
| 94 | + ':matches(MethodDefinition, TSMethodSignature)[kind=set]'( |
| 95 | + node: SetMethod, |
| 96 | + ): void { |
| 97 | + const setter = getMethodFromNode(node); |
| 98 | + |
| 99 | + if (setter.params.length === 1) { |
| 100 | + addPropertyNode(node, setter, 'set'); |
| 101 | + } |
| 102 | + }, |
| 103 | + |
| 104 | + 'ClassBody, TSInterfaceBody, TSTypeLiteral'(): void { |
| 105 | + methodPairsStack.push(new Map()); |
| 106 | + }, |
| 107 | + }; |
| 108 | + }, |
| 109 | +}); |
| 110 | + |
| 111 | +function getMethodFromNode(node: GetMethodRaw | SetMethod) { |
| 112 | + return node.type === AST_NODE_TYPES.TSMethodSignature ? node : node.value; |
| 113 | +} |
0 commit comments