moduleUrl function

String? moduleUrl(
  1. Element element
)

Finds the url of the library that declares the element.

Note that this needs to check librarySource instead of just source to handle part files correctly.

Implementation

String? moduleUrl(Element element) {
  // TODO(mfairhurst) delete this check. This is just to preserve goldens when
  // https://dart-review.googlesource.com/c/sdk/+/62730 is rolled out. It has
  // no user-facing effects.
  if (element.kind == ElementKind.DYNAMIC) {
    return null;
  }
  // Type parameters aren't imported, thus don't required a library URL.
  if (element.kind == ElementKind.TYPE_PARAMETER) {
    return null;
  }
  var source = element.librarySource ?? element.source;
  var uri = source?.uri.toString();
  if (uri == null) return null;
  if (Uri.parse(uri).scheme == 'dart') return uri;
  return toAssetUri(_fromUri(uri));
}