linter icon indicating copy to clipboard operation
linter copied to clipboard

proposal: add lint or collection of lints to detect when using interfaces in an impure way.

Open mmcdon20 opened this issue 1 year ago • 3 comments

Description

Warn when using interfaces in an impure way.

Details

Dart allows you to to use interfaces in an impure way (especially within the same library it is defined). This lint would detect and warn against any of the following in an interface:

  1. Non-abstract declarations a. Non-abstract interfaces b. Non-abstract properties c. Non-abstract methods
  2. Generative constructors (factory constructors are okay)
  3. An interface using extends or with
  4. Another class using extends on an interface

Kind

Style advice

Bad Examples

interface class Vehicle { // 1a. Non-abstract interface
  final String make; // 1b. Non-abstract property
  final String model; // 1b. Non-abstract property
  Vehicle(this.make, this.model); // 2. Generative constructor

  void moveForward() { // 1c. Non-abstract method
    print('$make $model is moving forward!');
  }
}

// 3. An interface using extends or with
abstract interface class Car extends Vehicle {
  Car(super.make, super.model);
}

// 4. Another class using extends on an interface
class HondaCivic extends Car {
  HondaCivic() : super('Honda', 'Civic');
}

Good Examples

abstract interface class Vehicle {
  abstract final String make;
  abstract final String model;
  void moveForward();
}

abstract interface class Car implements Vehicle {}

class HondaCivic implements Car {
  @override
  final String make = 'Honda';
  @override
  final String model = 'Civic';
  @override
  void moveForward() {
    print('$make $model is moving forward!');
  }
}

Discussion

See also https://github.com/dart-lang/language/issues/3736 and https://github.com/dart-lang/sdk/issues/55641.

mmcdon20 avatar May 03 '24 19:05 mmcdon20