collectData function

ProviderDataCollection collectData(
  1. ParseStringResult content
)

Implementation

ProviderDataCollection collectData(ParseStringResult content) {
  final unit = content.unit;

  final providers = <ProviderInfo>[];

  for (final declaration in unit.declarations) {
    if (declaration is ClassDeclaration) {
      if (!_hasAnnotation(declaration.metadata, 'provider')) continue;

      final className = declaration.name.lexeme;
      final isKeepAlive = _hasAnnotation(declaration.metadata, 'keepAlive');

      final createMethod = _findCreateMethod(declaration);
      if (createMethod == null) continue;

      final dataType = _extractGenericTypeMethod(createMethod);

      // Get create method params - these are the family args
      final createParams = _extractParametersMothod(createMethod);

      final providerType = _getProviderTypeMethod(createMethod);

      final commands = _extractCommands(declaration);
      final publicMethods = _extractPublicMethods(declaration);

      // For class-based providers, create method params are family params
      final familyParams = createParams
          .map(
            (p) => ParameterInfo(
              name: p.name,
              type: p.type,
              isRequired: p.isRequired,
              defaultValue: p.defaultValue,
              isPositional: p.isPositional,
              isFamily: true,
            ),
          )
          .toList();

      // @settable is ignored on class-based providers — only functional providers support it
      providers.add(
        ProviderInfo(
          name: className,
          dataType: dataType,
          isKeepAlive: isKeepAlive,
          type: providerType,
          params: familyParams,
          commands: commands,
          publicMethods: publicMethods,
        ),
      );
    }
  }

  // Top-level functional providers
  for (final declaration in unit.declarations) {
    if (declaration is FunctionDeclaration) {
      final metadata = declaration.metadata;
      final isProvider = _hasAnnotation(metadata, 'provider');
      if (!isProvider) continue;

      final functionName = declaration.name.lexeme;
      final isKeepAlive = _hasAnnotation(metadata, 'keepAlive');
      final isSettable = _hasAnnotation(metadata, 'settable');
      final providerType = _getProviderTypeFunction(declaration);
      final dataType = _extractGenericTypeFunction(declaration);

      // parameters
      var params = _extractParametersFunction(declaration);

      // Check for Ref ref as first parameter
      bool requiresRef = false;
      final firstPositional = params.where((p) => p.isPositional).firstOrNull;
      if (firstPositional != null &&
          firstPositional.name == 'ref' &&
          firstPositional.type == 'Ref') {
        requiresRef = true;
        params = List<ParameterInfo>.from(params)..remove(firstPositional);
      }

      providers.add(
        ProviderInfo(
          name: functionName[0].toUpperCase() + functionName.substring(1),
          dataType: dataType,
          isKeepAlive: isKeepAlive,
          type: providerType,
          params: params,
          commands: const [],
          isFunctional: true,
          functionName: functionName,
          requiresRef: requiresRef,
          isSettable: isSettable,
          publicMethods: const <PublicMethod>[],
        ),
      );
    }
  }

  final topLevelCommands = _extractTopLevelCommands(unit);

  return ProviderDataCollection(
    providers: providers,
    commands: topLevelCommands,
  );
}