maybeHookBuilderBody function

FunctionBody? maybeHookBuilderBody(
  1. InstanceCreationExpression node
)

Given an instance creation, returns the builder function body if the node is a HookBuilder.

Implementation

FunctionBody? maybeHookBuilderBody(InstanceCreationExpression node) {
  final classElement = node.constructorName.type.element;
  if (classElement == null) {
    return null;
  }

  final isHookBuilder = const TypeChecker.any([
    TypeChecker.fromName(
      'HookBuilder',
      packageName: 'flutter_hooks',
    ),
    TypeChecker.fromName(
      'HookConsumer',
      packageName: 'hooks_riverpod',
    ),
  ]).isExactly(classElement);
  if (!isHookBuilder) {
    return null;
  }

  final builderParameter = node.argumentList.arguments
      .whereType<NamedExpression>()
      .firstWhereOrNull((e) => e.name.label.name == 'builder');
  if (builderParameter
      case NamedExpression(expression: FunctionExpression(:final body))) {
    return body;
  }

  return null;
}