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 (typeName != 'MouseRegion' && typeName != 'Listener') return;

    // Check if has hover callbacks
    bool hasHover = false;
    for (final Expression arg in node.argumentList.arguments) {
      if (arg is NamedExpression) {
        final String name = arg.name.label.name;
        if (name == 'onEnter' || name == 'onExit' || name == 'onHover') {
          hasHover = true;
          break;
        }
      }
    }

    if (!hasHover) return;

    // Check if wrapped in GestureDetector or similar
    AstNode? current = node.parent;
    while (current != null) {
      if (current is InstanceCreationExpression) {
        final String parentType = current.constructorName.type.name.lexeme;
        if (parentType == 'GestureDetector' ||
            parentType == 'InkWell' ||
            parentType == 'Tooltip' ||
            parentType == 'IconButton' ||
            parentType == 'TextButton') {
          return; // Has tap alternative
        }
      }
      if (current is MethodDeclaration || current is FunctionDeclaration) {
        break;
      }
      current = current.parent;
    }

    // Also check if child contains tap widgets
    final String nodeSource = node.toSource();
    if (nodeSource.contains('GestureDetector') ||
        nodeSource.contains('InkWell') ||
        nodeSource.contains('onTap') ||
        nodeSource.contains('onPressed')) {
      return;
    }

    reporter.atNode(node.constructorName, code);
  });
}