Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

New rule to warn about the usage of this keyword inside static class methods

See original GitHub issue

Take 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:closed
  • Created 5 years ago
  • Comments:15 (7 by maintainers)

github_iconTop GitHub Comments

ljharbcommented, Jan 4, 2019

Since that syntax is required for symbols, a first-class language feature, i don’t think it makes sense to ever ban computed properties.

not-an-aardvarkcommented, Jan 3, 2019

I think it could be useful to have a rule that warns when:

  • A class does not have a superclass (i.e. it doesn’t use the extends keyword)
  • this.someMethod() is used in a static method of that class
  • someMethod is not a static method of the class, and there are no static computed method names

That 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Post

No results found

github_iconTop Related Hashnode Post

No results found