Index: pkg/compiler/lib/src/io/source_information.dart |
diff --git a/pkg/compiler/lib/src/io/source_information.dart b/pkg/compiler/lib/src/io/source_information.dart |
index cf3872cd0e35f229c23db196461c2f062247b6ad..a84d8bfe55077ba296029051e1fa237ade3d80b2 100644 |
--- a/pkg/compiler/lib/src/io/source_information.dart |
+++ b/pkg/compiler/lib/src/io/source_information.dart |
@@ -5,8 +5,15 @@ |
library dart2js.source_information; |
import '../common.dart'; |
-import '../elements/elements.dart' show AstElement, LocalElement, ResolvedAst; |
+import '../elements/elements.dart' |
+ show |
+ AstElement, |
+ CompilationUnitElement, |
+ LocalElement, |
+ ResolvedAst, |
+ ResolvedAstKind; |
import '../js/js.dart' show JavaScriptNodeSourceInformation; |
+import '../script.dart'; |
import '../tree/tree.dart' show Node; |
import 'source_file.dart'; |
@@ -37,8 +44,8 @@ abstract class SourceInformation extends JavaScriptNodeSourceInformation { |
class SourceInformationStrategy { |
const SourceInformationStrategy(); |
- /// Create a [SourceInformationBuilder] for [element]. |
- SourceInformationBuilder createBuilderForContext(AstElement element) { |
+ /// Create a [SourceInformationBuilder] for [resolvedAst]. |
+ SourceInformationBuilder createBuilderForContext(ResolvedAst resolvedAst) { |
return const SourceInformationBuilder(); |
} |
@@ -53,8 +60,8 @@ class SourceInformationStrategy { |
class SourceInformationBuilder { |
const SourceInformationBuilder(); |
- /// Create a [SourceInformationBuilder] for [element]. |
- SourceInformationBuilder forContext(AstElement element) => this; |
+ /// Create a [SourceInformationBuilder] for [resolvedAst]. |
+ SourceInformationBuilder forContext(ResolvedAst resolvedAst) => this; |
/// Generate [SourceInformation] the declaration of the element in |
/// [resolvedAst]. |
@@ -151,7 +158,9 @@ abstract class SourceLocation { |
int _line; |
SourceLocation(this._sourceFile) { |
- assert(isValid); |
+ assert(invariant(new SourceSpan(sourceUri, 0, 0), isValid, |
+ message: "Invalid source location in ${sourceUri}: " |
+ "offset=$offset, length=${_sourceFile.length}.")); |
} |
/// The absolute URI of the source file of this source location. |
@@ -244,3 +253,32 @@ String computeElementNameForSourceMaps(AstElement element) { |
return element.name; |
} |
} |
+ |
+/// Computes the [SourceFile] for the source code of [resolvedAst]. |
+SourceFile computeSourceFile(ResolvedAst resolvedAst) { |
+ SourceFile sourceFile; |
+ if (resolvedAst.kind != ResolvedAstKind.PARSED) { |
+ // Synthesized node. Use the enclosing element for the location. |
+ sourceFile = resolvedAst.element.compilationUnit.script.file; |
+ } else { |
+ Uri uri = resolvedAst.sourceUri; |
+ AstElement implementation = resolvedAst.element.implementation; |
+ Script script = implementation.compilationUnit.script; |
+ if (uri == script.resourceUri) { |
+ sourceFile = script.file; |
+ } else { |
+ // Slow path, happens only for deserialized elements. |
+ // TODO(johnniwinther): Support a way to get a [SourceFile] from a |
+ // [Uri]. |
+ for (CompilationUnitElement compilationUnit |
+ in implementation.library.compilationUnits) { |
+ Script script = compilationUnit.script; |
+ if (uri == script.resourceUri) { |
+ sourceFile = script.file; |
+ break; |
+ } |
+ } |
+ } |
+ } |
+ return sourceFile; |
+} |