New rule to warn about the usage of this keyword inside static class methods
See original GitHub issueTake a look at this silly class
class Rectangle {
constructor(width, height) {
this.width = width;
this.height = height;
}
getArea() {
return this.width * this.height;
}
static getHeight() {
return this.height();
}
}
This is valid JS while it has a lot of danger in it. I suggest the creation of a rule that prevents people from calling this
from inside static class methods.
I would even suggest that eslint default config should give a warning on this.
Issue Analytics
- State:
- Created 5 years ago
- Comments:15 (7 by maintainers)
Top Results From Across the Web
class-methods-use-this - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >Why can't you use the keyword 'this' in a static method in .Net?
The keyword 'this' returns a reference to the current instance of the class containing it. Static methods (or any static member) do not...
Read more >Can we use "this" keyword in a static method in java?
The "this" keyword is used as a reference to an instance. Since the static methods doesn't have (belong to) any instance you cannot...
Read more >Static properties and methods - The Modern JavaScript Tutorial
Usually, static methods are used to implement functions that belong to the class as a whole, but not to any particular object of...
Read more >CA1822: Mark members as static (code analysis) - .NET
In this article ; CA1822 · Performance · Non-breaking - If the member is not visible outside the assembly, regardless of the change...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Since that syntax is required for symbols, a first-class language feature, i don’t think it makes sense to ever ban computed properties.
I think it could be useful to have a rule that warns when:
extends
keyword)this.someMethod()
is used in a static method of that classsomeMethod
is not a static method of the class, and there are no static computed method namesThat seems like it would be able to catch most error cases, without any false positives except when methods are added to a class after-the-fact.