Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
## 4.4.1-dev

* Only use trailing commas if a collection (parameter list, ...) has many
elements.
* Don't use trailing commas for parameters lists without optional arguments.

## 4.4.0

* Mention how the `allocator` argument relates to imports in the `DartEmitter`
Expand Down
28 changes: 16 additions & 12 deletions lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -141,16 +141,22 @@ class DartEmitter extends Object
visitConstructor(c, spec.name, out);
out.writeln();
}
for (var f in spec.fields) {
visitField(f, out);
if (spec.fields.isNotEmpty) {
out.writeln();
}
for (var m in spec.methods) {
visitMethod(m, out);
if (_isLambdaMethod(m)) {
out.write(';');
for (var f in spec.fields) {
visitField(f, out);
out.writeln();
}
}
if (spec.methods.isNotEmpty) {
out.writeln();
for (var m in spec.methods) {
visitMethod(m, out);
if (_isLambdaMethod(m)) {
out.write(';');
}
out.writeln();
}
}
out.writeln(' }');
return out;
Expand Down Expand Up @@ -223,8 +229,7 @@ class DartEmitter extends Object
for (final p in spec.requiredParameters) {
count++;
_visitParameter(p, output);
if (hasMultipleParameters ||
spec.requiredParameters.length != count ||
if (spec.requiredParameters.length != count ||
spec.optionalParameters.isNotEmpty) {
output.write(', ');
}
Expand Down Expand Up @@ -391,7 +396,7 @@ class DartEmitter extends Object
spec.assignment!.accept(this, output);
});
}
output.writeln(';');
output.write(';');
return output;
}

Expand Down Expand Up @@ -585,8 +590,7 @@ class DartEmitter extends Object
for (final p in spec.requiredParameters) {
count++;
_visitParameter(p, output);
if (hasMultipleParameters ||
spec.requiredParameters.length != count ||
if (spec.requiredParameters.length != count ||
spec.optionalParameters.isNotEmpty) {
output.write(', ');
}
Expand Down
4 changes: 2 additions & 2 deletions lib/src/specs/expression.dart
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ abstract class ExpressionEmitter implements ExpressionVisitor<StringSink> {
});
final argumentCount = expression.positionalArguments.length +
expression.namedArguments.length;
if (argumentCount > 1) {
if (argumentCount > 3) {
out.write(', ');
}
return out..write(')');
Expand Down Expand Up @@ -540,7 +540,7 @@ abstract class ExpressionEmitter implements ExpressionVisitor<StringSink> {
visitAll<Object?>(expression.values, out, (value) {
_acceptLiteral(value, out);
});
if (expression.values.length > 1) {
if (expression.values.length > 3) {
out.write(', ');
}
return out..write(']');
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: code_builder
version: 4.4.0
version: 4.4.1-dev
description: >-
A fluent, builder-based library for generating valid Dart code
repository: https://github.com/dart-lang/code_builder
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/injection_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void main() {
final Module _module;

@override
Thing getThing() => Thing(_module.get1(), _module.get2(), );
Thing getThing() => Thing(_module.get1(), _module.get2());
}
'''),
);
Expand All @@ -59,7 +59,7 @@ void main() {
final _i2.Module _module;

@override
_i3.Thing getThing() => _i3.Thing(_module.get1(), _module.get2(), );
_i3.Thing getThing() => _i3.Thing(_module.get1(), _module.get2());
}
''', DartEmitter(allocator: Allocator.simplePrefixing())),
);
Expand Down
10 changes: 5 additions & 5 deletions test/specs/code/expression_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ void main() {
literal(2),
literal(3),
]),
equalsDart('foo(1, 2, 3, )'),
equalsDart('foo(1, 2, 3)'),
);
});

Expand All @@ -209,7 +209,7 @@ void main() {
'bar': literal(1),
'baz': literal(2),
}),
equalsDart('foo(bar: 1, baz: 2, )'),
equalsDart('foo(bar: 1, baz: 2)'),
);
});

Expand All @@ -221,7 +221,7 @@ void main() {
'bar': literal(2),
'baz': literal(3),
}),
equalsDart('foo(1, bar: 2, baz: 3, )'),
equalsDart('foo(1, bar: 2, baz: 3)'),
);
});

Expand Down Expand Up @@ -373,7 +373,7 @@ void main() {
literalString('foo'),
Method((b) => b..body = literalTrue.code).closure,
]),
equalsDart("map.putIfAbsent('foo', () => true, )"),
equalsDart("map.putIfAbsent('foo', () => true)"),
);
});

Expand All @@ -385,7 +385,7 @@ void main() {
..types.add(refer('T'))
..body = literalTrue.code).genericClosure,
]),
equalsDart("map.putIfAbsent('foo', <T>() => true, )"),
equalsDart("map.putIfAbsent('foo', <T>() => true)"),
);
});

Expand Down
2 changes: 0 additions & 2 deletions test/specs/enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ void main() {
]);

final int? myInt;

final String? myString;
}
'''));
Expand Down Expand Up @@ -336,7 +335,6 @@ void main() {
const MyEnum([this.myInt]);

final int? myInt;

static const String myString = 'abc';
}
'''));
Expand Down
2 changes: 1 addition & 1 deletion test/specs/method_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ void main() {
]),
),
equalsDart(r'''
foo<T extends Iterable>(T t, X<T> x, );
foo<T extends Iterable>(T t, X<T> x);
'''),
);
});
Expand Down