| 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:convert'; | |
| 11 | |
| 12 import 'package:source_span/source_span.dart'; | |
| 13 | |
| 14 import 'parser.dart'; | |
| 15 import 'src/source_map_span.dart'; | |
| 16 | |
| 17 /// Builds a source map given a set of mappings. | |
| 18 class SourceMapBuilder { | |
| 19 | |
| 20 final List<Entry> _entries = <Entry>[]; | |
| 21 | |
| 22 /// Adds an entry mapping the [targetOffset] to [source]. | |
| 23 void addFromOffset(SourceLocation source, SourceFile targetFile, | |
| 24 int targetOffset, String identifier) { | |
| 25 if (targetFile == null) { | |
| 26 throw new ArgumentError('targetFile cannot be null'); | |
| 27 } | |
| 28 _entries.add( | |
| 29 new Entry(source, targetFile.location(targetOffset), identifier)); | |
| 30 } | |
| 31 | |
| 32 /// Adds an entry mapping [target] to [source]. | |
| 33 /// | |
| 34 /// If [isIdentifier] is true or if [target] is a [SourceMapSpan] with | |
| 35 /// `isIdentifier` set to true, this entry is considered to represent an | |
| 36 /// identifier whose value will be stored in the source map. [isIdenfier] | |
| 37 /// takes precedence over [target]'s `isIdentifier` value. | |
| 38 void addSpan(SourceSpan source, SourceSpan target, {bool isIdentifier}) { | |
| 39 if (isIdentifier == null) { | |
| 40 isIdentifier = source is SourceMapSpan ? source.isIdentifier : false; | |
| 41 } | |
| 42 | |
| 43 var name = isIdentifier ? source.text : null; | |
| 44 _entries.add(new Entry(source.start, target.start, name)); | |
| 45 } | |
| 46 | |
| 47 /// Adds an entry mapping [target] to [source]. | |
| 48 void addLocation(SourceLocation source, SourceLocation target, | |
| 49 String identifier) { | |
| 50 _entries.add(new Entry(source, target, identifier)); | |
| 51 } | |
| 52 | |
| 53 /// Encodes all mappings added to this builder as a json map. | |
| 54 Map build(String fileUrl) { | |
| 55 return new SingleMapping.fromEntries(this._entries, fileUrl).toJson(); | |
| 56 } | |
| 57 | |
| 58 /// Encodes all mappings added to this builder as a json string. | |
| 59 String toJson(String fileUrl) => JSON.encode(build(fileUrl)); | |
| 60 } | |
| 61 | |
| 62 /// An entry in the source map builder. | |
| 63 class Entry implements Comparable { | |
| 64 /// Span denoting the original location in the input source file | |
| 65 final SourceLocation source; | |
| 66 | |
| 67 /// Span indicating the corresponding location in the target file. | |
| 68 final SourceLocation target; | |
| 69 | |
| 70 /// An identifier name, when this location is the start of an identifier. | |
| 71 final String identifierName; | |
| 72 | |
| 73 /// Creates a new [Entry] mapping [target] to [source]. | |
| 74 Entry(this.source, this.target, this.identifierName); | |
| 75 | |
| 76 /// Implements [Comparable] to ensure that entries are ordered by their | |
| 77 /// location in the target file. We sort primarily by the target offset | |
| 78 /// because source map files are encoded by printing each mapping in order as | |
| 79 /// they appear in the target file. | |
| 80 int compareTo(Entry other) { | |
| 81 int res = target.compareTo(other.target); | |
| 82 if (res != 0) return res; | |
| 83 res = source.sourceUrl.toString().compareTo( | |
| 84 other.source.sourceUrl.toString()); | |
| 85 if (res != 0) return res; | |
| 86 return source.compareTo(other.source); | |
| 87 } | |
| 88 } | |
| OLD | NEW |