Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 4.6.0-wip

* Add support for named arguments in `enum` classes

## 4.5.0

* Require Dart 2.19
Expand Down
14 changes: 12 additions & 2 deletions lib/src/emitter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -800,15 +800,25 @@ class DartEmitter extends Object
out.write('.${v.constructorName}');
}
visitTypeParameters(v.types.map((r) => r.type), out);
final takesArguments =
v.constructorName != null || v.arguments.isNotEmpty;
final takesArguments = v.constructorName != null ||
v.arguments.isNotEmpty ||
v.namedArguments.isNotEmpty;
if (takesArguments) {
out.write('(');
}
if (v.arguments.isNotEmpty) {
out.writeAll(
v.arguments.map<StringSink>((arg) => arg.accept(this)), ', ');
}
if (v.arguments.isNotEmpty && v.namedArguments.isNotEmpty) {
out.write(', ');
}
visitAll<String>(v.namedArguments.keys, out, (name) {
out
..write(name)
..write(': ');
v.namedArguments[name]!.accept(this, out);
});
if (takesArguments) {
out.write(')');
}
Expand Down
6 changes: 6 additions & 0 deletions lib/src/specs/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ abstract class EnumValue extends Object

/// Arguments to the constructor.
BuiltList<Expression> get arguments;

/// Named arguments to the constructor.
BuiltMap<String, Expression> get namedArguments;
}

abstract class EnumValueBuilder extends Object
Expand All @@ -130,4 +133,7 @@ abstract class EnumValueBuilder extends Object

/// Arguments to the constructor.
ListBuilder<Expression> arguments = ListBuilder();

/// Named arguments to the constructor.
MapBuilder<String, Expression> namedArguments = MapBuilder();
}
32 changes: 28 additions & 4 deletions lib/src/specs/enum.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 pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: code_builder
version: 4.5.0
version: 4.6.0-wip
description: >-
A fluent, builder-based library for generating valid Dart code
repository: https://github.com/dart-lang/code_builder
Expand Down
58 changes: 58 additions & 0 deletions test/specs/enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -381,4 +381,62 @@ void main() {
}
'''));
});

test('should create an enum which named and unnamed constructor parameters',
() {
final myEnum = Enum((b) => b
..name = 'MyEnum'
..constructors.add(Constructor((c) => c
..constant = true
..requiredParameters.addAll([
Parameter((p) => p
..toThis = true
..name = 'myInt')
])
..optionalParameters.addAll([
Parameter((p) => p
..toThis = true
..named = true
..required = true
..name = 'myString')
])))
..fields.addAll([
Field((f) => f
..modifier = FieldModifier.final$
..type = refer('int?')
..name = 'myInt'),
Field((f) => f
..modifier = FieldModifier.final$
..type = refer('String?')
..name = 'myString')
])
..values.addAll([
EnumValue((v) => v..name = 'a'),
EnumValue((v) => v
..name = 'b'
..arguments.addAll([
literalNum(1),
])
..namedArguments.addAll({
'myString': literalString('abc'),
})),
EnumValue((v) => v..name = 'c'),
]));
expect(myEnum, equalsDart('''
enum MyEnum {
a,
b(1, myString: 'abc'),
c;

const MyEnum(
this.myInt,
{required this.myString, }
);

final int? myInt;

final String? myString;
}
'''));
});
}