SingleMapping.fromEntries constructor

SingleMapping.fromEntries(
  1. Iterable<Entry> entries, [
  2. String? fileUrl
])

Implementation

factory SingleMapping.fromEntries(Iterable<builder.Entry> entries,
    [String? fileUrl]) {
  // The entries needs to be sorted by the target offsets.
  var sourceEntries = entries.toList()..sort();
  var lines = <TargetLineEntry>[];

  // Indices associated with file urls that will be part of the source map. We
  // rely on map order so that `urls.keys[urls[u]] == u`
  var urls = <String, int>{};

  // Indices associated with identifiers that will be part of the source map.
  // We rely on map order so that `names.keys[names[n]] == n`
  var names = <String, int>{};

  /// The file for each URL, indexed by [urls]' values.
  var files = <int, SourceFile>{};

  int? lineNum;
  late List<TargetEntry> targetEntries;
  for (var sourceEntry in sourceEntries) {
    if (lineNum == null || sourceEntry.target.line > lineNum) {
      lineNum = sourceEntry.target.line;
      targetEntries = <TargetEntry>[];
      lines.add(TargetLineEntry(lineNum, targetEntries));
    }

    var sourceUrl = sourceEntry.source.sourceUrl;
    var urlId = urls.putIfAbsent(
        sourceUrl == null ? '' : sourceUrl.toString(), () => urls.length);

    if (sourceEntry.source is FileLocation) {
      files.putIfAbsent(
          urlId, () => (sourceEntry.source as FileLocation).file);
    }

    var sourceEntryIdentifierName = sourceEntry.identifierName;
    var srcNameId = sourceEntryIdentifierName == null
        ? null
        : names.putIfAbsent(sourceEntryIdentifierName, () => names.length);
    targetEntries.add(TargetEntry(sourceEntry.target.column, urlId,
        sourceEntry.source.line, sourceEntry.source.column, srcNameId));
  }
  return SingleMapping._(fileUrl, urls.values.map((i) => files[i]).toList(),
      urls.keys.toList(), names.keys.toList(), lines);
}