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,
) {
  // Only check test files
  final String path = context.filePath;
  if (!path.contains('_test.dart') &&
      !path.contains('/test/') &&
      !path.contains(r'\test\')) {
    return;
  }

  context.addMethodInvocation((MethodInvocation node) {
    final String methodName = node.methodName.name;

    // Check for sleep() calls
    if (methodName == 'sleep') {
      reporter.atNode(node);
      return;
    }

    // Check for Future.delayed with actual duration
    if (methodName == 'delayed') {
      final Expression? target = node.target;
      if (target is SimpleIdentifier && target.name == 'Future') {
        // Check if inside fakeAsync - that's okay
        AstNode? current = node.parent;
        while (current != null) {
          if (current is MethodInvocation) {
            final String parentMethod = current.methodName.name;
            if (parentMethod == 'fakeAsync') {
              return; // Inside fakeAsync, so it's okay
            }
          }
          current = current.parent;
        }
        reporter.atNode(node);
      }
    }
  });
}