typeImportPrefix function

String typeImportPrefix(
  1. DartType type,
  2. LibraryElement library
)

Implementation

String typeImportPrefix(DartType type, LibraryElement library) {
  if (type.element?.library case LibraryElement(uri: final typeLibraryUri)) {
    for (final import in library.fragments.expand(
      (f) => f.prefixes.expand((p) => p.imports),
    )) {
      final exportedLibraries = <LibraryElement>[
        import.importedLibrary!,
        ...import.importedLibrary!.exportedLibraries,
      ];

      for (final exported in exportedLibraries) {
        if (exported case LibraryElement(
          uri: final importLibraryUri,
        ) when importLibraryUri == typeLibraryUri) {
          if (import.prefix case PrefixFragment(
            element: PrefixElement(:final displayName),
          )) {
            return '$displayName.';
          }
        }
      }
    }
  }
  return '';
}