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

Commit 9f922c9

Browse files
authored
Next update: closer to a final release. (#141)
* Fix a number of issues. * OK, add some tests. * Add closures, some getters. * Another refactor. * Added FunctionType. * Updated README. * Dartfmt. * Fix CHANGELOG.
1 parent 709b663 commit 9f922c9

30 files changed

+753
-123
lines changed

CHANGELOG.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
## 2.0.0-alpha+3
22

3+
* Added `Expression.annotation` and `Expression.annotationNamed`.
4+
* Added `Method.closure` to create an `Expression`.
5+
* Added `FunctionType`.
36
* Added `{new|const}InstanceNamed` to `Expression` [#135](https://github.com/dart-lang/code_builder/issues/135).
47
* Also added a `typeArguments` option to all invocations.
5-
* **BUG FIX**: `Block` now implements `Code` [#136](https://github.com/dart-lang/code_builder/issues/136).
68
* Added `assign{...}` variants to `Expression` [#137](https://github.com/dart-lang/code_builder/issues/137).
79
* Added `.awaited` and `.returned` to `Expression` [#138](https://github.com/dart-lang/code_builder/issues/138).
10+
11+
* **BUG FIX**: `Block` now implements `Code` [#136](https://github.com/dart-lang/code_builder/issues/136).
812
* **BUG FIX**: `new DartEmitter.scoped()` applies prefixing [#139](https://github.com/dart-lang/code_builder/issues/139).
913

14+
* Renamed many of the `.asFoo(...)` and `.toFoo(...)` methods to single getter:
15+
* `asCode()` to `code`
16+
* `asStatement()` to `statement`
17+
* `toExpression()` to `expression`
18+
19+
* Moved `{new|const}Instance{[Named]}` from `Expression` to `Reference`.
20+
1021
## 2.0.0-alpha+2
1122

1223
* Upgraded `build_runner` from `^0.3.0` to `>=0.4.0 <0.6.0`.

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ import 'package:dart_style/dart_style.dart';
4040
void main() {
4141
final animal = new Class((b) => b
4242
..name = 'Animal'
43-
..extend = refer('Organism').toType()
43+
..extend = refer('Organism')
4444
..methods.add(new Method.returnsVoid((b) => b
4545
..name = 'eat'
4646
..lambda = true
4747
..body = const Code('print(\'Yum\')'))));
48-
final emitter = const DartEmitter();
48+
final emitter = new DartEmitter();
4949
print(new DartFormatter().format('${animal.accept(emitter)}'));
5050
}
5151
```
@@ -68,11 +68,11 @@ import 'package:dart_style/dart_style.dart';
6868
void main() {
6969
final library = new File((b) => b.body.addAll([
7070
new Method((b) => b
71-
..body = new Code((b) => b.code = '')
71+
..body = const Code('')
7272
..name = 'doThing'
7373
..returns = refer('Thing', 'package:a/a.dart')),
7474
new Method((b) => b
75-
..body = new Code((b) => b..code = '')
75+
..body = const Code('')
7676
..name = 'doOther'
7777
..returns = refer('Other', 'package:b/b.dart')),
7878
]));

lib/code_builder.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export 'src/specs/directive.dart'
1515
show Directive, DirectiveType, DirectiveBuilder;
1616
export 'src/specs/expression.dart'
1717
show
18-
AsCodeExpression,
18+
ToCodeExpression,
1919
BinaryExpression,
2020
CodeExpression,
2121
Expression,
@@ -46,4 +46,5 @@ export 'src/specs/method.dart'
4646
Parameter,
4747
ParameterBuilder;
4848
export 'src/specs/reference.dart' show refer, Reference;
49+
export 'src/specs/type_function.dart' show FunctionType, FunctionTypeBuilder;
4950
export 'src/specs/type_reference.dart' show TypeReference, TypeReferenceBuilder;

lib/src/emitter.dart

Lines changed: 74 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'specs/field.dart';
1616
import 'specs/file.dart';
1717
import 'specs/method.dart';
1818
import 'specs/reference.dart';
19+
import 'specs/type_function.dart';
1920
import 'specs/type_reference.dart';
2021
import 'visitors.dart';
2122

@@ -80,27 +81,38 @@ class DartEmitter extends Object
8081
output.write('abstract ');
8182
}
8283
output.write('class ${spec.name}');
83-
visitTypeParameters(spec.types.map((r) => r.toType()), output);
84+
visitTypeParameters(spec.types.map((r) => r.type), output);
8485
if (spec.extend != null) {
8586
output.write(' extends ');
86-
visitType(spec.extend.toType(), output);
87+
visitType(spec.extend.type, output);
8788
}
8889
if (spec.mixins.isNotEmpty) {
8990
output
9091
..write(' with ')
91-
..writeAll(
92-
spec.mixins.map<StringSink>((m) => visitType(m.toType())), ',');
92+
..writeAll(spec.mixins.map<StringSink>((m) => visitType(m.type)), ',');
9393
}
9494
if (spec.implements.isNotEmpty) {
9595
output
9696
..write(' implements ')
9797
..writeAll(
98-
spec.implements.map<StringSink>((m) => visitType(m.toType())), ',');
98+
spec.implements.map<StringSink>((m) => visitType(m.type)), ',');
9999
}
100100
output.write(' {');
101-
spec.constructors.forEach((c) => visitConstructor(c, spec.name, output));
102-
spec.fields.forEach((f) => visitField(f, output));
103-
spec.methods.forEach((m) => visitMethod(m, output));
101+
spec.constructors.forEach((c) {
102+
visitConstructor(c, spec.name, output);
103+
output.writeln();
104+
});
105+
spec.fields.forEach((f) {
106+
visitField(f, output);
107+
output.writeln();
108+
});
109+
spec.methods.forEach((m) {
110+
visitMethod(m, output);
111+
if (m.lambda) {
112+
output.write(';');
113+
}
114+
output.writeln();
115+
});
104116
output.writeln(' }');
105117
return output;
106118
}
@@ -170,7 +182,7 @@ class DartEmitter extends Object
170182
}
171183
if (spec.redirect != null) {
172184
output.write(' = ');
173-
visitType(spec.redirect.toType(), output);
185+
visitType(spec.redirect.type, output);
174186
output.write(';');
175187
} else if (spec.body != null) {
176188
if (spec.lambda) {
@@ -239,7 +251,7 @@ class DartEmitter extends Object
239251
break;
240252
}
241253
if (spec.type != null) {
242-
visitType(spec.type.toType(), output);
254+
visitType(spec.type.type, output);
243255
output.write(' ');
244256
}
245257
output.write(spec.name);
@@ -258,6 +270,9 @@ class DartEmitter extends Object
258270
final body = new StringBuffer();
259271
for (final spec in spec.body) {
260272
body.write(visitSpec(spec));
273+
if (spec is Method && spec.lambda) {
274+
output.write(';');
275+
}
261276
}
262277
// TODO: Allow some sort of logical ordering.
263278
for (final directive in spec.directives) {
@@ -270,6 +285,46 @@ class DartEmitter extends Object
270285
return output;
271286
}
272287

288+
@override
289+
visitFunctionType(FunctionType spec, [StringSink output]) {
290+
output ??= new StringBuffer();
291+
if (spec.returnType != null) {
292+
spec.returnType.accept(this, output);
293+
output.write(' ');
294+
}
295+
output.write('Function');
296+
if (spec.types.isNotEmpty) {
297+
output.write('<');
298+
visitAll<Reference>(spec.types, output, (spec) {
299+
spec.accept(this, output);
300+
});
301+
output.write('>');
302+
}
303+
output.write('(');
304+
visitAll<Reference>(spec.requiredParameters, output, (spec) {
305+
spec.accept(this, output);
306+
});
307+
if (spec.optionalParameters.isNotEmpty ||
308+
spec.namedParameters.isNotEmpty && spec.requiredParameters.isNotEmpty) {
309+
output.write(', ');
310+
}
311+
if (spec.optionalParameters.isNotEmpty) {
312+
output.write('[');
313+
visitAll<Reference>(spec.optionalParameters, output, (spec) {
314+
spec.accept(this, output);
315+
});
316+
output.write(']');
317+
} else if (spec.namedParameters.isNotEmpty) {
318+
output.write('{');
319+
visitAll<String>(spec.namedParameters.keys, output, (name) {
320+
spec.namedParameters[name].accept(this, output);
321+
output..write(' ')..write(name);
322+
});
323+
output.write('}');
324+
}
325+
return output..write(')');
326+
}
327+
273328
@override
274329
visitMethod(Method spec, [StringSink output]) {
275330
output ??= new StringBuffer();
@@ -282,7 +337,7 @@ class DartEmitter extends Object
282337
output.write('static ');
283338
}
284339
if (spec.returns != null) {
285-
visitType(spec.returns.toType(), output);
340+
visitType(spec.returns.type, output);
286341
output.write(' ');
287342
}
288343
if (spec.type == MethodType.getter) {
@@ -291,8 +346,10 @@ class DartEmitter extends Object
291346
if (spec.type == MethodType.setter) {
292347
output.write('set ');
293348
}
294-
output.write(spec.name);
295-
visitTypeParameters(spec.types.map((r) => r.toType()), output);
349+
if (spec.name != null) {
350+
output.write(spec.name);
351+
}
352+
visitTypeParameters(spec.types.map((r) => r.type), output);
296353
output.write('(');
297354
if (spec.requiredParameters.isNotEmpty) {
298355
var count = 0;
@@ -348,15 +405,12 @@ class DartEmitter extends Object
348405
output.write(' { ');
349406
}
350407
spec.body.accept(this, output);
351-
if (spec.lambda) {
352-
output.write(';');
353-
} else {
408+
if (!spec.lambda) {
354409
output.write(' } ');
355410
}
356411
} else {
357412
output.write(';');
358413
}
359-
output.writeln();
360414
return output;
361415
}
362416

@@ -370,7 +424,7 @@ class DartEmitter extends Object
370424
spec.docs.forEach(output.writeln);
371425
spec.annotations.forEach((a) => visitAnnotation(a, output));
372426
if (spec.type != null) {
373-
visitType(spec.type.toType(), output);
427+
visitType(spec.type.type, output);
374428
output.write(' ');
375429
}
376430
if (spec.toThis) {
@@ -401,9 +455,9 @@ class DartEmitter extends Object
401455
visitReference(spec, output);
402456
if (spec.bound != null) {
403457
output.write(' extends ');
404-
visitType(spec.bound.toType(), output);
458+
visitType(spec.bound.type, output);
405459
}
406-
visitTypeParameters(spec.types.map((r) => r.toType()), output);
460+
visitTypeParameters(spec.types.map((r) => r.type), output);
407461
return output;
408462
}
409463

lib/src/specs/annotation.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/specs/class.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/specs/code.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ abstract class BlockBuilder implements Builder<Block, BlockBuilder> {
7070
///
7171
/// **NOTE**: Not all expressions are _useful_ statements.
7272
void addExpression(Expression expression) {
73-
statements.add(expression.asStatement());
73+
statements.add(expression.statement);
7474
}
7575

7676
ListBuilder<Code> statements = new ListBuilder<Code>();

lib/src/specs/code.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/specs/constructor.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/specs/directive.g.dart

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)