Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(59)

Unified Diff: pkg/source_maps/lib/builder.dart

Issue 372153002: Moving logic for writing source maps from SourceMapBuilder into the Mapping class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Mostly style updates addressing round 1 comments. Created 6 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | pkg/source_maps/lib/parser.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/source_maps/lib/builder.dart
diff --git a/pkg/source_maps/lib/builder.dart b/pkg/source_maps/lib/builder.dart
index ef22e3123720dcdf9d726c85662ac3b06c24cd61..cd30ccb94941fd4b2ec57872e3be7a8e3daef716 100644
--- a/pkg/source_maps/lib/builder.dart
+++ b/pkg/source_maps/lib/builder.dart
@@ -7,25 +7,16 @@ library source_maps.builder;
// TODO(sigmund): add a builder for multi-section mappings.
-import 'dart:collection';
import 'dart:convert';
+import 'parser.dart';
import 'span.dart';
-import 'src/vlq.dart';
/// Builds a source map given a set of mappings.
class SourceMapBuilder {
final List<Entry> _entries = <Entry>[];
- /// Indices associated with file urls that will be part of the source map. We
- /// use a linked hash-map so that `_urls.keys[_urls[u]] == u`
- final Map<String, int> _urls = new LinkedHashMap<String, int>();
-
- /// Indices associated with identifiers that will be part of the source map.
- /// We use a linked hash-map so that `_names.keys[_names[n]] == n`
- final Map<String, int> _names = new LinkedHashMap<String, int>();
-
/// Adds an entry mapping the [targetOffset] to [source].
void addFromOffset(Location source,
SourceFile targetFile, int targetOffset, String identifier) {
@@ -48,78 +39,11 @@ class SourceMapBuilder {
/// Encodes all mappings added to this builder as a json map.
Map build(String fileUrl) {
- var buff = new StringBuffer();
- var line = 0;
- var column = 0;
- var srcLine = 0;
- var srcColumn = 0;
- var srcUrlId = 0;
- var srcNameId = 0;
- var first = true;
-
- // The encoding needs to be sorted by the target offsets.
- _entries.sort();
- for (var entry in _entries) {
- int nextLine = entry.target.line;
- if (nextLine > line) {
- for (int i = line; i < nextLine; ++i) {
- buff.write(';');
- }
- line = nextLine;
- column = 0;
- first = true;
- }
-
- if (!first) buff.write(',');
- first = false;
- column = _append(buff, column, entry.target.column);
-
- // Encoding can be just the column offset if there is no source
- // information.
- var source = entry.source;
- if (source == null) continue;
- var newUrlId = _indexOf(_urls, source.sourceUrl);
-
- srcUrlId = _append(buff, srcUrlId, newUrlId);
- srcLine = _append(buff, srcLine, source.line);
- srcColumn = _append(buff, srcColumn, source.column);
-
- if (entry.identifierName == null) continue;
- srcNameId = _append(buff, srcNameId,
- _indexOf(_names, entry.identifierName));
- }
-
- var result = {
- 'version': 3,
- 'sourceRoot': '',
- 'sources': _urls.keys.toList(),
- 'names' : _names.keys.toList(),
- 'mappings' : buff.toString()
- };
- if (fileUrl != null) {
- result['file'] = fileUrl;
- }
- return result;
+ return new SingleMapping.fromEntries(this._entries, fileUrl).toJson();
}
/// Encodes all mappings added to this builder as a json string.
String toJson(String fileUrl) => JSON.encode(build(fileUrl));
-
- /// Get the index of [value] in [map], or create one if it doesn't exist.
- int _indexOf(Map<String, int> map, String value) {
- return map.putIfAbsent(value, () {
- int index = map.length;
- map[value] = index;
- return index;
- });
- }
-
- /// Appends to [buff] a VLQ encoding of [newValue] using the difference
- /// between [oldValue] and [newValue]
- static int _append(StringBuffer buff, int oldValue, int newValue) {
- buff.writeAll(encodeVlq(newValue - oldValue));
- return newValue;
- }
}
/// An entry in the source map builder.
« no previous file with comments | « no previous file | pkg/source_maps/lib/parser.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698