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) {
    // Check if it's a State class
    final ExtendsClause? extendsClause = node.extendsClause;
    if (extendsClause == null) return;

    final String superclassName = extendsClause.superclass.name.lexeme;
    if (superclassName != 'State') return;

    // Check if already has AutomaticKeepAliveClientMixin
    final WithClause? withClause = node.withClause;
    if (withClause != null) {
      for (final NamedType mixin in withClause.mixinTypes) {
        if (mixin.name.lexeme == 'AutomaticKeepAliveClientMixin') {
          return; // Already has the mixin
        }
      }
    }

    // Check if this State builds a scrollable inside a tabbed/paged context.
    // Use word boundary so we match TabBarView/PageView as tokens, not
    // substrings (e.g. avoids matching "Tab" in HomeTab.icon).
    final String classSource = node.toSource();
    if (classSource.contains('ListView') ||
        classSource.contains('GridView') ||
        classSource.contains('CustomScrollView')) {
      if (_tabPageViewPattern.hasMatch(classSource)) {
        reporter.atToken(node.nameToken, code);
      }
    }
  });
}