format
pass nodes into InitializerData
BUG= https://github.com/dart-lang/polymer-dart/issues/33
R=sigmund@google.com
Review URL: https://codereview.chromium.org//1004033004
diff --git a/CHANGELOG.md b/CHANGELOG.md
index be288d9..b481d29 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,3 +1,9 @@
+## 0.5.1+5
+
+* Fix an issue where for certain programs the transformer could fail,
+ [33](https://github.com/dart-lang/polymer-dart/issues/33).
+
+
## 0.5.1+4
* Update to use mock dart sdk from `code_transformers` and update the `analyzer`
diff --git a/lib/build/initializer_plugin.dart b/lib/build/initializer_plugin.dart
index f70e885..466065f 100644
--- a/lib/build/initializer_plugin.dart
+++ b/lib/build/initializer_plugin.dart
@@ -231,7 +231,7 @@
var element = expression.bestElement;
if (element == null || !element.isPublic) {
logger.error('Private constants are not supported in intializer '
- 'constructors, found $element.');
+ 'constructors, found $element.');
}
libraryPrefixes.putIfAbsent(
element.library, () => 'i${libraryPrefixes.length}');
diff --git a/lib/src/mirror_loader.dart b/lib/src/mirror_loader.dart
index bda2704..ba49566 100644
--- a/lib/src/mirror_loader.dart
+++ b/lib/src/mirror_loader.dart
@@ -45,8 +45,8 @@
// they were seen.
// TODO(jakemac): This is an approximation of what we actually want.
// https://github.com/dart-lang/initialize/issues/25
- var relativeLibraryUris = new List.from(libraries.keys.where(
- (uri) => uri.scheme != 'package' && uri.scheme != 'dart'));
+ var relativeLibraryUris = new List.from(libraries.keys
+ .where((uri) => uri.scheme != 'package' && uri.scheme != 'dart'));
for (var import in relativeLibraryUris.reversed) {
// Always load the package: version of a library if available for
@@ -74,7 +74,6 @@
return queue;
}
-
/// Whether [uri] is an http URI that contains a 'packages' segment, and
/// therefore could be converted into a 'package:' URI.
bool _isHttpStylePackageUrl(Uri uri) {
@@ -87,8 +86,8 @@
}
Uri _packageUriFor(Uri httpUri) {
- var packagePath = httpUri.path.substring(
- httpUri.path.lastIndexOf('packages/') + 'packages/'.length);
+ var packagePath = httpUri.path
+ .substring(httpUri.path.lastIndexOf('packages/') + 'packages/'.length);
return Uri.parse('package:$packagePath');
}
diff --git a/lib/transformer.dart b/lib/transformer.dart
index 76cc98c..caae7a6 100644
--- a/lib/transformer.dart
+++ b/lib/transformer.dart
@@ -62,8 +62,8 @@
var document = parse(html);
var originalDartFile =
_findMainScript(document, transform.primaryInput.id, transform);
- return _buildBootstrapFile(transform, primaryId: originalDartFile).then(
- (AssetId newDartFile) {
+ return _buildBootstrapFile(transform, primaryId: originalDartFile)
+ .then((AssetId newDartFile) {
return _replaceEntryWithBootstrap(transform, document,
transform.primaryInput.id, originalDartFile, newDartFile);
});
@@ -233,8 +233,21 @@
bool _readAnnotations(Element element) {
var found = false;
- element.metadata.where((ElementAnnotation meta) {
+ if (element.metadata.isEmpty) return found;
+
+ var metaNodes;
+ var node = element.node;
+ if (node is SimpleIdentifier && node.parent is LibraryIdentifier) {
+ metaNodes = node.parent.parent.metadata;
+ } else if (node is ClassDeclaration || node is FunctionDeclaration) {
+ metaNodes = node.metadata;
+ } else {
+ return found;
+ }
+
+ metaNodes.where((Annotation metaNode) {
// First filter out anything that is not a Initializer.
+ var meta = metaNode.elementAnnotation;
var e = meta.element;
if (e is PropertyAccessorElement) {
return _isInitializer(e.variable.evaluationResult.value.type);
@@ -242,12 +255,14 @@
return _isInitializer(e.returnType);
}
return false;
- }).where((ElementAnnotation meta) {
+ }).where((Annotation metaNode) {
+ var meta = metaNode.elementAnnotation;
_seenAnnotations.putIfAbsent(element, () => new Set<ElementAnnotation>());
return !_seenAnnotations[element].contains(meta);
- }).forEach((ElementAnnotation meta) {
+ }).forEach((Annotation metaNode) {
+ var meta = metaNode.elementAnnotation;
_seenAnnotations[element].add(meta);
- _initQueue.addLast(new InitializerData._(element, meta));
+ _initQueue.addLast(new InitializerData._(node, metaNode));
found = true;
});
return found;
@@ -310,8 +325,8 @@
_logger.error("Can't import `${id}` from `${_newEntryPoint}`");
} else if (path.url.split(id.path)[0] ==
path.url.split(_newEntryPoint.path)[0]) {
- var relativePath = path.url.relative(
- id.path, from: path.url.dirname(_newEntryPoint.path));
+ var relativePath = path.url.relative(id.path,
+ from: path.url.dirname(_newEntryPoint.path));
buffer.write("import '${relativePath}'");
} else {
_logger.error("Can't import `${id}` from `${_newEntryPoint}`");
@@ -388,71 +403,59 @@
return (new List.from(library.imports)
..addAll(library.exports)
..sort((a, b) {
- // dart: imports don't have a uri
- if (a.uri == null && b.uri != null) return -1;
- if (b.uri == null && a.uri != null) return 1;
- if (a.uri == null && b.uri == null) {
- return getLibrary(a).name.compareTo(getLibrary(b).name);
- }
+ // dart: imports don't have a uri
+ if (a.uri == null && b.uri != null) return -1;
+ if (b.uri == null && a.uri != null) return 1;
+ if (a.uri == null && b.uri == null) {
+ return getLibrary(a).name.compareTo(getLibrary(b).name);
+ }
- // package: imports next
- var aIsPackage = a.uri.startsWith('package:');
- var bIsPackage = b.uri.startsWith('package:');
- if (aIsPackage && !bIsPackage) {
- return -1;
- } else if (bIsPackage && !aIsPackage) {
- return 1;
- } else if (bIsPackage && aIsPackage) {
- return a.uri.compareTo(b.uri);
- }
+ // package: imports next
+ var aIsPackage = a.uri.startsWith('package:');
+ var bIsPackage = b.uri.startsWith('package:');
+ if (aIsPackage && !bIsPackage) {
+ return -1;
+ } else if (bIsPackage && !aIsPackage) {
+ return 1;
+ } else if (bIsPackage && aIsPackage) {
+ return a.uri.compareTo(b.uri);
+ }
- // And finally compare based on the relative uri if both are file paths.
- var aUri = path.url.relative(a.source.uri.path,
- from: path.url.dirname(library.source.uri.path));
- var bUri = path.url.relative(b.source.uri.path,
- from: path.url.dirname(library.source.uri.path));
- return aUri.compareTo(bUri);
- })).map(getLibrary);
+ // And finally compare based on the relative uri if both are file paths.
+ var aUri = path.url.relative(a.source.uri.path,
+ from: path.url.dirname(library.source.uri.path));
+ var bUri = path.url.relative(b.source.uri.path,
+ from: path.url.dirname(library.source.uri.path));
+ return aUri.compareTo(bUri);
+ })).map(getLibrary);
}
}
/// An [Initializer] annotation and the target of that annotation.
class InitializerData {
- /// The target [Element] of the annotation.
- final Element targetElement;
-
- /// The [ElementAnnotation] representing the annotation itself.
- final ElementAnnotation annotationElement;
-
- AstNode _targetNode;
-
/// The target [AstNode] of the annotation.
- // TODO(jakemac): We at least cache this for now, but ideally `targetElement`
- // would actually be the getter, and `targetNode` would be the property.
- AstNode get targetNode {
- if (_targetNode == null) _targetNode = targetElement.node;
- return _targetNode;
- }
+ final AstNode targetNode;
/// The [Annotation] representing the annotation itself.
- Annotation get annotationNode {
- var annotatedNode;
+ final Annotation annotationNode;
+
+ /// The [ElementAnnotation] representing the annotation itself.
+ ElementAnnotation get annotationElement => annotationNode.elementAnnotation;
+
+ /// The target [Element] of the annotation.
+ Element get targetElement {
if (targetNode is SimpleIdentifier &&
targetNode.parent is LibraryIdentifier) {
- annotatedNode = targetNode.parent.parent;
+ return targetNode.parent.parent.element;
} else if (targetNode is ClassDeclaration ||
targetNode is FunctionDeclaration) {
- annotatedNode = targetNode;
+ return targetNode.element;
} else {
return null;
}
- if (annotatedNode is! AnnotatedNode) return null;
- var astMeta = annotatedNode.metadata;
-
- return astMeta.firstWhere((m) => m.elementAnnotation == annotationElement);
}
- InitializerData._(this.targetElement, this.annotationElement);
+ InitializerData._(this.targetNode, this.annotationNode);
}
// Reads a file list from a barback settings configuration field.
diff --git a/pubspec.yaml b/pubspec.yaml
index b10df23..8d17859 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -1,5 +1,5 @@
name: initialize
-version: 0.5.1+4
+version: 0.5.1+5
author: Polymer.dart Authors <web@dartlang.org>
description: Generic building blocks for doing static initialization.
homepage: https://github.com/dart-lang/initialize