|
| 1 | +import type { TSESTree } from '@typescript-eslint/utils'; |
| 2 | +import { AST_NODE_TYPES } from '@typescript-eslint/utils'; |
| 3 | + |
| 4 | +import * as util from '../util'; |
| 5 | + |
| 6 | +type Options = [ |
| 7 | + { |
| 8 | + exceptMethods?: string[]; |
| 9 | + enforceForClassFields?: boolean; |
| 10 | + ignoreOverrideMethods?: boolean; |
| 11 | + ignoreClassesThatImplementAnInterface?: boolean; |
| 12 | + }, |
| 13 | +]; |
| 14 | +type MessageIds = 'missingThis'; |
| 15 | + |
| 16 | +export default util.createRule<Options, MessageIds>({ |
| 17 | + name: 'class-methods-use-this', |
| 18 | + meta: { |
| 19 | + type: 'suggestion', |
| 20 | + docs: { |
| 21 | + description: 'Enforce that class methods utilize `this`', |
| 22 | + extendsBaseRule: true, |
| 23 | + requiresTypeChecking: false, |
| 24 | + }, |
| 25 | + fixable: 'code', |
| 26 | + hasSuggestions: false, |
| 27 | + schema: [ |
| 28 | + { |
| 29 | + type: 'object', |
| 30 | + properties: { |
| 31 | + exceptMethods: { |
| 32 | + type: 'array', |
| 33 | + description: |
| 34 | + 'Allows specified method names to be ignored with this rule', |
| 35 | + items: { |
| 36 | + type: 'string', |
| 37 | + }, |
| 38 | + }, |
| 39 | + enforceForClassFields: { |
| 40 | + type: 'boolean', |
| 41 | + description: |
| 42 | + 'Enforces that functions used as instance field initializers utilize `this`', |
| 43 | + default: true, |
| 44 | + }, |
| 45 | + ignoreOverrideMethods: { |
| 46 | + type: 'boolean', |
| 47 | + description: 'Ingore members marked with the `override` modifier', |
| 48 | + }, |
| 49 | + ignoreClassesThatImplementAnInterface: { |
| 50 | + type: 'boolean', |
| 51 | + description: |
| 52 | + 'Ignore classes that specifically implement some interface', |
| 53 | + }, |
| 54 | + }, |
| 55 | + additionalProperties: false, |
| 56 | + }, |
| 57 | + ], |
| 58 | + messages: { |
| 59 | + missingThis: "Expected 'this' to be used by class {{name}}.", |
| 60 | + }, |
| 61 | + }, |
| 62 | + defaultOptions: [ |
| 63 | + { |
| 64 | + enforceForClassFields: true, |
| 65 | + exceptMethods: [], |
| 66 | + ignoreClassesThatImplementAnInterface: false, |
| 67 | + ignoreOverrideMethods: false, |
| 68 | + }, |
| 69 | + ], |
| 70 | + create( |
| 71 | + context, |
| 72 | + [ |
| 73 | + { |
| 74 | + enforceForClassFields, |
| 75 | + exceptMethods: exceptMethodsRaw, |
| 76 | + ignoreClassesThatImplementAnInterface, |
| 77 | + ignoreOverrideMethods, |
| 78 | + }, |
| 79 | + ], |
| 80 | + ) { |
| 81 | + const exceptMethods = new Set(exceptMethodsRaw); |
| 82 | + type Stack = |
| 83 | + | { |
| 84 | + member: null; |
| 85 | + class: null; |
| 86 | + parent: Stack | undefined; |
| 87 | + usesThis: boolean; |
| 88 | + } |
| 89 | + | { |
| 90 | + member: TSESTree.MethodDefinition | TSESTree.PropertyDefinition; |
| 91 | + class: TSESTree.ClassDeclaration | TSESTree.ClassExpression; |
| 92 | + parent: Stack | undefined; |
| 93 | + usesThis: boolean; |
| 94 | + }; |
| 95 | + let stack: Stack | undefined; |
| 96 | + |
| 97 | + const sourceCode = context.getSourceCode(); |
| 98 | + |
| 99 | + function pushContext( |
| 100 | + member?: TSESTree.MethodDefinition | TSESTree.PropertyDefinition, |
| 101 | + ): void { |
| 102 | + if (member?.parent.type === AST_NODE_TYPES.ClassBody) { |
| 103 | + stack = { |
| 104 | + member, |
| 105 | + class: member.parent.parent as |
| 106 | + | TSESTree.ClassDeclaration |
| 107 | + | TSESTree.ClassExpression, |
| 108 | + usesThis: false, |
| 109 | + parent: stack, |
| 110 | + }; |
| 111 | + } else { |
| 112 | + stack = { |
| 113 | + member: null, |
| 114 | + class: null, |
| 115 | + usesThis: false, |
| 116 | + parent: stack, |
| 117 | + }; |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + function enterFunction( |
| 122 | + node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression, |
| 123 | + ): void { |
| 124 | + if ( |
| 125 | + node.parent.type === AST_NODE_TYPES.MethodDefinition || |
| 126 | + node.parent.type === AST_NODE_TYPES.PropertyDefinition |
| 127 | + ) { |
| 128 | + pushContext(node.parent); |
| 129 | + } else { |
| 130 | + pushContext(); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + /** |
| 135 | + * Pop `this` used flag from the stack. |
| 136 | + */ |
| 137 | + function popContext(): Stack | undefined { |
| 138 | + const oldStack = stack; |
| 139 | + stack = stack?.parent; |
| 140 | + return oldStack; |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Check if the node is an instance method not excluded by config |
| 145 | + */ |
| 146 | + function isIncludedInstanceMethod( |
| 147 | + node: NonNullable<Stack['member']>, |
| 148 | + ): node is NonNullable<Stack['member']> { |
| 149 | + if ( |
| 150 | + node.static || |
| 151 | + (node.type === AST_NODE_TYPES.MethodDefinition && |
| 152 | + node.kind === 'constructor') || |
| 153 | + (node.type === AST_NODE_TYPES.PropertyDefinition && |
| 154 | + !enforceForClassFields) |
| 155 | + ) { |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + if (node.computed || exceptMethods.size === 0) { |
| 160 | + return true; |
| 161 | + } |
| 162 | + |
| 163 | + const hashIfNeeded = |
| 164 | + node.key.type === AST_NODE_TYPES.PrivateIdentifier ? '#' : ''; |
| 165 | + const name = |
| 166 | + node.key.type === AST_NODE_TYPES.Literal |
| 167 | + ? util.getStaticStringValue(node.key) |
| 168 | + : node.key.name || ''; |
| 169 | + |
| 170 | + return !exceptMethods.has(hashIfNeeded + (name ?? '')); |
| 171 | + } |
| 172 | + |
| 173 | + /** |
| 174 | + * Checks if we are leaving a function that is a method, and reports if 'this' has not been used. |
| 175 | + * Static methods and the constructor are exempt. |
| 176 | + * Then pops the context off the stack. |
| 177 | + */ |
| 178 | + function exitFunction( |
| 179 | + node: TSESTree.ArrowFunctionExpression | TSESTree.FunctionExpression, |
| 180 | + ): void { |
| 181 | + const stackContext = popContext(); |
| 182 | + if ( |
| 183 | + stackContext?.member == null || |
| 184 | + stackContext.class == null || |
| 185 | + stackContext.usesThis || |
| 186 | + (ignoreOverrideMethods && stackContext.member.override) || |
| 187 | + (ignoreClassesThatImplementAnInterface && |
| 188 | + stackContext.class.implements != null) |
| 189 | + ) { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + if (isIncludedInstanceMethod(stackContext.member)) { |
| 194 | + context.report({ |
| 195 | + node, |
| 196 | + loc: util.getFunctionHeadLoc(node, sourceCode), |
| 197 | + messageId: 'missingThis', |
| 198 | + data: { |
| 199 | + name: util.getFunctionNameWithKind(node), |
| 200 | + }, |
| 201 | + }); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + return { |
| 206 | + // function declarations have their own `this` context |
| 207 | + FunctionDeclaration(): void { |
| 208 | + pushContext(); |
| 209 | + }, |
| 210 | + 'FunctionDeclaration:exit'(): void { |
| 211 | + popContext(); |
| 212 | + }, |
| 213 | + |
| 214 | + FunctionExpression(node): void { |
| 215 | + enterFunction(node); |
| 216 | + }, |
| 217 | + 'FunctionExpression:exit'(node): void { |
| 218 | + exitFunction(node); |
| 219 | + }, |
| 220 | + ...(enforceForClassFields |
| 221 | + ? { |
| 222 | + 'PropertyDefinition > ArrowFunctionExpression.value'( |
| 223 | + node: TSESTree.ArrowFunctionExpression, |
| 224 | + ): void { |
| 225 | + enterFunction(node); |
| 226 | + }, |
| 227 | + 'PropertyDefinition > ArrowFunctionExpression.value:exit'( |
| 228 | + node: TSESTree.ArrowFunctionExpression, |
| 229 | + ): void { |
| 230 | + exitFunction(node); |
| 231 | + }, |
| 232 | + } |
| 233 | + : {}), |
| 234 | + |
| 235 | + /* |
| 236 | + * Class field value are implicit functions. |
| 237 | + */ |
| 238 | + 'PropertyDefinition > *.key:exit'(): void { |
| 239 | + pushContext(); |
| 240 | + }, |
| 241 | + 'PropertyDefinition:exit'(): void { |
| 242 | + popContext(); |
| 243 | + }, |
| 244 | + |
| 245 | + /* |
| 246 | + * Class static blocks are implicit functions. They aren't required to use `this`, |
| 247 | + * but we have to push context so that it captures any use of `this` in the static block |
| 248 | + * separately from enclosing contexts, because static blocks have their own `this` and it |
| 249 | + * shouldn't count as used `this` in enclosing contexts. |
| 250 | + */ |
| 251 | + StaticBlock(): void { |
| 252 | + pushContext(); |
| 253 | + }, |
| 254 | + 'StaticBlock:exit'(): void { |
| 255 | + popContext(); |
| 256 | + }, |
| 257 | + |
| 258 | + 'ThisExpression, Super'(): void { |
| 259 | + if (stack) { |
| 260 | + stack.usesThis = true; |
| 261 | + } |
| 262 | + }, |
| 263 | + }; |
| 264 | + }, |
| 265 | +}); |
0 commit comments