44
55import 'package:analyzer/analyzer.dart' ;
66import 'package:analyzer/dart/ast/standard_ast_factory.dart' ;
7+ import 'package:code_builder/src/builders/annotation.dart' ;
78import 'package:code_builder/src/builders/shared.dart' ;
89import 'package:code_builder/src/builders/statement.dart' ;
910import 'package:code_builder/src/tokens.dart' ;
1011
1112/// Builds a file of Dart source code.
1213///
13- /// See [LibraryBuilder] and [PartBuilder ] for concrete implementations.
14+ /// See [LibraryBuilder] and [PartOfBuilder ] for concrete implementations.
1415abstract class FileBuilder implements AstBuilder <CompilationUnit > {
1516 final List <AstBuilder > _members = < AstBuilder > [];
1617
@@ -28,8 +29,9 @@ abstract class FileBuilder implements AstBuilder<CompilationUnit> {
2829}
2930
3031/// Builds a standalone file (library) of Dart source code.
31- class LibraryBuilder extends FileBuilder {
32+ class LibraryBuilder extends FileBuilder with HasAnnotationsMixin {
3233 final List <AstBuilder <Directive >> _directives = < AstBuilder <Directive >> [];
34+ final String _name;
3335 final Scope _scope;
3436
3537 /// Creates a new standalone Dart library, optionally with [name] .
@@ -43,11 +45,7 @@ class LibraryBuilder extends FileBuilder {
4345 return new LibraryBuilder ._(name, scope ?? new Scope ());
4446 }
4547
46- LibraryBuilder ._(String name, this ._scope) : super ._() {
47- if (name != null ) {
48- _directives.add (new _LibraryDirectiveBuilder (name));
49- }
50- }
48+ LibraryBuilder ._(this ._name, this ._scope) : super ._();
5149
5250 /// Adds a file [directive] .
5351 void addDirective (AstBuilder <Directive > directive) {
@@ -60,14 +58,18 @@ class LibraryBuilder extends FileBuilder {
6058 }
6159
6260 @override
63- CompilationUnit buildAst ([_ ]) {
61+ CompilationUnit buildAst ([Scope scope ]) {
6462 var members = _members.map ((m) {
6563 if (m is TopLevelMixin ) {
6664 return (m as TopLevelMixin ).buildTopLevelAst (_scope);
6765 }
6866 return m.buildAst (_scope);
6967 }).toList ();
70- var directives = < Directive > []
68+ var directives = < Directive > [];
69+ if (_name != null ) {
70+ directives.add (new _LibraryDirectiveBuilder (_name, this ).buildAst (scope));
71+ }
72+ directives
7173 ..addAll (_scope.toImports ().map ((d) => d.buildAst ()))
7274 ..addAll (_directives.map ((d) => d.buildAst ()));
7375 return astFactory.compilationUnit (
@@ -81,28 +83,30 @@ class LibraryBuilder extends FileBuilder {
8183}
8284
8385/// Lazily builds a partial file (part of) Dart source code.
84- class PartBuilder extends FileBuilder {
85- final String _name;
86+ class PartOfBuilder extends FileBuilder with HasAnnotationsMixin {
87+ final String _uri;
88+ final Scope _scope;
8689
8790 /// Creates a partial Dart file.
88- factory PartBuilder (String name ) = PartBuilder ._;
91+ factory PartOfBuilder (String uri, [ Scope scope] ) = PartOfBuilder ._;
8992
90- PartBuilder ._(this ._name ) : super ._();
93+ PartOfBuilder ._(this ._uri, [ this ._scope] ) : super ._();
9194
9295 @override
93- CompilationUnit buildAst ([_]) {
96+ CompilationUnit buildAst ([Scope scope]) {
97+ scope ?? = _scope;
9498 return astFactory.compilationUnit (
9599 null ,
96100 null ,
97101 [
98102 astFactory.partOfDirective (
99103 null ,
100- null ,
104+ buildAnnotations (scope) ,
101105 $part,
102106 $of,
103107 null ,
104108 astFactory.libraryIdentifier ([
105- astFactory.simpleIdentifier (stringToken (_name )),
109+ astFactory.simpleIdentifier (stringToken ("'$ _uri '" )),
106110 ]),
107111 $semicolon,
108112 )
@@ -119,15 +123,16 @@ class PartBuilder extends FileBuilder {
119123}
120124
121125class _LibraryDirectiveBuilder implements AstBuilder <LibraryDirective > {
126+ final LibraryBuilder _library;
122127 final String _name;
123128
124- _LibraryDirectiveBuilder (this ._name);
129+ _LibraryDirectiveBuilder (this ._name, this ._library );
125130
126131 @override
127- LibraryDirective buildAst ([_ ]) {
132+ LibraryDirective buildAst ([Scope scope ]) {
128133 return astFactory.libraryDirective (
129134 null ,
130- null ,
135+ _library. buildAnnotations (scope) ,
131136 $library,
132137 astFactory.libraryIdentifier ([
133138 astFactory.simpleIdentifier (
@@ -139,8 +144,31 @@ class _LibraryDirectiveBuilder implements AstBuilder<LibraryDirective> {
139144 }
140145}
141146
147+ /// Lazily builds a [PartDirective] AST when built.
148+ class PartBuilder extends Object
149+ with HasAnnotationsMixin
150+ implements AstBuilder <PartDirective > {
151+ final String _uri;
152+
153+ factory PartBuilder (String uri) = PartBuilder ._;
154+ PartBuilder ._(this ._uri);
155+
156+ @override
157+ PartDirective buildAst ([Scope scope]) {
158+ return astFactory.partDirective (
159+ null ,
160+ buildAnnotations (scope),
161+ $part,
162+ astFactory.simpleStringLiteral (stringToken ("'$_uri '" ), _uri),
163+ $semicolon,
164+ );
165+ }
166+ }
167+
142168/// Lazily builds an [ImportDirective] AST when built.
143- class ImportBuilder implements AstBuilder <ImportDirective > {
169+ class ImportBuilder extends Object
170+ with HasAnnotationsMixin
171+ implements AstBuilder <ImportDirective > {
144172 final String _prefix;
145173 final String _uri;
146174 final bool _deferred;
@@ -171,7 +199,7 @@ class ImportBuilder implements AstBuilder<ImportDirective> {
171199 }
172200
173201 @override
174- ImportDirective buildAst ([_ ]) {
202+ ImportDirective buildAst ([Scope scope ]) {
175203 var combinators = < Combinator > [];
176204 if (_show.isNotEmpty) {
177205 combinators.add (
@@ -191,7 +219,7 @@ class ImportBuilder implements AstBuilder<ImportDirective> {
191219 }
192220 return astFactory.importDirective (
193221 null ,
194- null ,
222+ buildAnnotations (scope) ,
195223 null ,
196224 astFactory.simpleStringLiteral (stringToken ("'$_uri '" ), _uri),
197225 null ,
@@ -205,7 +233,9 @@ class ImportBuilder implements AstBuilder<ImportDirective> {
205233}
206234
207235/// Lazily builds an [ExportDirective] AST when built.
208- class ExportBuilder implements AstBuilder <ExportDirective > {
236+ class ExportBuilder extends Object
237+ with HasAnnotationsMixin
238+ implements AstBuilder <ExportDirective > {
209239 final String _uri;
210240
211241 final Set <String > _show = new Set <String >();
@@ -232,7 +262,7 @@ class ExportBuilder implements AstBuilder<ExportDirective> {
232262 }
233263
234264 @override
235- ExportDirective buildAst ([_ ]) {
265+ ExportDirective buildAst ([Scope scope ]) {
236266 var combinators = < Combinator > [];
237267 if (_show.isNotEmpty) {
238268 combinators.add (
@@ -252,7 +282,7 @@ class ExportBuilder implements AstBuilder<ExportDirective> {
252282 }
253283 return astFactory.exportDirective (
254284 null ,
255- null ,
285+ buildAnnotations (scope) ,
256286 null ,
257287 astFactory.simpleStringLiteral (stringToken ("'$_uri '" ), _uri),
258288 null ,
0 commit comments