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((node) {
    // Only check async static methods
    if (!node.isStatic) return;
    if (node.body case BlockFunctionBody body) {
      if (!body.isAsynchronous) return;

      // Get BuildContext parameter names
      final contextParamNames = <String>[];
      for (final param
          in node.parameters?.parameters ?? <FormalParameter>[]) {
        final name = getBuildContextParamName(param);
        if (name != null) contextParamNames.add(name);
      }
      if (contextParamNames.isEmpty) return;

      // Analyze block for context usages after await without guards
      checkAsyncStaticBody(
        body.block,
        contextParamNames,
        (node) => reporter.atNode(node),
      );
    }
  });
}