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.addMethodInvocation((MethodInvocation node) {
    if (node.methodName.name != 'validate') return;

    // Walk up to find if we're inside an onChanged callback
    AstNode? current = node.parent;
    while (current != null) {
      if (current is NamedArgument && current.name.lexeme == 'onChanged') {
        reporter.atNode(node);
        return;
      }
      // Stop at class/function/method declaration boundary
      if (current is ClassDeclaration ||
          current is FunctionDeclaration ||
          current is MethodDeclaration) {
        break;
      }
      current = current.parent;
    }
  });
}