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,
) {
  // Track options usage in file
  final List<MethodInvocation> dioRequestsWithOptions = [];

  context.addMethodInvocation((MethodInvocation node) {
    final methodName = node.methodName.name;
    if (!['get', 'post', 'put', 'patch', 'delete'].contains(methodName)) {
      return;
    }

    final target = node.target;
    if (target == null) return;

    final targetSource = target.toSource().toLowerCase();
    if (!_dioTargetRegex.hasMatch(targetSource)) return;

    // Check for options parameter
    for (final arg in node.argumentList.arguments) {
      if (arg is NamedExpression && arg.name.label.name == 'options') {
        dioRequestsWithOptions.add(node);
        break;
      }
    }

    // If we've seen multiple requests with options, report
    if (dioRequestsWithOptions.length >= 3) {
      reporter.atNode(node.methodName, code);
    }
  });
}