sindresorhus/eslint-plugin-unicorn

Disallow `super` and `this` in static methods

已关闭

#1,050 创建于 2021年1月23日

 (12 条评论) (5 个反应) (1 位负责人)JavaScript (468 个派生)user submission
help wantednew rule

仓库指标

星标
 (5,022 个星标)
PR 合并指标
 (平均合并 1天 16小时) (30 天内合并 399 个 PR)

描述

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

贡献者指南