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.addInstanceCreationExpression((InstanceCreationExpression node) {
    final String typeName = node.constructorName.type.name.lexeme;
    if (!_buttonWidgets.contains(typeName)) return;

    Expression? onPressedExpr;
    Expression? childExpr;

    for (final Expression arg in node.argumentList.arguments) {
      if (arg is NamedExpression) {
        final String argName = arg.name.label.name;
        if (argName == 'onPressed') {
          onPressedExpr = arg.expression;
        }
        if (argName == 'child') {
          childExpr = arg.expression;
        }
      }
    }

    if (onPressedExpr == null) return;
    final String onPressedSource = onPressedExpr.toSource();

    // Check if the callback is async
    bool isAsync =
        onPressedSource.contains('async') ||
        onPressedSource.contains('await');

    if (!isAsync) return;

    // Check if there's loading state handling
    bool hasLoadingState =
        onPressedSource.contains('isLoading') ||
        onPressedSource.contains('_loading') ||
        onPressedSource.contains('loading') ||
        onPressedSource.contains('isSubmitting') ||
        onPressedSource.contains('_submitting');

    // Check if child shows loading indicator
    String childSource = childExpr?.toSource() ?? '';
    bool hasLoadingIndicator =
        childSource.contains('CircularProgressIndicator') ||
        childSource.contains('Loading') ||
        childSource.contains('isLoading') ||
        childSource.contains('?');

    if (!hasLoadingState && !hasLoadingIndicator) {
      reporter.atNode(node.constructorName, code);
    }
  });
}