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