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,
) {
  // CLI tools use print() for terminal output — no release/debug distinction
  final projectInfo = ProjectContext.getProjectInfo(context.filePath);
  if (projectInfo == null || !projectInfo.isFlutterProject) return;

  context.addMethodInvocation((MethodInvocation node) {
    if (node.methodName.name != 'print') return;
    if (node.target != null) return; // Skip object.print()

    if (!_isInsideDebugGuard(node)) {
      reporter.atNode(node);
    }
  });

  context.addFunctionExpressionInvocation((
    FunctionExpressionInvocation node,
  ) {
    final Expression function = node.function;
    if (function is SimpleIdentifier && function.name == 'print') {
      if (!_isInsideDebugGuard(node)) {
        reporter.atNode(node);
      }
    }
  });
}