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,
) {
  // addFunctionBody is a no-op stub in the native engine (FunctionBody is not
  // a visitable node), so this rule never fired; addBlockFunctionBody is the
  // real registration (BUG FIX 2026-07-16).
  context.addBlockFunctionBody((node) {
    final statements = node.block.statements;
    if (statements.isEmpty) return;

    // Check if function starts with an if that wraps most of the body
    final first = statements.first;
    if (first is! IfStatement) return;
    if (first.elseStatement != null) return;

    // If the if statement is the only statement or wraps most code
    if (statements.length == 1) {
      reporter.atNode(first);
    }
  });
}