sindresorhus/eslint-plugin-unicorn

Disallow `super` and `this` in static methods

Open

#1,050 opened on Jan 23, 2021

View on GitHub
 (12 comments) (5 reactions) (1 assignee)JavaScript (5,022 stars) (468 forks)user submission
help wantednew rule

Description

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()
    }
}

Contributor guide