urlOf function

Uri urlOf(
  1. Element? element, [
  2. String? name
])

Returns a canonical URL pointing to element.

For example:

  • List would be 'dart:core#List',
  • Duration.zero would be 'dart:core#Duration.zero'.

Implementation

Uri urlOf(Element? element, [String? name]) {
  if (element?.source == null) {
    return Uri(scheme: 'dart', path: 'core', fragment: 'dynamic');
  }

  var fragment = name ?? element!.name;
  final enclosing = element!.enclosingElement;
  if (enclosing is ClassElement) {
    fragment = '${enclosing.name}.$fragment';
  }

  // NOTE: element.source.uri might be a file that is not importable (i.e. is
  // a "part"), while element.library.source.uri is always importable.
  return normalizeUrl(element.library!.source.uri).replace(fragment: fragment);
}