collectTypeParameters function

Future<Map<String, String>> collectTypeParameters(
  1. Iterable<ClassElement> directives,
  2. BuildStep buildStep
)

Returns the generic type parameters, mapped by class name, of directives.

Generic type parameters are returned exactly as written in source. If a directive isn't generic, its type parameters are represented as the empty string.

For example, if directives contains class elements for Foo and Bar which are declared as

class Foo {}
class Bar<T, R extends prefix.Bound> { ... }

this returns

{
  'Foo': '',
  'Bar': '<T, R extends prefix.Bound>',
}

Implementation

Future<Map<String, String>> collectTypeParameters(
    Iterable<ClassElement> directives, BuildStep buildStep) async {
  final typeParameters = <String, String>{};
  final assetsToParse = <AssetId>{};
  final resolver = buildStep.resolver;
  for (final directive in directives) {
    typeParameters[directive.name] = '';
    assetsToParse.add(await resolver.assetIdForElement(directive));
  }
  // Avoid parsing source if there are no directives with generic type
  // parameters to collect.
  if (assetsToParse.isNotEmpty) {
    await Future.wait(assetsToParse.map((asset) =>
        _collectTypeParametersFromUnit(asset, buildStep, typeParameters)));
  }
  return typeParameters;
}