AnnotationArguments.fromDartObject constructor

AnnotationArguments.fromDartObject(
  1. ElementAnnotation annotation
)

Implementation

factory AnnotationArguments.fromDartObject(ElementAnnotation annotation) {
  final positionalArguments = <AnnotationArgument>[];
  final namedArguments = <String, AnnotationArgument>{};
  final annotationContext = switch (annotation) {
    ElementAnnotationImpl(annotationAst: final ast) => ast.toSource(),
    _ => annotation.element?.displayName,
  };

  if (annotation case ElementAnnotationImpl(
    annotationAst: Annotation(
      arguments: ArgumentList(childEntities: final args),
    ),
  )) {
    final positionalParameters = _positionalParameters(annotation);
    var positionalIndex = 0;

    for (final param in args) {
      if (param case NamedExpression(:final name, :final expression)) {
        namedArguments[name.label.name] = AnnotationArgument.fromExpression(
          expression,
          annotationContext: annotationContext,
          knownNamedParameter: name.label.name,
        );
      } else if (param case final Expression expression) {
        final knownParameter = positionalIndex < positionalParameters.length
            ? positionalParameters[positionalIndex]
            : null;
        positionalIndex++;

        positionalArguments.add(
          AnnotationArgument.fromExpression(
            expression,
            annotationContext: annotationContext,
            knownNamedParameter: knownParameter?.name,
            knownIsRequired: knownParameter?.isRequired,
          ),
        );
      }
    }
  }

  return AnnotationArguments(
    positional: positionalArguments,
    named: namedArguments,
  );
}