linter
linter copied to clipboard
proposal: add lint or collection of lints to detect when using interfaces in an impure way.
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:
- Non-abstract declarations a. Non-abstract interfaces b. Non-abstract properties c. Non-abstract methods
- Generative constructors (factory constructors are okay)
- An interface using
extendsorwith - Another class using
extendson 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.