@@ -16,17 +16,13 @@ import 'visitors.dart';
1616/// If no Angular 2 `Directive` s are found in [code] , returns the empty
1717/// string unless [forceGenerate] is true, in which case an empty ngDeps
1818/// file is created.
19- String createNgDeps (String code, String path, { bool forceGenerate : false } ) {
19+ String createNgDeps (String code, String path) {
2020 // TODO(kegluneq): Shortcut if we can determine that there are no
21- // [Directive]s present.
21+ // [Directive]s present, taking into account `export`s .
2222 var writer = new PrintStringWriter ();
2323 var visitor = new CreateNgDepsVisitor (writer, path);
2424 parseCompilationUnit (code, name: path).accept (visitor);
25- if (visitor.foundNgDirectives || forceGenerate) {
26- return writer.toString ();
27- } else {
28- return '' ;
29- }
25+ return '$writer ' ;
3026}
3127
3228/// Visitor responsible for processing [CompilationUnit] and creating an
@@ -35,8 +31,8 @@ class CreateNgDepsVisitor extends Object
3531 with SimpleAstVisitor <Object >, VisitorMixin {
3632 final PrintWriter writer;
3733 final _Tester _tester = const _Tester ();
38- bool foundNgDirectives = false ;
39- bool wroteImport = false ;
34+ bool _foundNgDirectives = false ;
35+ bool _wroteImport = false ;
4036 final ToSourceVisitor _copyVisitor;
4137 final FactoryTransformVisitor _factoryVisitor;
4238 final ParameterTransformVisitor _paramsVisitor;
@@ -61,33 +57,35 @@ class CreateNgDepsVisitor extends Object
6157 return null ;
6258 }
6359
64- void _writeImport () {
60+ /// Write the import to the file the .ng_deps.dart file is based on if it
61+ /// has not yet been written.
62+ void _maybeWriteImport () {
63+ if (_wroteImport) return ;
64+ _wroteImport = true ;
6565 writer.print ('''import '${path .basename (importPath )}';''' );
6666 }
6767
6868 @override
6969 Object visitImportDirective (ImportDirective node) {
70- if (! wroteImport) {
71- _writeImport ();
72- wroteImport = true ;
73- }
70+ _maybeWriteImport ();
7471 return node.accept (_copyVisitor);
7572 }
7673
7774 @override
7875 Object visitExportDirective (ExportDirective node) {
76+ _maybeWriteImport ();
7977 return node.accept (_copyVisitor);
8078 }
8179
8280 void _openFunctionWrapper () {
83- // TODO(kegluneq): Use a [PrintWriter] with a length getter.
81+ _maybeWriteImport ();
8482 writer.print ('bool _visited = false;'
8583 'void ${SETUP_METHOD_NAME }(${REFLECTOR_VAR_NAME }) {'
8684 'if (_visited) return; _visited = true;' );
8785 }
8886
8987 void _closeFunctionWrapper () {
90- if (foundNgDirectives ) {
88+ if (_foundNgDirectives ) {
9189 writer.print (';' );
9290 }
9391 writer.print ('}' );
@@ -135,10 +133,10 @@ class CreateNgDepsVisitor extends Object
135133 if (shouldProcess) {
136134 var ctor = _getCtor (node);
137135
138- if (! foundNgDirectives ) {
136+ if (! _foundNgDirectives ) {
139137 // The receiver for cascaded calls.
140138 writer.print (REFLECTOR_VAR_NAME );
141- foundNgDirectives = true ;
139+ _foundNgDirectives = true ;
142140 }
143141 writer.print ('..registerType(' );
144142 visitNode (node.name);
@@ -168,7 +166,14 @@ class CreateNgDepsVisitor extends Object
168166 }
169167
170168 @override
171- Object visitLibraryDirective (LibraryDirective node) => _nodeToSource (node);
169+ Object visitLibraryDirective (LibraryDirective node) {
170+ if (node != null && node.name != null ) {
171+ writer.print ('library ' );
172+ _nodeToSource (node.name);
173+ writer.print ('$DEPS_EXTENSION ;' );
174+ }
175+ return null ;
176+ }
172177
173178 @override
174179 Object visitPartOfDirective (PartOfDirective node) {
@@ -194,6 +199,7 @@ class _Tester {
194199 var metaName = meta.name.toString ();
195200 return metaName == 'Component' ||
196201 metaName == 'Decorator' ||
202+ metaName == 'Injectable' ||
197203 metaName == 'Template' ;
198204 }
199205}
0 commit comments