@@ -106,14 +106,16 @@ class C {
106
106
107
107
## Options
108
108
109
- This rule has two options:
109
+ This rule has four options:
110
110
111
111
* ` "exceptMethods" ` allows specified method names to be ignored with this rule.
112
112
* ` "enforceForClassFields" ` enforces that functions used as instance field initializers utilize ` this ` . (default: ` true ` )
113
+ * ` "ignoreOverrideMethods" ` ignores members that are marked with the ` override ` modifier. (TypeScript only, default: ` false ` )
114
+ * ` "ignoreClassesWithImplements" ` ignores class members that are defined within a class that ` implements ` an interface. (TypeScript only)
113
115
114
116
### exceptMethods
115
117
116
- ``` js
118
+ ``` ts
117
119
" class-methods-use-this" : [< enabled > , { " exceptMethods" : [< ... exceptions > ] }]
118
120
```
119
121
@@ -153,7 +155,7 @@ class A {
153
155
154
156
### enforceForClassFields
155
157
156
- ``` js
158
+ ``` ts
157
159
" class-methods-use-this" : [< enabled > , { " enforceForClassFields" : true | false }]
158
160
```
159
161
@@ -200,3 +202,164 @@ class A {
200
202
```
201
203
202
204
:::
205
+
206
+ ### ignoreOverrideMethods
207
+
208
+ ``` ts
209
+ " class-methods-use-this" : [< enabled > , { " ignoreOverrideMethods" : true | false }]
210
+ ```
211
+
212
+ The ` ignoreOverrideMethods ` option ignores members that are marked with the ` override ` modifier. (default: ` false ` )
213
+
214
+ Examples of ** incorrect** TypeScript code for this rule with the ` { "ignoreOverrideMethods": false } ` option (default):
215
+
216
+ ::: incorrect
217
+
218
+ ``` ts
219
+ /* eslint class-methods-use-this: ["error", { "ignoreOverrideMethods": false }] */
220
+
221
+ abstract class Base {
222
+ abstract method(): void ;
223
+ abstract property: () => void ;
224
+ }
225
+
226
+ class Derived extends Base {
227
+ override method() {}
228
+ override property = () => {};
229
+ }
230
+ ```
231
+
232
+ :::
233
+
234
+ Examples of ** correct** TypeScript code for this rule with the ` { "ignoreOverrideMethods": false } ` option (default):
235
+
236
+ ::: correct
237
+
238
+ ``` ts
239
+ /* eslint class-methods-use-this: ["error", { "ignoreOverrideMethods": false }] */
240
+
241
+ abstract class Base {
242
+ abstract method(): void ;
243
+ abstract property: () => void ;
244
+ }
245
+
246
+ class Derived extends Base {
247
+ override method() {
248
+ this .foo = " Hello World" ;
249
+ };
250
+ override property = () => {
251
+ this ;
252
+ };
253
+ }
254
+ ```
255
+
256
+ :::
257
+
258
+ Examples of ** correct** TypeScript code for this rule with the ` { "ignoreOverrideMethods": true } ` option:
259
+
260
+ ::: correct
261
+
262
+ ``` ts
263
+ /* eslint class-methods-use-this: ["error", { "ignoreOverrideMethods": true }] */
264
+
265
+ abstract class Base {
266
+ abstract method(): void ;
267
+ abstract property: () => void ;
268
+ }
269
+
270
+ class Derived extends Base {
271
+ override method() {}
272
+ override property = () => {};
273
+ }
274
+ ```
275
+
276
+ :::
277
+
278
+ ### ignoreClassesWithImplements
279
+
280
+ ``` ts
281
+ " class-methods-use-this" : [< enabled > , { " ignoreClassesWithImplements" : " all" | " public-fields" }]
282
+ ```
283
+
284
+ The ` ignoreClassesWithImplements ` ignores class members that are defined within a class that ` implements ` an interface. The option accepts two possible values:
285
+
286
+ * ` "all" ` - Ignores all classes that implement interfaces
287
+ * ` "public-fields" ` - Only ignores public fields in classes that implement interfaces
288
+
289
+ Examples of ** incorrect** TypeScript code for this rule with the ` { "ignoreClassesWithImplements": "all" } ` :
290
+
291
+ ::: incorrect
292
+
293
+ ``` ts
294
+ /* eslint class-methods-use-this: ["error", { "ignoreClassesWithImplements": "all" }] */
295
+
296
+ class Standalone {
297
+ method() {}
298
+ property = () => {};
299
+ }
300
+ ```
301
+
302
+ :::
303
+
304
+ Examples of ** correct** TypeScript code for this rule with the ` { "ignoreClassesWithImplements": "all" } ` option:
305
+
306
+ ::: correct
307
+
308
+ ``` ts
309
+ /* eslint class-methods-use-this: ["error", { "ignoreClassesWithImplements": "all" }] */
310
+
311
+ interface Base {
312
+ method(): void ;
313
+ }
314
+
315
+ class Derived implements Base {
316
+ method() {}
317
+ property = () => {};
318
+ }
319
+ ```
320
+
321
+ :::
322
+
323
+ Examples of ** incorrect** TypeScript code for this rule with the ` { "ignoreClassesWithImplements": "public-fields" } ` option:
324
+
325
+ ::: incorrect
326
+
327
+ ``` ts
328
+ /* eslint class-methods-use-this: ["error", { "ignoreClassesWithImplements": "public-fields" }] */
329
+
330
+ interface Base {
331
+ method(): void ;
332
+ }
333
+
334
+ class Derived implements Base {
335
+ method() {}
336
+ property = () => {};
337
+
338
+ private privateMethod() {}
339
+ private privateProperty = () => {};
340
+
341
+ protected protectedMethod() {}
342
+ protected protectedProperty = () => {};
343
+ }
344
+ ```
345
+
346
+ :::
347
+
348
+ Examples of ** correct** TypeScript code for this rule with the ` { "ignoreClassesWithImplements": "public-fields" } ` option:
349
+
350
+ ::: correct
351
+
352
+ ``` ts
353
+ /* eslint class-methods-use-this: ["error", { "ignoreClassesWithImplements": "public-fields" }] */
354
+
355
+ interface Base {
356
+ method(): void ;
357
+ }
358
+
359
+ class Derived implements Base {
360
+ method() {}
361
+ property = () => {};
362
+ }
363
+ ```
364
+
365
+ :::
0 commit comments