runWithReporter method

  1. @override
void runWithReporter(
  1. SaropaDiagnosticReporter reporter,
  2. SaropaContext context
)
override

Override this method to implement your lint rule.

Use context to register callbacks for AST node types:

context.addMethodInvocation((node) {
  if (condition) {
    reporter.atNode(node);
  }
});

Implementation

@override
void runWithReporter(
  SaropaDiagnosticReporter reporter,
  SaropaContext context,
) {
  context.addClassDeclaration((ClassDeclaration node) {
    final ExtendsClause? extendsClause = node.extendsClause;
    if (extendsClause == null) return;

    final String superclassName = extendsClause.superclass.name.lexeme;

    // Only suggest for classes that directly extend Equatable
    if (superclassName != 'Equatable') return;

    // Check if class already uses any mixins - if so, mixin pattern
    // would be even more appropriate
    final WithClause? withClause = node.withClause;
    if (withClause != null && withClause.mixinTypes.isNotEmpty) {
      // Already using mixins, strongly suggest EquatableMixin
      reporter.atNode(extendsClause);
      return;
    }

    // For classes extending only Equatable with no mixins,
    // this is more of a suggestion for awareness
    // Report at INFO level
    reporter.atNode(extendsClause);
  });
}