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.addMethodDeclaration((MethodDeclaration node) {
    if (node.name.lexeme != 'build') return;

    final Map<String, int> ofCallCounts = <String, int>{};
    final List<MethodInvocation> duplicateCalls = <MethodInvocation>[];

    node.body.visitChildren(
      _OfCallVisitor((MethodInvocation call) {
        final Expression? target = call.target;
        if (target is SimpleIdentifier) {
          final String typeName = target.name;
          final int count = (ofCallCounts[typeName] ?? 0) + 1;
          ofCallCounts[typeName] = count;
          if (count > 1) {
            duplicateCalls.add(call);
          }
        }
      }),
    );

    for (final MethodInvocation call in duplicateCalls) {
      reporter.atNode(call);
    }
  });
}