dart-code-metrics icon indicating copy to clipboard operation
dart-code-metrics copied to clipboard

[New rule] Implement a class instead of extending it when it is semantically an interface

Open purplenoodlesoop opened this issue 3 years ago • 3 comments

Please describe what the rule should do:

The rule should suggest swapping the extends keyword to the implements keyword if the class that is being extended follows two rules:

  1. Is abstract.
  2. Does not contain any implementations.

What category of rule is this? (place an "X" next to just one item)

[ ] Warns about a potential error (problem) [x] Suggests an alternate way of doing something (suggestion) [ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

BAD:


abstract class A {}

class B extends A {}

BAD:


abstract class A {
  int get a;
}

class B extends A {
  // Implementation
}

BAD:


abstract class A {
  int get a;
}

abstract class B extends A {
  String get b;
}

class C extends B {
  // Implementation
}

GOOD:


abstract class A {
  int get a;
}

class B implements A {
  // Implementation
}

GOOD:


abstract class A {
  int get a;
}

abstract class B {
  String get b;
}

abstract class C implements A, B {
  @override
  int get a => 1;
}

class D extends C {
  // Implementation
}

Are you willing to submit a pull request to implement this rule?

Not sure at the moment.

purplenoodlesoop avatar Dec 12 '21 20:12 purplenoodlesoop

@vlkonoshenko is this still in progress?

incendial avatar Mar 22 '22 17:03 incendial

@vlkonoshenko is this still in progress?

It’s on pause for now. I can find extends of abstract classes. But I can`t check if there are implementation of any methods in these classes.

vlkonoshenko avatar Mar 26 '22 07:03 vlkonoshenko

@vlkonoshenko what the problem with checking the implementation? You can collect class declarations and compare them to the base class to find the implemented methods, right?

incendial avatar Mar 28 '22 12:03 incendial

Feels like this one is covered by new Dart 3.0 features.

incendial avatar May 08 '23 07:05 incendial