Expected 'this' to be used by class method 'parseValue'. Open
parseValue(rows = []) {
- Read upRead up
- Exclude checks
title: class-methods-use-this ruletype: suggestion furtherreading: - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/static
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()
)
It's possible to have a class method which doesn't use this
, such as:
class A {
constructor() {
this.a = "hi";
}
print() {
console.log(this.a);
}
sayHi() {
console.log("hi");
}
}
let a = new A();
a.sayHi(); // => "hi"
In the example above, the sayHi
method doesn't use this
, so we can make it a static method:
class A {
constructor() {
this.a = "hi";
}
print() {
console.log(this.a);
}
static sayHi() {
console.log("hi");
}
}
A.sayHi(); // => "hi"
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
Rule Details
This rule is aimed to flag class methods that do not use this
.
Examples of incorrect code for this rule:
::: incorrect
/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/
class A {
foo() {
console.log("Hello World"); /*error Expected 'this' to be used by class method 'foo'.*/
}
}
:::
Examples of correct code for this rule:
::: correct
/*eslint class-methods-use-this: "error"*/
/*eslint-env es6*/
class A {
foo() {
this.bar = "Hello World"; // OK, this is used
}
}
class A {
constructor() {
// OK. constructor is exempt
}
}
class A {
static foo() {
// OK. static methods aren't expected to use this.
}
static {
// OK. static blocks are exempt.
}
}
:::
Options
This rule has two options:
-
"exceptMethods"
allows specified method names to be ignored with this rule. -
"enforceForClassFields"
enforces that functions used as instance field initializers utilizethis
. (default:true
)
exceptMethods
"class-methods-use-this": [<enabled>, { "exceptMethods": [<...exceptions>] }]</enabled>
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.
Examples of incorrect code for this rule when used without "exceptMethods"
:
::: incorrect
/*eslint class-methods-use-this: "error"*/
class A {
foo() {
}
}
:::
Examples of correct code for this rule when used with exceptMethods:
::: correct
/*eslint class-methods-use-this: ["error", { "exceptMethods": ["foo", "#bar"] }] */
class A {
foo() {
}
#bar() {
}
}
:::
enforceForClassFields
"class-methods-use-this": [<enabled>, { "enforceForClassFields": true | false }]</enabled>
The enforceForClassFields
option enforces that arrow functions and function expressions used as instance field initializers utilize this
. (default: true
)
Examples of incorrect code for this rule with the { "enforceForClassFields": true }
option (default):
::: incorrect
/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
class A {
foo = () => {}
}
:::
Examples of correct code for this rule with the { "enforceForClassFields": true }
option (default):
::: correct
/*eslint class-methods-use-this: ["error", { "enforceForClassFields": true }] */
class A {
foo = () => {this;}
}
:::
Examples of correct code for this rule with the { "enforceForClassFields": false }
option:
::: correct
/*eslint class-methods-use-this: ["error", { "enforceForClassFields": false }] */
class A {
foo = () => {}
}
::: Source: http://eslint.org/docs/rules/
Assignment to property of function parameter 'result'. Open
result[keys[index]] = values;
- Read upRead up
- Exclude checks
title: no-param-reassign ruletype: suggestion furtherreading:
- https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/
Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments
object when not in strict mode (see When Not To Use It below). Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.
This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.
Rule Details
This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.
Examples of incorrect code for this rule:
::: incorrect
/*eslint no-param-reassign: "error"*/
function foo(bar) {
bar = 13;
}
function foo(bar) {
bar++;
}
function foo(bar) {
for (bar in baz) {}
}
function foo(bar) {
for (bar of baz) {}
}
:::
Examples of correct code for this rule:
::: correct
/*eslint no-param-reassign: "error"*/
function foo(bar) {
var baz = bar;
}
:::
Options
This rule takes one option, an object, with a boolean property "props"
, and arrays "ignorePropertyModificationsFor"
and "ignorePropertyModificationsForRegex"
. "props"
is false
by default. If "props"
is set to true
, this rule warns against the modification of parameter properties unless they're included in "ignorePropertyModificationsFor"
or "ignorePropertyModificationsForRegex"
, which is an empty array by default.
props
Examples of correct code for the default { "props": false }
option:
::: correct
/*eslint no-param-reassign: ["error", { "props": false }]*/
function foo(bar) {
bar.prop = "value";
}
function foo(bar) {
delete bar.aaa;
}
function foo(bar) {
bar.aaa++;
}
function foo(bar) {
for (bar.aaa in baz) {}
}
function foo(bar) {
for (bar.aaa of baz) {}
}
:::
Examples of incorrect code for the { "props": true }
option:
::: incorrect
/*eslint no-param-reassign: ["error", { "props": true }]*/
function foo(bar) {
bar.prop = "value";
}
function foo(bar) {
delete bar.aaa;
}
function foo(bar) {
bar.aaa++;
}
function foo(bar) {
for (bar.aaa in baz) {}
}
function foo(bar) {
for (bar.aaa of baz) {}
}
:::
Examples of correct code for the { "props": true }
option with "ignorePropertyModificationsFor"
set:
::: correct
/*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsFor": ["bar"] }]*/
function foo(bar) {
bar.prop = "value";
}
function foo(bar) {
delete bar.aaa;
}
function foo(bar) {
bar.aaa++;
}
function foo(bar) {
for (bar.aaa in baz) {}
}
function foo(bar) {
for (bar.aaa of baz) {}
}
:::
Examples of correct code for the { "props": true }
option with "ignorePropertyModificationsForRegex"
set:
::: correct
/*eslint no-param-reassign: ["error", { "props": true, "ignorePropertyModificationsForRegex": ["^bar"] }]*/
function foo(barVar) {
barVar.prop = "value";
}
function foo(barrito) {
delete barrito.aaa;
}
function foo(bar_) {
bar_.aaa++;
}
function foo(barBaz) {
for (barBaz.aaa in baz) {}
}
function foo(barBaz) {
for (barBaz.aaa of baz) {}
}
:::
When Not To Use It
If you want to allow assignment to function parameters, then you can safely disable this rule.
Strict mode code doesn't sync indices of the arguments object with each parameter binding. Therefore, this rule is not necessary to protect against arguments object mutation in ESM modules or other strict mode functions. Source: http://eslint.org/docs/rules/
Export statements should appear at the end of the file Open
export default class Nested extends Component {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
parseValue should be placed after onChange Open
parseValue(rows = []) {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
JSX props should not use .bind() Open
return <MarkDown onChange={this.onChange.bind(this)} value={this.state.value} />;
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "onChange" is not required, but has no corresponding defaultProps declaration. Open
onChange: PropTypes.func,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Prop type "array" is forbidden Open
value: PropTypes.array,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Definition for rule 'node/no-restricted-import' was not found. Open
import PropTypes from 'prop-types';
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Prefer named exports. Open
export default class Nested extends Component {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "value" is not required, but has no corresponding defaultProps declaration. Open
value: PropTypes.array,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/