visitTypeVariantElement method

  1. @override
List<Class> visitTypeVariantElement(
  1. TypeVariantElement node
)
override

Implementation

@override
List<Class> visitTypeVariantElement(TypeVariantElement node) {
  final typeDefinitionElement = node.enclosingElement;

  final variantClass = ClassBuilder(
    name: node.name,
    withEquality: true,
  )..final$ = true;

  for (final parameter in node.parameters) {
    for (final type in _typeParametersFromType(parameter.type!)) {
      variantClass.addParameter(_typeReferenceFromType(type));
    }

    variantClass.addField(
      _typeReferenceFromType(parameter.type!),
      parameter,
    );
  }

  if (typeDefinitionElement.variants.length > 1) {
    variantClass.defineSupertypeName(typeDefinitionElement.name);

    final currentVariantTypes = [
      for (final parameter in node.parameters) parameter.type!,
    ];

    final typeParameters = _typeParametersFromTypeList(currentVariantTypes);

    for (final typeParameter in typeDefinitionElement.parameters) {
      final argument = typeParameters.contains(typeParameter.type) //
          ? typeParameter.type!
          : const BottomType();

      final parameter = _typeReferenceFromType(argument);
      variantClass.addParameterToSupertype(parameter);
    }
  }

  return [variantClass.asCodeBuilderClass()];
}