Method 'renderActiveShape' has too many statements (12). Maximum allowed is 10. Open
renderActiveShape(props) {
- Read upRead up
- Exclude checks
title: max-statements ruletype: suggestion relatedrules: - complexity - max-depth - max-len - max-lines - max-lines-per-function - max-nested-callbacks
- max-params
The max-statements
rule allows you to specify the maximum number of statements allowed in a function.
function foo() {
var bar = 1; // one statement
var baz = 2; // two statements
var qux = 3; // three statements
}
Rule Details
This rule enforces a maximum number of statements allowed in function blocks.
Options
This rule has a number or object option:
-
"max"
(default10
) enforces a maximum number of statements allows in function blocks
Deprecated: The object property maximum
is deprecated; please use the object property max
instead.
This rule has an object option:
-
"ignoreTopLevelFunctions": true
ignores top-level functions
max
Examples of incorrect code for this rule with the default { "max": 10 }
option:
::: incorrect
/*eslint max-statements: ["error", 10]*/
/*eslint-env es6*/
function foo() {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
var foo11 = 11; // Too many.
}
let foo = () => {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
var foo11 = 11; // Too many.
};
:::
Examples of correct code for this rule with the default { "max": 10 }
option:
::: correct
/*eslint max-statements: ["error", 10]*/
/*eslint-env es6*/
function foo() {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
return function () {
// The number of statements in the inner function does not count toward the
// statement maximum.
return 42;
};
}
let foo = () => {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
return function () {
// The number of statements in the inner function does not count toward the
// statement maximum.
return 42;
};
}
:::
Note that this rule does not apply to class static blocks, and that statements in class static blocks do not count as statements in the enclosing function.
Examples of correct code for this rule with { "max": 2 }
option:
::: correct
/*eslint max-statements: ["error", 2]*/
function foo() {
let one;
let two = class {
static {
let three;
let four;
let five;
if (six) {
let seven;
let eight;
let nine;
}
}
};
}
:::
ignoreTopLevelFunctions
Examples of additional correct code for this rule with the { "max": 10 }, { "ignoreTopLevelFunctions": true }
options:
::: correct
/*eslint max-statements: ["error", 10, { "ignoreTopLevelFunctions": true }]*/
function foo() {
var foo1 = 1;
var foo2 = 2;
var foo3 = 3;
var foo4 = 4;
var foo5 = 5;
var foo6 = 6;
var foo7 = 7;
var foo8 = 8;
var foo9 = 9;
var foo10 = 10;
var foo11 = 11;
}
::: Source: http://eslint.org/docs/rules/
Expected 'this' to be used by class method 'renderActiveShape'. Open
renderActiveShape(props) {
- 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/
Expected 'this' to be used by class method 'getFilteredIndex'. Open
getFilteredIndex(data, index) {
- 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/
Definition for rule 'node/no-restricted-import' was not found. Open
import React, { Component } from 'react';
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Export statements should appear at the end of the file Open
export class RechartsPie extends Component {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Prop type "array" is forbidden Open
data: PropTypes.array,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
JSX props should not use .bind() Open
onClick={this.onIndexClick.bind(this)}
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
onIndexEnter should be placed before getFilteredIndex Open
onIndexEnter(_data, index) {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Do not use Array index in keys Open
<Cell key={index} fill={filteredColors[index]} opacity={0.8} />
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
renderActiveShape should be placed after getFilteredIndex Open
renderActiveShape(props) {
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "data" is not required, but has no corresponding defaultProps declaration. Open
data: PropTypes.array,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
JSX props should not use .bind() Open
onMouseEnter={this.onFullIndexEnter.bind(this)}
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Prefer named exports. Open
export default connect()(RechartsPie);
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
JSX props should not use .bind() Open
onClick={this.onIndexEnter.bind(this)}
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
'data' PropType is defined but prop is never used Open
data: PropTypes.array,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
JSX props should not use .bind() Open
onMouseEnter={this.onIndexEnter.bind(this)}
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/