Skip to content

eslint/class-methods-use-this Restriction

What it does

Enforce that class methods utilize this.

Examples

Examples of incorrect code for this rule:

js
class A {
  foo() {
    console.log("Hello World");
  }
}

Examples of correct code for this rule:

js
class A {
  foo() {
    this.bar = "Hello World"; // OK, this is used
  }
}

class B {
  constructor() {
    // OK. constructor is exempt
  }
}

class C {
  static foo() {
    // OK. static methods aren't expected to use this.
  }
}

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny class-methods-use-this
json
{
  "rules": {
    "class-methods-use-this": "error"
  }
}

References

Released under the MIT License.