Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit 9355e2d

Browse files
authored
Resolve a number of open issues (#86)
* Add new features. * More changes. * Dartfmt. * Yegors issue. * Address feedback and merge. * Oops.
1 parent d1d4194 commit 9355e2d

File tree

10 files changed

+260
-45
lines changed

10 files changed

+260
-45
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## 1.0.0-beta+4
2+
3+
- Renamed `PartBuilder` to `PartOfBuilder`.
4+
- Added a new class, `PartBuilder`, to represent `part '...dart'` directives.
5+
- Added the `HasAnnotations` interface to all library/part/directive builders.
6+
- Added `asFactory` and `asConst` to `ConstructorBuilder`.
7+
- Added `ConstructorBuilder.redirectTo` for a redirecting factory constructor.
8+
- Added a `name` getter to `ReferenceBuilder`.
9+
- Supplying an empty constructor name (`''`) is equivalent to `null` (default).
10+
- Automatically encodes string literals with multiple lines as `'''`.
11+
112
## 1.0.0-beta+3
213

314
- Added support for `genericTypes` parameter for `ExpressionBuilder#invoke`:

lib/code_builder.dart

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ export 'src/builders/expression.dart'
1010
export 'src/builders/field.dart'
1111
show varConst, varField, varFinal, FieldBuilder;
1212
export 'src/builders/file.dart'
13-
show ExportBuilder, ImportBuilder, LibraryBuilder, PartBuilder;
13+
show
14+
ExportBuilder,
15+
ImportBuilder,
16+
LibraryBuilder,
17+
PartBuilder,
18+
PartOfBuilder;
1419
export 'src/builders/method.dart'
1520
show
1621
constructor,

lib/src/builders/file.dart

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
import 'package:analyzer/analyzer.dart';
66
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
7+
import 'package:code_builder/src/builders/annotation.dart';
78
import 'package:code_builder/src/builders/shared.dart';
89
import 'package:code_builder/src/builders/statement.dart';
910
import '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.
1415
abstract 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

121125
class _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,

lib/src/builders/method.dart

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ typedef void _AddParameter(ConstructorBuilder constructor);
183183
abstract class ConstructorBuilder
184184
implements
185185
AstBuilder<ConstructorDeclaration>,
186+
HasAnnotations,
186187
HasParameters,
187188
HasStatements,
188189
ValidClassMember {
@@ -193,8 +194,18 @@ abstract class ConstructorBuilder
193194
String name,
194195
String superName,
195196
List<ExpressionBuilder> invokeSuper,
197+
bool asConst,
198+
bool asFactory,
196199
}) = _NormalConstructorBuilder;
197200

201+
/// Create a new [ConstructorBuilder] that redirects to another constructor.
202+
factory ConstructorBuilder.redirectTo(
203+
String name,
204+
TypeBuilder redirectToClass, {
205+
String constructor,
206+
bool asConst,
207+
}) = _RedirectingConstructorBuilder;
208+
198209
/// Adds a field initializer to this constructor.
199210
void addInitializer(
200211
String fieldName, {
@@ -541,10 +552,78 @@ class _NamedParameterWrapper
541552
AstNode buildAst([_]) => throw new UnsupportedError('Use within method');
542553
}
543554

555+
class _RedirectingConstructorBuilder extends Object
556+
with HasAnnotationsMixin, HasParametersMixin
557+
implements ConstructorBuilder {
558+
final String name;
559+
final TypeBuilder redirectToClass;
560+
final bool asConst;
561+
final String constructor;
562+
563+
_RedirectingConstructorBuilder(
564+
this.name,
565+
this.redirectToClass, {
566+
this.asConst: false,
567+
this.constructor,
568+
});
569+
570+
@override
571+
void addInitializer(
572+
String fieldName, {
573+
ExpressionBuilder toExpression,
574+
String toParameter,
575+
}) {
576+
throw new UnsupportedError('Not valid for redirect constructors');
577+
}
578+
579+
@override
580+
void addStatement(StatementBuilder statement) {
581+
throw new UnsupportedError('Not valid for redirect constructors');
582+
}
583+
584+
@override
585+
void addStatements(Iterable<StatementBuilder> statements) {
586+
throw new UnsupportedError('Not valid for redirect constructors');
587+
}
588+
589+
@override
590+
ConstructorDeclaration buildAst([_]) {
591+
throw new UnsupportedError('Can only be built as part of a class.');
592+
}
593+
594+
@override
595+
ConstructorDeclaration buildConstructor(
596+
TypeBuilder returnType, [
597+
Scope scope,
598+
]) {
599+
return astFactory.constructorDeclaration(
600+
null,
601+
buildAnnotations(scope),
602+
null,
603+
asConst ? $const : null,
604+
$factory,
605+
returnType.buildType(scope).name,
606+
name != null ? $period : null,
607+
name != null ? stringIdentifier(name) : null,
608+
buildParameterList(scope),
609+
null,
610+
null,
611+
astFactory.constructorName(
612+
redirectToClass.buildType(scope),
613+
constructor != null ? $period : null,
614+
constructor != null ? stringIdentifier(constructor) : null,
615+
),
616+
astFactory.emptyFunctionBody($semicolon),
617+
);
618+
}
619+
}
620+
544621
class _NormalConstructorBuilder extends Object
545622
with HasAnnotationsMixin, HasParametersMixin, HasStatementsMixin
546623
implements ConstructorBuilder {
547624
final _initializers = <String, ExpressionBuilder>{};
625+
final bool _asFactory;
626+
final bool _asConst;
548627
final String _name;
549628
final String _superName;
550629
final List<ExpressionBuilder> _superInvocation;
@@ -553,10 +632,14 @@ class _NormalConstructorBuilder extends Object
553632
List<ExpressionBuilder> invokeSuper,
554633
String name,
555634
String superName,
635+
bool asConst: false,
636+
bool asFactory: false,
556637
})
557638
: _name = name,
558639
_superInvocation = invokeSuper,
559-
_superName = superName;
640+
_superName = superName,
641+
_asConst = asConst,
642+
_asFactory = asFactory;
560643

561644
@override
562645
void addInitializer(
@@ -611,8 +694,8 @@ class _NormalConstructorBuilder extends Object
611694
null,
612695
buildAnnotations(scope),
613696
null,
614-
null,
615-
null,
697+
_asConst ? $const : null,
698+
_asFactory ? $factory : null,
616699
returnType.buildType(scope).name,
617700
_name != null ? $period : null,
618701
_name != null ? stringIdentifier(_name) : null,

0 commit comments

Comments
 (0)