asCodeBuilderClass method

Class asCodeBuilderClass()

Implementation

Class asCodeBuilderClass() {
  return Class((builder) {
    builder.name = name;
    builder.types = ListBuilder(_typeParameters);

    if (final$) {
      builder.modifier = ClassModifier.final$;
    }

    builder.sealed = sealed;

    if (_supertype case final supertype?) {
      builder.implements.add(
        TypeReference((builder) {
          final (symbol, types) = supertype;

          builder.symbol = symbol;
          builder.types = ListBuilder(types);
        }),
      );
    }

    if (!sealed) {
      // TODO(mateusfccp): Improve this
      builder.constructors.add(
        Constructor((builder) {
          builder.constant = true;
          // See: https://github.com/dart-lang/code_builder/issues/385
          builder.optionalParameters = _constructorParameters;
        }),
      );
    }

    builder.fields = _fields;

    if (withEquality) {
      builder.methods.addAll([
        _equals(),
        _hashCode(),
        _toString(),
      ]);
    }
  });
}