Expected 'this' to be used by class method 'handleOver'. Open
handleOver() {}
- 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/
Arrow function should not return assignment. Open
ref={ref => (this.pagesContainer = ref)}
- Read upRead up
- Exclude checks
title: no-return-assign
rule_type: suggestion
One of the interesting, and sometimes confusing, aspects of JavaScript is that assignment can happen at almost any point. Because of this, an errant equals sign can end up causing assignment when the true intent was to do a comparison. This is especially true when using a return
statement. For example:
function doSomething() {
return foo = bar + 2;
}
It is difficult to tell the intent of the return
statement here. It's possible that the function is meant to return the result of bar + 2
, but then why is it assigning to foo
? It's also possible that the intent was to use a comparison operator such as ==
and that this code is an error.
Because of this ambiguity, it's considered a best practice to not use assignment in return
statements.
Rule Details
This rule aims to eliminate assignments from return
statements. As such, it will warn whenever an assignment is found as part of return
.
Options
The rule takes one option, a string, which must contain one of the following values:
-
except-parens
(default): Disallow assignments unless they are enclosed in parentheses. -
always
: Disallow all assignments.
except-parens
This is the default option. It disallows assignments unless they are enclosed in parentheses.
Examples of incorrect code for the default "except-parens"
option:
::: incorrect
/*eslint no-return-assign: "error"*/
function doSomething() {
return foo = bar + 2;
}
function doSomething() {
return foo += 2;
}
const foo = (a, b) => a = b
const bar = (a, b, c) => (a = b, c == b)
function doSomething() {
return foo = bar && foo > 0;
}
:::
Examples of correct code for the default "except-parens"
option:
::: correct
/*eslint no-return-assign: "error"*/
function doSomething() {
return foo == bar + 2;
}
function doSomething() {
return foo === bar + 2;
}
function doSomething() {
return (foo = bar + 2);
}
const foo = (a, b) => (a = b)
const bar = (a, b, c) => ((a = b), c == b)
function doSomething() {
return (foo = bar) && foo > 0;
}
:::
always
This option disallows all assignments in return
statements.
All assignments are treated as problems.
Examples of incorrect code for the "always"
option:
::: incorrect
/*eslint no-return-assign: ["error", "always"]*/
function doSomething() {
return foo = bar + 2;
}
function doSomething() {
return foo += 2;
}
function doSomething() {
return (foo = bar + 2);
}
:::
Examples of correct code for the "always"
option:
::: correct
/*eslint no-return-assign: ["error", "always"]*/
function doSomething() {
return foo == bar + 2;
}
function doSomething() {
return foo === bar + 2;
}
:::
When Not To Use It
If you want to allow the use of assignment operators in a return
statement, then you can safely disable this rule.
Source: http://eslint.org/docs/rules/
Prop type "object" is forbidden Open
doc: PropTypes.object,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Prop type "object" is forbidden Open
references: PropTypes.object,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Prefer named exports. Open
export default Document;
- 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 'app/Viewer/scss/conversion_base.scss';
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
Prop type "object" is forbidden Open
file: PropTypes.object,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "references" is not required, but has no corresponding defaultProps declaration. Open
references: PropTypes.object,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "doc" is not required, but has no corresponding defaultProps declaration. Open
doc: PropTypes.object,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "unsetSelection" is not required, but has no corresponding defaultProps declaration. Open
unsetSelection: PropTypes.func,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "disableTextSelection" is not required, but has no corresponding defaultProps declaration. Open
disableTextSelection: PropTypes.bool,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/
propType "setSelection" is not required, but has no corresponding defaultProps declaration. Open
setSelection: PropTypes.func,
- Read upRead up
- Exclude checks
For more information visit Source: http://eslint.org/docs/rules/