lookupIdentifierByProvider method

DeclarationRef? lookupIdentifierByProvider(
  1. String name,
  2. String providerSrc
)

Looks up an identifier using its provider source.

This is useful for resolving identifiers when you know which library is providing them, such as when dealing with re-exports.

name is the identifier name. providerSrc is the ID of the library providing the identifier.

Returns null if the identifier cannot be found.

Implementation

DeclarationRef? lookupIdentifierByProvider(String name, String providerSrc) {
  if (!assets.containsKey(providerSrc)) return null;
  final Map<String, List<dynamic>> possibleSrcs = Map<String, List<dynamic>>.fromEntries(
    identifiers
        .where((List<dynamic> e) => e[GraphIndex.identifierName] == name)
        .map(
          (List<dynamic> e) => MapEntry<String, List<dynamic>>(
            e[GraphIndex.identifierSrc],
            e,
          ),
        ),
  );

  // First check if the identifier is declared directly in this file
  for (final MapEntry<String, List<dynamic>> entry in possibleSrcs.entries) {
    if (entry.key == providerSrc) {
      return DeclarationRef(
        identifier: name,
        srcId: providerSrc,
        providerId: providerSrc,
        type: ReferenceType.fromValue(entry.value[GraphIndex.identifierType]),
        srcUri: uriForAsset(providerSrc),
      );
    }
  }
  // trace exports
  final Set<String> visitedSrcs = <String>{};
  final String? src = _traceExportsOf(
    providerSrc,
    name,
    possibleSrcs.keys,
    visitedSrcs,
  );
  if (src != null) {
    return DeclarationRef(
      identifier: name,
      srcId: src,
      providerId: providerSrc,
      type: ReferenceType.fromValue(
        possibleSrcs[src]![GraphIndex.identifierType],
      ),
      srcUri: uriForAsset(src),
    );
  }
  return null;
}