visitMethodInvocation method

  1. @override
void visitMethodInvocation(
  1. MethodInvocation node
)
override

Implementation

@override
void visitMethodInvocation(MethodInvocation node) {
  if (result != null) {
    return;
  }

  if (node.methodName.name == 'on' &&
      node.argumentList.arguments.isNotEmpty) {
    final argument = node.argumentList.arguments.first;

    if (argument is SimpleIdentifier && argument.name == methodName) {
      TypeAnnotation? eventNode;
      if (node.typeArguments != null &&
          node.typeArguments!.arguments.isNotEmpty) {
        eventNode = node.typeArguments!.arguments.first;
      }

      FunctionBody? handlerBodyNode;
      final enclosingClass = node.thisOrAncestorOfType<ClassDeclaration>();
      if (enclosingClass != null) {
        for (final member in enclosingClass.members) {
          if (member is MethodDeclaration &&
              member.name.lexeme == methodName) {
            handlerBodyNode = member.body;
            break;
          }
        }
      }

      if (eventNode != null && handlerBodyNode != null) {
        result = OnInvocationNodes(eventNode, handlerBodyNode);
      }
    }
  }

  if (result == null) {
    super.visitMethodInvocation(node);
  }
}