Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// Contains a builder object useful for creating source maps programatically. | 5 /// Contains a builder object useful for creating source maps programatically. |
| 6 library source_maps.builder; | 6 library source_maps.builder; |
| 7 | 7 |
| 8 // TODO(sigmund): add a builder for multi-section mappings. | 8 // TODO(sigmund): add a builder for multi-section mappings. |
| 9 | 9 |
| 10 import 'dart:convert'; | 10 import 'dart:convert'; |
| 11 | 11 |
| 12 import 'parser.dart'; | 12 import 'parser.dart'; |
| 13 import 'span.dart'; | 13 import 'span.dart'; |
| 14 import 'src/span_wrapper.dart'; | |
| 14 | 15 |
| 15 /// Builds a source map given a set of mappings. | 16 /// Builds a source map given a set of mappings. |
| 16 class SourceMapBuilder { | 17 class SourceMapBuilder { |
| 17 | 18 |
| 18 final List<Entry> _entries = <Entry>[]; | 19 final List<Entry> _entries = <Entry>[]; |
| 19 | 20 |
| 20 /// Adds an entry mapping the [targetOffset] to [source]. | 21 /// Adds an entry mapping the [targetOffset] to [source]. |
| 21 void addFromOffset(Location source, | 22 /// |
| 22 SourceFile targetFile, int targetOffset, String identifier) { | 23 /// [source] can be either a [Location] or a [SourceLocation]. Using a |
| 24 /// [Location] is deprecated and will be unsupported in version 0.10.0. | |
| 25 void addFromOffset(source, targetFile, int targetOffset, String identifier) { | |
| 23 if (targetFile == null) { | 26 if (targetFile == null) { |
| 24 throw new ArgumentError('targetFile cannot be null'); | 27 throw new ArgumentError('targetFile cannot be null'); |
| 25 } | 28 } |
| 26 _entries.add(new Entry(source, | 29 _entries.add(new Entry(source, |
| 27 new FileLocation(targetFile, targetOffset), identifier)); | 30 new FileLocation(targetFile, targetOffset), identifier)); |
| 28 } | 31 } |
| 29 | 32 |
| 30 /// Adds an entry mapping [target] to [source]. | 33 /// Adds an entry mapping [target] to [source]. |
| 31 void addSpan(Span source, Span target) { | 34 /// |
| 32 var name = source.isIdentifier ? source.text : null; | 35 /// [source] and [target] can be either a [Span] or a [SourceSpan]. Using a |
| 36 /// [Span] is deprecated and will be unsupported in version 0.10.0. | |
| 37 /// | |
| 38 /// If [isIdentifier] is true, this entry is considered to represent an | |
| 39 /// identifier whose value will be stored in the source map. | |
| 40 void addSpan(source, target, {bool isIdentifier}) { | |
| 41 if (isIdentifier == null) isIdentifier = source.isIdentifier; | |
|
Siggi Cherem (dart-lang)
2014/07/18 01:40:53
maybe `isIdentifier == null && source is Span` ?
nweiz
2014/07/23 23:17:18
Moved this under the wrapping code.
| |
| 42 source = SpanWrapper.wrap(source); | |
| 43 target = SpanWrapper.wrap(target); | |
| 44 | |
| 45 var name = isIdentifier ? source.text : null; | |
| 33 _entries.add(new Entry(source.start, target.start, name)); | 46 _entries.add(new Entry(source.start, target.start, name)); |
| 34 } | 47 } |
| 35 | 48 |
| 36 void addLocation(Location source, Location target, String identifier) { | 49 /// Adds an entry mapping [target] to [source]. |
| 50 /// | |
| 51 /// [source] and [target] can be either a [Location] or a [SourceLocation]. | |
| 52 /// Using a [Location] is deprecated and will be unsupported in version | |
| 53 /// 0.10.0. | |
| 54 void addLocation(source, target, String identifier) { | |
| 37 _entries.add(new Entry(source, target, identifier)); | 55 _entries.add(new Entry(source, target, identifier)); |
| 38 } | 56 } |
| 39 | 57 |
| 40 /// Encodes all mappings added to this builder as a json map. | 58 /// Encodes all mappings added to this builder as a json map. |
| 41 Map build(String fileUrl) { | 59 Map build(String fileUrl) { |
| 42 return new SingleMapping.fromEntries(this._entries, fileUrl).toJson(); | 60 return new SingleMapping.fromEntries(this._entries, fileUrl).toJson(); |
| 43 } | 61 } |
| 44 | 62 |
| 45 /// Encodes all mappings added to this builder as a json string. | 63 /// Encodes all mappings added to this builder as a json string. |
| 46 String toJson(String fileUrl) => JSON.encode(build(fileUrl)); | 64 String toJson(String fileUrl) => JSON.encode(build(fileUrl)); |
| 47 } | 65 } |
| 48 | 66 |
| 49 /// An entry in the source map builder. | 67 /// An entry in the source map builder. |
| 50 class Entry implements Comparable { | 68 class Entry implements Comparable { |
| 51 /// Span denoting the original location in the input source file | 69 /// Span denoting the original location in the input source file |
| 52 final Location source; | 70 final Location source; |
| 53 | 71 |
| 54 /// Span indicating the corresponding location in the target file. | 72 /// Span indicating the corresponding location in the target file. |
| 55 final Location target; | 73 final Location target; |
| 56 | 74 |
| 57 /// An identifier name, when this location is the start of an identifier. | 75 /// An identifier name, when this location is the start of an identifier. |
| 58 final String identifierName; | 76 final String identifierName; |
| 59 | 77 |
| 60 Entry(this.source, this.target, this.identifierName); | 78 /// Creates a new [Entry] mapping [target] to [source]. |
| 79 /// | |
| 80 /// [source] and [target] can be either a [Location] or a [SourceLocation]. | |
| 81 /// Using a [Location] is deprecated and will be unsupported in version | |
| 82 /// 0.10.0. | |
| 83 Entry(source, target, this.identifierName) | |
| 84 : source = LocationWrapper.wrap(source), | |
| 85 target = LocationWrapper.wrap(target); | |
| 61 | 86 |
| 62 /// Implements [Comparable] to ensure that entries are ordered by their | 87 /// Implements [Comparable] to ensure that entries are ordered by their |
| 63 /// location in the target file. We sort primarily by the target offset | 88 /// location in the target file. We sort primarily by the target offset |
| 64 /// because source map files are encoded by printing each mapping in order as | 89 /// because source map files are encoded by printing each mapping in order as |
| 65 /// they appear in the target file. | 90 /// they appear in the target file. |
| 66 int compareTo(Entry other) { | 91 int compareTo(Entry other) { |
| 67 int res = target.compareTo(other.target); | 92 int res = target.compareTo(other.target); |
| 68 if (res != 0) return res; | 93 if (res != 0) return res; |
| 69 res = source.sourceUrl.compareTo(other.source.sourceUrl); | 94 res = source.sourceUrl.compareTo(other.source.sourceUrl); |
| 70 if (res != 0) return res; | 95 if (res != 0) return res; |
| 71 return source.compareTo(other.source); | 96 return source.compareTo(other.source); |
| 72 } | 97 } |
| 73 } | 98 } |
| OLD | NEW |