sindresorhus/eslint-plugin-unicorn

Disallow `super` and `this` in static methods

Open

#1,050 建立於 2021年1月23日

在 GitHub 查看
 (12 留言) (5 反應) (1 負責人)JavaScript (5,022 star) (468 fork)user submission
help wantednew rule

描述

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

貢獻者指南