Skip to content

Rule Change: [class-methods-use-this] Add ignoreClassesThatImplementAnInterface option #19507

@JoshuaKGoldberg

Description

@JoshuaKGoldberg

What rule do you want to change?

class-methods-use-this

What change do you want to make?

Generate fewer warnings

How do you think the change should be implemented?

A new option

Example code

interface Base {
  method(): void;
}

class Derived implements Base {
  method() {}
  property = () => {};
}

What does the rule currently do for this code?

Reports on the method and property for not using this.

This is sub-optimal because some projects prefer organizing code with classes implementing interfaces even when implementing methods don't always need to use this.

What will the rule do after it's changed?

If the user has enabled a new ignoreClassesThatImplementAnInterface option to...

  • 'all': ignore all classes that implement an interface
    • This is true in the typescript-eslint rule for legacy reasons, but IMO it's odd having a boolean | string option
  • 'public-fields': ignore only the public fields of classes that implement an interface

Either way, the provided snippet would not receive any lint reports.

Participation

  • I am willing to submit a pull request to implement this change.

Additional comments

It would be nice to know which methods are actually defined on the base interface. But doing so would require typed linting, and not even the typescript-eslint extension rule is typed.

Following #19498, this feature request corresponds to the existing typescript-eslint rule option: https://typescript-eslint.io/rules/class-methods-use-this/#ignoreclassesthatimplementaninterface

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedThere is consensus among the team that this change meets the criteria for inclusionenhancementThis change enhances an existing feature of ESLintruleRelates to ESLint's core rules

    Type

    No type

    Projects

    Status

    Complete

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions