eslint-plugin-unicorn icon indicating copy to clipboard operation
eslint-plugin-unicorn copied to clipboard

Disallow `super` and `this` in static methods

Open Torhal opened this issue 4 years ago • 12 comments

The this keyword on static methods refers the class instance (the constructor). This can be confusing.

Fail

class A {
    static foo() {
        doSomething()
    }

    static bar() {
        this.foo()   //ERROR: Unexpected 'this'.
    }
}

class B extends A {
    static foo() {
        super.foo()  //ERROR: Unexpected 'super'.
    }
}

Pass

class A {
    static foo() {
        doSomething()
    }

    static bar() {
        A.foo()
    }
}

class B extends A {
    static foo() {
        A.foo()
    }
}

Torhal avatar Jan 23 '21 00:01 Torhal

Why is this confusing on a static method?

sindresorhus avatar Jan 23 '21 05:01 sindresorhus

Well, it can be confusing to people switching between languages or are only used to languages where using this in a static context isn't allowed because it's reserved for class instances where here it's referring to the constructor.

It can also become imprecise when used with extended classes when a static this of a parent class no longer specifically refers to the parent class.

Torhal avatar Jan 25 '21 03:01 Torhal

I would actually want the opposite, so I think the rule should let you enforce either this or "class name" in static methods. Same with super.

Any suggestions for the rule name?

sindresorhus avatar Jan 25 '21 12:01 sindresorhus

Without "classname" how to access another property/method?

fisker avatar Jan 25 '21 12:01 fisker

Without "classname" how to access another property/method?

Not sure I understand the question. I would access it with this or super.

sindresorhus avatar Jan 25 '21 14:01 sindresorhus

Oh, sorry I misunderstood, I though this rule not allow this "class name" and super.

fisker avatar Jan 25 '21 14:01 fisker

Something to consider.

When working in Typescript there are cases where you need to create or wrap things in anonymous classes. This is because you're passing down the generic type to the underlying static methods. More complex scenarios.

You may not have access to a class name. Not sure there's a likely default here.

With @sindresorhus on this as to enforcing either this or classname but this particular rule I can see being set per file in several cases.

blujedis avatar Jan 25 '21 18:01 blujedis

Suggestions for the rule name:

  • this-and-super-in-static-methods
  • class-reference-in-static-methods

Better suggestions welcome.

sindresorhus avatar Feb 16 '21 17:02 sindresorhus

This is now accepted.

sindresorhus avatar Feb 16 '21 17:02 sindresorhus

  • class-reference-in-static-methods

That seems to be the best and most concise for what this has turned into. The original proposal would probably have been something like no-static-this.

Torhal avatar Feb 16 '21 23:02 Torhal

👍 for class-reference-in-static-methods

fisker avatar Feb 19 '21 08:02 fisker

If anyone wants to work on this, see the initial attempt and feedback in https://github.com/sindresorhus/eslint-plugin-unicorn/pull/1119.

sindresorhus avatar Oct 29 '23 18:10 sindresorhus