@@ -11,6 +11,7 @@ import 'package:angular2/src/transform/common/interface_matcher.dart';
1111import 'package:angular2/src/transform/common/logging.dart' ;
1212import 'package:angular2/src/transform/common/names.dart' ;
1313import 'package:angular2/src/transform/common/xhr_impl.dart' ;
14+ import 'package:angular2/src/transform/common/ng_meta.dart' ;
1415import 'package:barback/barback.dart' show AssetId;
1516import 'package:path/path.dart' as path;
1617
@@ -23,14 +24,19 @@ import 'visitors.dart';
2324/// If no Angular 2 `Directive` s are found in `code` , returns the empty
2425/// string unless `forceGenerate` is true, in which case an empty ngDeps
2526/// file is created.
26- Future <String > createNgDeps (
27- AssetReader reader, AssetId assetId, AnnotationMatcher annotationMatcher ,
27+ Future <String > createNgDeps (AssetReader reader, AssetId assetId,
28+ AnnotationMatcher annotationMatcher, NgMeta ngMeta ,
2829 {bool inlineViews}) async {
2930 // TODO(kegluneq): Shortcut if we can determine that there are no
3031 // [Directive]s present, taking into account `export`s.
3132 var writer = new AsyncStringWriter ();
32- var visitor = new CreateNgDepsVisitor (writer, assetId,
33- new XhrImpl (reader, assetId), annotationMatcher, _interfaceMatcher,
33+ var visitor = new CreateNgDepsVisitor (
34+ writer,
35+ assetId,
36+ new XhrImpl (reader, assetId),
37+ annotationMatcher,
38+ _interfaceMatcher,
39+ ngMeta,
3440 inlineViews: inlineViews);
3541 var code = await reader.readAsString (assetId);
3642 parseCompilationUnit (code, name: assetId.path).accept (visitor);
@@ -49,10 +55,19 @@ InterfaceMatcher _interfaceMatcher = new InterfaceMatcher();
4955/// associated .ng_deps.dart file.
5056class CreateNgDepsVisitor extends Object with SimpleAstVisitor <Object > {
5157 final AsyncStringWriter writer;
58+
59+ /// Output ngMeta information about aliases.
60+ // TODO(sigmund): add more to ngMeta. Currently this only contains aliasing
61+ // information, but we could produce here all the metadata we need and avoid
62+ // parsing the ngdeps files later.
63+ final NgMeta ngMeta;
64+
5265 /// Whether an Angular 2 `Injectable` has been found.
5366 bool _foundNgInjectable = false ;
67+
5468 /// Whether this library `imports` or `exports` any non-'dart:' libraries.
5569 bool _usesNonLangLibs = false ;
70+
5671 /// Whether we have written an import of base file
5772 /// (the file we are processing).
5873 bool _wroteBaseLibImport = false ;
@@ -66,8 +81,13 @@ class CreateNgDepsVisitor extends Object with SimpleAstVisitor<Object> {
6681 /// The assetId for the file which we are parsing.
6782 final AssetId assetId;
6883
69- CreateNgDepsVisitor (AsyncStringWriter writer, AssetId assetId, XHR xhr,
70- AnnotationMatcher annotationMatcher, InterfaceMatcher interfaceMatcher,
84+ CreateNgDepsVisitor (
85+ AsyncStringWriter writer,
86+ AssetId assetId,
87+ XHR xhr,
88+ AnnotationMatcher annotationMatcher,
89+ InterfaceMatcher interfaceMatcher,
90+ this .ngMeta,
7191 {bool inlineViews})
7292 : writer = writer,
7393 _copyVisitor = new ToSourceVisitor (writer),
@@ -223,6 +243,29 @@ class CreateNgDepsVisitor extends Object with SimpleAstVisitor<Object> {
223243 return null ;
224244 }
225245
246+ @override
247+ Object visitTopLevelVariableDeclaration (TopLevelVariableDeclaration node) {
248+ // We process any top-level declaration that fits the directive-alias
249+ // declaration pattern. Ideally we would use an annotation on the field to
250+ // help us filter out only what's needed, but unfortunately TypeScript
251+ // doesn't support decorators on variable declarations (see
252+ // angular/angular#1747 and angular/ts2dart#249 for context).
253+ outer: for (var variable in node.variables.variables) {
254+ var initializer = variable.initializer;
255+ if (initializer != null && initializer is ListLiteral ) {
256+ var otherNames = [];
257+ for (var exp in initializer.elements) {
258+ // Only simple identifiers are supported for now.
259+ // TODO(sigmund): add support for prefixes (see issue #3232).
260+ if (exp is ! SimpleIdentifier ) continue outer;
261+ otherNames.add (exp.name);
262+ }
263+ ngMeta.aliases[variable.name.name] = otherNames;
264+ }
265+ }
266+ return null ;
267+ }
268+
226269 Object _nodeToSource (AstNode node) {
227270 if (node == null ) return null ;
228271 return node.accept (_copyVisitor);
0 commit comments