OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /// Contains a builder object useful for creating source maps programatically. |
| 6 library source_maps.builder; |
| 7 |
| 8 // TODO(sigmund): add a builder for multi-section mappings. |
| 9 |
| 10 import 'dart:json' as json; |
| 11 import 'dart:collection'; |
| 12 |
| 13 import 'span.dart'; |
| 14 import 'src/vlq.dart'; |
| 15 |
| 16 /// Builds a source map given a set of mappings. |
| 17 class SourceMapBuilder { |
| 18 |
| 19 final List<Entry> _entries = <Entry>[]; |
| 20 |
| 21 /// Indices associated with file urls that will be part of the source map. We |
| 22 /// use a linked hash-map so that `_urls.keys[_urls[u]] == u` |
| 23 final Map<String, int> _urls = new LinkedHashMap<String, int>(); |
| 24 |
| 25 /// Indices associated with identifiers that will be part of the source map. |
| 26 /// We use a linked hash-map so that `_names.keys[_names[n]] == n` |
| 27 final Map<String, int> _names = new LinkedHashMap<String, int>(); |
| 28 |
| 29 /// Adds an entry mapping the [targetOffset] to [source]. |
| 30 void addFromOffset(Location source, |
| 31 SourceFile targetFile, int targetOffset, String identifier) { |
| 32 if (targetFile == null) { |
| 33 throw new ArgumentError('targetFile cannot be null'); |
| 34 } |
| 35 _entries.add(new Entry(source, |
| 36 new FileLocation(targetFile, targetOffset), identifier)); |
| 37 } |
| 38 |
| 39 /// Adds an entry mapping [target] to [source]. |
| 40 void addSpan(Span source, Span target) { |
| 41 var name = source.isIdentifier ? source.text : null; |
| 42 _entries.add(new Entry(source.start, target.start, name)); |
| 43 } |
| 44 |
| 45 void addLocation(Location source, Location target, String identifier) { |
| 46 _entries.add(new Entry(source, target, identifier)); |
| 47 } |
| 48 |
| 49 /// Encodes all mappings added to this builder as a json map. |
| 50 Map build(String fileUrl) { |
| 51 var buff = new StringBuffer(); |
| 52 var line = 0; |
| 53 var column = 0; |
| 54 var srcLine = 0; |
| 55 var srcColumn = 0; |
| 56 var srcUrlId = 0; |
| 57 var srcNameId = 0; |
| 58 var first = true; |
| 59 |
| 60 // The encoding needs to be sorted by the target offsets. |
| 61 _entries.sort(); |
| 62 for (var entry in _entries) { |
| 63 int nextLine = entry.target.line; |
| 64 if (nextLine > line) { |
| 65 for (int i = line; i < nextLine; ++i) { |
| 66 buff.write(';'); |
| 67 } |
| 68 line = nextLine; |
| 69 column = 0; |
| 70 first = true; |
| 71 } |
| 72 |
| 73 if (!first) buff.write(','); |
| 74 first = false; |
| 75 column = _append(buff, column, entry.target.column); |
| 76 |
| 77 if (entry.source == null) continue; |
| 78 |
| 79 srcUrlId = _append(buff, srcUrlId, |
| 80 _indexOf(_urls, entry.source.sourceUrl)); |
| 81 srcLine = _append(buff, srcLine, entry.source.line); |
| 82 srcColumn = _append(buff, srcColumn, entry.source.column); |
| 83 |
| 84 if (entry.identifierName == null) continue; |
| 85 srcNameId = _append(buff, srcNameId, |
| 86 _indexOf(_names, entry.identifierName)); |
| 87 } |
| 88 |
| 89 var result = { |
| 90 'version': 3, |
| 91 'sourceRoot': '', |
| 92 'sources': _urls.keys.toList(), |
| 93 'names' : _names.keys.toList(), |
| 94 'mappings' : buff.toString() |
| 95 }; |
| 96 if (fileUrl != null) { |
| 97 result['file'] = fileUrl; |
| 98 } |
| 99 return result; |
| 100 } |
| 101 |
| 102 /// Encodes all mappings added to this builder as a json string. |
| 103 String toJson(String fileUrl) => json.stringify(build(fileUrl)); |
| 104 |
| 105 /// Get the index of [value] in [map], or create one if it doesn't exist. |
| 106 int _indexOf(Map<String, int> map, String value) { |
| 107 return map.putIfAbsent(value, () { |
| 108 int index = map.length; |
| 109 map[value] = index; |
| 110 return index; |
| 111 }); |
| 112 } |
| 113 |
| 114 /// Appends to [buff] a VLQ encoding of [newValue] using the difference |
| 115 /// between [oldValue] and [newValue] |
| 116 static int _append(StringBuffer buff, int oldValue, int newValue) { |
| 117 buff.writeAll(encodeVlq(newValue - oldValue)); |
| 118 return newValue; |
| 119 } |
| 120 } |
| 121 |
| 122 /// An entry in the source map builder. |
| 123 class Entry implements Comparable { |
| 124 /// Span denoting the original location in the input source file |
| 125 final Location source; |
| 126 |
| 127 /// Span indicating the corresponding location in the target file. |
| 128 final Location target; |
| 129 |
| 130 /// An identifier name, when this location is the start of an identifier. |
| 131 final String identifierName; |
| 132 |
| 133 Entry(this.source, this.target, this.identifierName); |
| 134 |
| 135 /// Implements [Comparable] to ensure that entries are ordered by their |
| 136 /// location in the target file. We sort primarily by the target offset |
| 137 /// because source map files are encoded by printing each mapping in order as |
| 138 /// they appear in the target file. |
| 139 int compareTo(Entry other) { |
| 140 int res = target.compareTo(other.target); |
| 141 if (res != 0) return res; |
| 142 res = source.sourceUrl.compareTo(other.source.sourceUrl); |
| 143 if (res != 0) return res; |
| 144 return source.compareTo(other.source); |
| 145 } |
| 146 } |
OLD | NEW |