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,
) {
  // Check for field declarations with Box<T> type
  context.addFieldDeclaration((FieldDeclaration node) {
    final String? typeName = node.fields.type?.toSource();
    if (typeName == null) return;

    // Check for Box<T> but not LazyBox<T>
    if (typeName.startsWith('Box<') &&
        !RegExp(r'\bLazy\b').hasMatch(typeName)) {
      // Heuristic: warn if the type suggests a collection
      // (messages, items, logs, history, etc.)
      for (final variable in node.fields.variables) {
        final name = variable.name.lexeme.toLowerCase();
        if (_suggestsLargeCollection(name)) {
          reporter.atNode(variable);
        }
      }
    }
  });

  // Also check for Hive.openBox calls
  context.addMethodInvocation((MethodInvocation node) {
    final target = node.target;
    if (target is! SimpleIdentifier || target.name != 'Hive') return;

    if (node.methodName.name != 'openBox') return;

    // Check the box name argument
    final args = node.argumentList.arguments;
    if (args.isNotEmpty) {
      final firstArg = args.first;
      if (firstArg is StringLiteral) {
        final boxName = firstArg.stringValue?.toLowerCase() ?? '';
        if (_suggestsLargeCollection(boxName)) {
          reporter.atNode(node);
        }
      }
    }
  });
}