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

Commit 3b2039b

Browse files
authored
Added support for named arguments in enum classes (#421)
Add `namedArguments` field to `EnumValue` and emit the arguments in the visitor.
1 parent b07f6b3 commit 3b2039b

File tree

6 files changed

+109
-7
lines changed

6 files changed

+109
-7
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 4.6.0-wip
2+
3+
* Add support for named arguments in `enum` classes
4+
15
## 4.5.0
26

37
* Require Dart 2.19

lib/src/emitter.dart

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -800,15 +800,25 @@ class DartEmitter extends Object
800800
out.write('.${v.constructorName}');
801801
}
802802
visitTypeParameters(v.types.map((r) => r.type), out);
803-
final takesArguments =
804-
v.constructorName != null || v.arguments.isNotEmpty;
803+
final takesArguments = v.constructorName != null ||
804+
v.arguments.isNotEmpty ||
805+
v.namedArguments.isNotEmpty;
805806
if (takesArguments) {
806807
out.write('(');
807808
}
808809
if (v.arguments.isNotEmpty) {
809810
out.writeAll(
810811
v.arguments.map<StringSink>((arg) => arg.accept(this)), ', ');
811812
}
813+
if (v.arguments.isNotEmpty && v.namedArguments.isNotEmpty) {
814+
out.write(', ');
815+
}
816+
visitAll<String>(v.namedArguments.keys, out, (name) {
817+
out
818+
..write(name)
819+
..write(': ');
820+
v.namedArguments[name]!.accept(this, out);
821+
});
812822
if (takesArguments) {
813823
out.write(')');
814824
}

lib/src/specs/enum.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ abstract class EnumValue extends Object
105105

106106
/// Arguments to the constructor.
107107
BuiltList<Expression> get arguments;
108+
109+
/// Named arguments to the constructor.
110+
BuiltMap<String, Expression> get namedArguments;
108111
}
109112

110113
abstract class EnumValueBuilder extends Object
@@ -130,4 +133,7 @@ abstract class EnumValueBuilder extends Object
130133

131134
/// Arguments to the constructor.
132135
ListBuilder<Expression> arguments = ListBuilder();
136+
137+
/// Named arguments to the constructor.
138+
MapBuilder<String, Expression> namedArguments = MapBuilder();
133139
}

lib/src/specs/enum.g.dart

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

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: code_builder
2-
version: 4.5.0
2+
version: 4.6.0-wip
33
description: >-
44
A fluent, builder-based library for generating valid Dart code
55
repository: https://github.com/dart-lang/code_builder

test/specs/enum_test.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,4 +381,62 @@ void main() {
381381
}
382382
'''));
383383
});
384+
385+
test('should create an enum which named and unnamed constructor parameters',
386+
() {
387+
final myEnum = Enum((b) => b
388+
..name = 'MyEnum'
389+
..constructors.add(Constructor((c) => c
390+
..constant = true
391+
..requiredParameters.addAll([
392+
Parameter((p) => p
393+
..toThis = true
394+
..name = 'myInt')
395+
])
396+
..optionalParameters.addAll([
397+
Parameter((p) => p
398+
..toThis = true
399+
..named = true
400+
..required = true
401+
..name = 'myString')
402+
])))
403+
..fields.addAll([
404+
Field((f) => f
405+
..modifier = FieldModifier.final$
406+
..type = refer('int?')
407+
..name = 'myInt'),
408+
Field((f) => f
409+
..modifier = FieldModifier.final$
410+
..type = refer('String?')
411+
..name = 'myString')
412+
])
413+
..values.addAll([
414+
EnumValue((v) => v..name = 'a'),
415+
EnumValue((v) => v
416+
..name = 'b'
417+
..arguments.addAll([
418+
literalNum(1),
419+
])
420+
..namedArguments.addAll({
421+
'myString': literalString('abc'),
422+
})),
423+
EnumValue((v) => v..name = 'c'),
424+
]));
425+
expect(myEnum, equalsDart('''
426+
enum MyEnum {
427+
a,
428+
b(1, myString: 'abc'),
429+
c;
430+
431+
const MyEnum(
432+
this.myInt,
433+
{required this.myString, }
434+
);
435+
436+
final int? myInt;
437+
438+
final String? myString;
439+
}
440+
'''));
441+
});
384442
}

0 commit comments

Comments
 (0)