generateClass function

String generateClass(
  1. ClassElement c
)

Implementation

String generateClass(ClassElement c) {
  final sub = c.library.topLevelElements
      .where((e) =>
          e is ClassElement &&
          e.allSupertypes.map((i) => i.element).contains(c))
      .toList();
  return '''
      extension ${c.name}Match on ${c.name} {
        T match<T>({
          ${sub.map(namedFunctionArgument).map((n) => 'required $n').join(',\n')}
        }) {
          final v = this;
          ${sub.map(typeMatch).join('\n')}

          throw Exception('${c.name}.match failed, found no match for: \$this');
        }

        T matchAny<T>({
          required T Function() any,
          ${sub.map(optionalNamedFunctionArgument).join(',\n')}
        }) {
          final v = this;
          ${sub.map(optionalTypeMatch).join('\n')}

          return any();
        }
      }
    ''';
}