1 # Enforce that class methods utilize `this` (class-methods-use-this)
3 If a class method does not use `this`, it can *sometimes* be made into a static function. If you do convert the method into a static function, instances of the class that call that particular method have to be converted to a static call as well (`MyClass.callStaticMethod()`)
5 It's possible to have a class method which doesn't use `this`, such as:
26 In the example above, the `sayHi` method doesn't use `this`, so we can make it a static method:
46 Also note in the above examples that if you switch a method to a static method, *instances* of the class that call the static method (`let a = new A(); a.sayHi();`) have to be updated to being a static call (`A.sayHi();`) instead of having the instance of the *class* call the method
50 This rule is aimed to flag class methods that do not use `this`.
52 Examples of **incorrect** code for this rule:
55 /*eslint class-methods-use-this: "error"*/
60 console.log("Hello World"); /*error Expected 'this' to be used by class method 'foo'.*/
65 Examples of **correct** code for this rule:
68 /*eslint class-methods-use-this: "error"*/
72 this.bar = "Hello World"; // OK, this is used
78 // OK. constructor is exempt
84 // OK. static methods aren't expected to use this.
88 // OK. static blocks are exempt.
95 This rule has two options:
97 * `"exceptMethods"` allows specified method names to be ignored with this rule.
98 * `"enforceForClassFields"` enforces that functions used as instance field initializers utilize `this`. (default: `true`)
103 "class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]
106 The `exceptMethods` option allows you to pass an array of method names for which you would like to ignore warnings. For example, you might have a spec from an external library that requires you to overwrite a method as a regular function (and not as a static method) and does not use `this` inside the function body. In this case, you can add that method to ignore in the warnings.
108 Examples of **incorrect** code for this rule when used without exceptMethods:
111 /*eslint class-methods-use-this: "error"*/
119 Examples of **correct** code for this rule when used with exceptMethods:
122 /*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */
132 ## enforceForClassFields
135 "class-methods-use-this": [<enabled>, { "enforceForClassFields": true | false }]
138 The `enforceForClassFields` option enforces that arrow functions and function expressions used as instance field initializers utilize `this`. (default: `true`)
140 Examples of **incorrect** code for this rule with the `{ "enforceForClassFields": true }` option (default):
143 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
150 Examples of **correct** code for this rule with the `{ "enforceForClassFields": true }` option (default):
153 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
160 Examples of **correct** code for this rule with the `{ "enforceForClassFields": false }` option:
163 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */
172 * [Classes](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes)
173 * [Static Methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static)