2 title: class-methods-use-this
6 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
7 - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
11 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()`)
13 It's possible to have a class method which doesn't use `this`, such as:
34 In the example above, the `sayHi` method doesn't use `this`, so we can make it a static method:
54 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
58 This rule is aimed to flag class methods that do not use `this`.
60 Examples of **incorrect** code for this rule:
65 /*eslint class-methods-use-this: "error"*/
70 console.log("Hello World"); /*error Expected 'this' to be used by class method 'foo'.*/
77 Examples of **correct** code for this rule:
82 /*eslint class-methods-use-this: "error"*/
86 this.bar = "Hello World"; // OK, this is used
92 // OK. constructor is exempt
98 // OK. static methods aren't expected to use this.
102 // OK. static blocks are exempt.
111 This rule has two options:
113 * `"exceptMethods"` allows specified method names to be ignored with this rule.
114 * `"enforceForClassFields"` enforces that functions used as instance field initializers utilize `this`. (default: `true`)
119 "class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]
122 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.
124 Examples of **incorrect** code for this rule when used without exceptMethods:
129 /*eslint class-methods-use-this: "error"*/
139 Examples of **correct** code for this rule when used with exceptMethods:
144 /*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */
156 ### enforceForClassFields
159 "class-methods-use-this": [<enabled>, { "enforceForClassFields": true | false }]
162 The `enforceForClassFields` option enforces that arrow functions and function expressions used as instance field initializers utilize `this`. (default: `true`)
164 Examples of **incorrect** code for this rule with the `{ "enforceForClassFields": true }` option (default):
169 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
178 Examples of **correct** code for this rule with the `{ "enforceForClassFields": true }` option (default):
183 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
192 Examples of **correct** code for this rule with the `{ "enforceForClassFields": false }` option:
197 /*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */