Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
Next update: closer to a final release. (#141)
Browse files Browse the repository at this point in the history
* Fix a number of issues.

* OK, add some tests.

* Add closures, some getters.

* Another refactor.

* Added FunctionType.

* Updated README.

* Dartfmt.

* Fix CHANGELOG.
  • Loading branch information
matanlurey authored Oct 16, 2017
1 parent 709b663 commit 9f922c9
Show file tree
Hide file tree
Showing 30 changed files with 753 additions and 123 deletions.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
## 2.0.0-alpha+3

* Added `Expression.annotation` and `Expression.annotationNamed`.
* Added `Method.closure` to create an `Expression`.
* Added `FunctionType`.
* Added `{new|const}InstanceNamed` to `Expression` [#135](https://github.com/dart-lang/code_builder/issues/135).
* Also added a `typeArguments` option to all invocations.
* **BUG FIX**: `Block` now implements `Code` [#136](https://github.com/dart-lang/code_builder/issues/136).
* Added `assign{...}` variants to `Expression` [#137](https://github.com/dart-lang/code_builder/issues/137).
* Added `.awaited` and `.returned` to `Expression` [#138](https://github.com/dart-lang/code_builder/issues/138).

* **BUG FIX**: `Block` now implements `Code` [#136](https://github.com/dart-lang/code_builder/issues/136).
* **BUG FIX**: `new DartEmitter.scoped()` applies prefixing [#139](https://github.com/dart-lang/code_builder/issues/139).

* Renamed many of the `.asFoo(...)` and `.toFoo(...)` methods to single getter:
* `asCode()` to `code`
* `asStatement()` to `statement`
* `toExpression()` to `expression`

* Moved `{new|const}Instance{[Named]}` from `Expression` to `Reference`.

## 2.0.0-alpha+2

* Upgraded `build_runner` from `^0.3.0` to `>=0.4.0 <0.6.0`.
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ import 'package:dart_style/dart_style.dart';
void main() {
final animal = new Class((b) => b
..name = 'Animal'
..extend = refer('Organism').toType()
..extend = refer('Organism')
..methods.add(new Method.returnsVoid((b) => b
..name = 'eat'
..lambda = true
..body = const Code('print(\'Yum\')'))));
final emitter = const DartEmitter();
final emitter = new DartEmitter();
print(new DartFormatter().format('${animal.accept(emitter)}'));
}
```
Expand All @@ -68,11 +68,11 @@ import 'package:dart_style/dart_style.dart';
void main() {
final library = new File((b) => b.body.addAll([
new Method((b) => b
..body = new Code((b) => b.code = '')
..body = const Code('')
..name = 'doThing'
..returns = refer('Thing', 'package:a/a.dart')),
new Method((b) => b
..body = new Code((b) => b..code = '')
..body = const Code('')
..name = 'doOther'
..returns = refer('Other', 'package:b/b.dart')),
]));
Expand Down
3 changes: 2 additions & 1 deletion lib/code_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export 'src/specs/directive.dart'
show Directive, DirectiveType, DirectiveBuilder;
export 'src/specs/expression.dart'
show
AsCodeExpression,
ToCodeExpression,
BinaryExpression,
CodeExpression,
Expression,
Expand Down Expand Up @@ -46,4 +46,5 @@ export 'src/specs/method.dart'
Parameter,
ParameterBuilder;
export 'src/specs/reference.dart' show refer, Reference;
export 'src/specs/type_function.dart' show FunctionType, FunctionTypeBuilder;
export 'src/specs/type_reference.dart' show TypeReference, TypeReferenceBuilder;
94 changes: 74 additions & 20 deletions lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'specs/field.dart';
import 'specs/file.dart';
import 'specs/method.dart';
import 'specs/reference.dart';
import 'specs/type_function.dart';
import 'specs/type_reference.dart';
import 'visitors.dart';

Expand Down Expand Up @@ -80,27 +81,38 @@ class DartEmitter extends Object
output.write('abstract ');
}
output.write('class ${spec.name}');
visitTypeParameters(spec.types.map((r) => r.toType()), output);
visitTypeParameters(spec.types.map((r) => r.type), output);
if (spec.extend != null) {
output.write(' extends ');
visitType(spec.extend.toType(), output);
visitType(spec.extend.type, output);
}
if (spec.mixins.isNotEmpty) {
output
..write(' with ')
..writeAll(
spec.mixins.map<StringSink>((m) => visitType(m.toType())), ',');
..writeAll(spec.mixins.map<StringSink>((m) => visitType(m.type)), ',');
}
if (spec.implements.isNotEmpty) {
output
..write(' implements ')
..writeAll(
spec.implements.map<StringSink>((m) => visitType(m.toType())), ',');
spec.implements.map<StringSink>((m) => visitType(m.type)), ',');
}
output.write(' {');
spec.constructors.forEach((c) => visitConstructor(c, spec.name, output));
spec.fields.forEach((f) => visitField(f, output));
spec.methods.forEach((m) => visitMethod(m, output));
spec.constructors.forEach((c) {
visitConstructor(c, spec.name, output);
output.writeln();
});
spec.fields.forEach((f) {
visitField(f, output);
output.writeln();
});
spec.methods.forEach((m) {
visitMethod(m, output);
if (m.lambda) {
output.write(';');
}
output.writeln();
});
output.writeln(' }');
return output;
}
Expand Down Expand Up @@ -170,7 +182,7 @@ class DartEmitter extends Object
}
if (spec.redirect != null) {
output.write(' = ');
visitType(spec.redirect.toType(), output);
visitType(spec.redirect.type, output);
output.write(';');
} else if (spec.body != null) {
if (spec.lambda) {
Expand Down Expand Up @@ -239,7 +251,7 @@ class DartEmitter extends Object
break;
}
if (spec.type != null) {
visitType(spec.type.toType(), output);
visitType(spec.type.type, output);
output.write(' ');
}
output.write(spec.name);
Expand All @@ -258,6 +270,9 @@ class DartEmitter extends Object
final body = new StringBuffer();
for (final spec in spec.body) {
body.write(visitSpec(spec));
if (spec is Method && spec.lambda) {
output.write(';');
}
}
// TODO: Allow some sort of logical ordering.
for (final directive in spec.directives) {
Expand All @@ -270,6 +285,46 @@ class DartEmitter extends Object
return output;
}

@override
visitFunctionType(FunctionType spec, [StringSink output]) {
output ??= new StringBuffer();
if (spec.returnType != null) {
spec.returnType.accept(this, output);
output.write(' ');
}
output.write('Function');
if (spec.types.isNotEmpty) {
output.write('<');
visitAll<Reference>(spec.types, output, (spec) {
spec.accept(this, output);
});
output.write('>');
}
output.write('(');
visitAll<Reference>(spec.requiredParameters, output, (spec) {
spec.accept(this, output);
});
if (spec.optionalParameters.isNotEmpty ||
spec.namedParameters.isNotEmpty && spec.requiredParameters.isNotEmpty) {
output.write(', ');
}
if (spec.optionalParameters.isNotEmpty) {
output.write('[');
visitAll<Reference>(spec.optionalParameters, output, (spec) {
spec.accept(this, output);
});
output.write(']');
} else if (spec.namedParameters.isNotEmpty) {
output.write('{');
visitAll<String>(spec.namedParameters.keys, output, (name) {
spec.namedParameters[name].accept(this, output);
output..write(' ')..write(name);
});
output.write('}');
}
return output..write(')');
}

@override
visitMethod(Method spec, [StringSink output]) {
output ??= new StringBuffer();
Expand All @@ -282,7 +337,7 @@ class DartEmitter extends Object
output.write('static ');
}
if (spec.returns != null) {
visitType(spec.returns.toType(), output);
visitType(spec.returns.type, output);
output.write(' ');
}
if (spec.type == MethodType.getter) {
Expand All @@ -291,8 +346,10 @@ class DartEmitter extends Object
if (spec.type == MethodType.setter) {
output.write('set ');
}
output.write(spec.name);
visitTypeParameters(spec.types.map((r) => r.toType()), output);
if (spec.name != null) {
output.write(spec.name);
}
visitTypeParameters(spec.types.map((r) => r.type), output);
output.write('(');
if (spec.requiredParameters.isNotEmpty) {
var count = 0;
Expand Down Expand Up @@ -348,15 +405,12 @@ class DartEmitter extends Object
output.write(' { ');
}
spec.body.accept(this, output);
if (spec.lambda) {
output.write(';');
} else {
if (!spec.lambda) {
output.write(' } ');
}
} else {
output.write(';');
}
output.writeln();
return output;
}

Expand All @@ -370,7 +424,7 @@ class DartEmitter extends Object
spec.docs.forEach(output.writeln);
spec.annotations.forEach((a) => visitAnnotation(a, output));
if (spec.type != null) {
visitType(spec.type.toType(), output);
visitType(spec.type.type, output);
output.write(' ');
}
if (spec.toThis) {
Expand Down Expand Up @@ -401,9 +455,9 @@ class DartEmitter extends Object
visitReference(spec, output);
if (spec.bound != null) {
output.write(' extends ');
visitType(spec.bound.toType(), output);
visitType(spec.bound.type, output);
}
visitTypeParameters(spec.types.map((r) => r.toType()), output);
visitTypeParameters(spec.types.map((r) => r.type), output);
return output;
}

Expand Down
4 changes: 4 additions & 0 deletions lib/src/specs/annotation.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/src/specs/class.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/src/specs/code.dart
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ abstract class BlockBuilder implements Builder<Block, BlockBuilder> {
///
/// **NOTE**: Not all expressions are _useful_ statements.
void addExpression(Expression expression) {
statements.add(expression.asStatement());
statements.add(expression.statement);
}

ListBuilder<Code> statements = new ListBuilder<Code>();
Expand Down
4 changes: 4 additions & 0 deletions lib/src/specs/code.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/src/specs/constructor.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions lib/src/specs/directive.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9f922c9

Please sign in to comment.