2 title: class-methods-use-this
5 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
6 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
10 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()`)
12 It's possible to have a class method which doesn't use `this`, such as:
33 In the example above, the `sayHi` method doesn't use `this`, so we can make it a static method:
53 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
57 This rule is aimed to flag class methods that do not use `this`.
59 Examples of **incorrect** code for this rule:
64 /*eslint class-methods-use-this: "error"*/
69 console.log("Hello World"); /*error Expected 'this' to be used by class method 'foo'.*/
76 Examples of **correct** code for this rule:
81 /*eslint class-methods-use-this: "error"*/
85 this.bar = "Hello World"; // OK, this is used
91 // OK. constructor is exempt
97 // OK. static methods aren't expected to use this.
101 // OK. static blocks are exempt.
110 This rule has two options:
112 * `"exceptMethods"` allows specified method names to be ignored with this rule.
113 * `"enforceForClassFields"` enforces that functions used as instance field initializers utilize `this`. (default: `true`)
118 "class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]
121 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.
123 Examples of **incorrect** code for this rule when used without `"exceptMethods"`:
128 /*eslint class-methods-use-this: "error"*/
138 Examples of **correct** code for this rule when used with exceptMethods:
143 /*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */
155 ### enforceForClassFields
158 "class-methods-use-this": [<enabled>, { "enforceForClassFields": true | false }]
161 The `enforceForClassFields` option enforces that arrow functions and function expressions used as instance field initializers utilize `this`. (default: `true`)
163 Examples of **incorrect** code for this rule with the `{ "enforceForClassFields": true }` option (default):
168 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
177 Examples of **correct** code for this rule with the `{ "enforceForClassFields": true }` option (default):
182 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
191 Examples of **correct** code for this rule with the `{ "enforceForClassFields": false }` option:
196 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */