@@ -13,29 +13,27 @@ macro class DataClass
1313 Future <void > buildDeclarationsForClass (
1414 ClassDeclaration clazz, ClassMemberDeclarationBuilder context) async {
1515 await Future .wait ([
16- autoConstructor .buildDeclarationsForClass (clazz, context),
17- copyWith .buildDeclarationsForClass (clazz, context),
18- hashCode .buildDeclarationsForClass (clazz, context),
19- equality .buildDeclarationsForClass (clazz, context),
20- toString .buildDeclarationsForClass (clazz, context),
16+ AutoConstructor () .buildDeclarationsForClass (clazz, context),
17+ CopyWith () .buildDeclarationsForClass (clazz, context),
18+ HashCode () .buildDeclarationsForClass (clazz, context),
19+ Equality () .buildDeclarationsForClass (clazz, context),
20+ ToString () .buildDeclarationsForClass (clazz, context),
2121 ]);
2222 }
2323
2424 @override
2525 Future <void > buildDefinitionForClass (
2626 ClassDeclaration clazz, ClassDefinitionBuilder builder) async {
2727 await Future .wait ([
28- hashCode .buildDefinitionForClass (clazz, builder),
29- equality .buildDefinitionForClass (clazz, builder),
30- toString .buildDefinitionForClass (clazz, builder),
28+ HashCode () .buildDefinitionForClass (clazz, builder),
29+ Equality () .buildDefinitionForClass (clazz, builder),
30+ ToString () .buildDefinitionForClass (clazz, builder),
3131 ]);
3232 }
3333}
3434
35- const autoConstructor = _AutoConstructor ();
36-
37- macro class _AutoConstructor implements ClassDeclarationsMacro {
38- const _AutoConstructor ();
35+ macro class AutoConstructor implements ClassDeclarationsMacro {
36+ const AutoConstructor ();
3937
4038 @override
4139 Future <void > buildDeclarationsForClass (
@@ -46,13 +44,14 @@ macro class _AutoConstructor implements ClassDeclarationsMacro {
4644 'Cannot generate an unnamed constructor because one already exists' );
4745 }
4846
49- // Don't use the identifier here because it should just be the raw name.
50- var parts = < Object > [clazz.identifier.name, '.gen({' ];
47+ var params = < Object > [];
5148 // Add all the fields of `declaration` as named parameters.
5249 var fields = await builder.fieldsOf (clazz);
53- for (var field in fields) {
54- var requiredKeyword = field.type.isNullable ? '' : 'required ' ;
55- parts.addAll (['\n ${requiredKeyword }' , field.identifier, ',' ]);
50+ if (fields.isNotEmpty) {
51+ for (var field in fields) {
52+ var requiredKeyword = field.type.isNullable ? '' : 'required ' ;
53+ params.addAll (['\n ${requiredKeyword }' , field.identifier, ',' ]);
54+ }
5655 }
5756
5857 // The object type from dart:core.
@@ -78,22 +77,32 @@ macro class _AutoConstructor implements ClassDeclarationsMacro {
7877 // parameters in this constructor.
7978 for (var param in superconstructor.positionalParameters) {
8079 var requiredKeyword = param.isRequired ? 'required' : '' ;
81- parts .addAll ([
80+ params .addAll ([
8281 '\n $requiredKeyword ' ,
8382 param.type.code,
8483 ' ${param .identifier .name },' ,
8584 ]);
8685 }
8786 for (var param in superconstructor.namedParameters) {
8887 var requiredKeyword = param.isRequired ? '' : 'required ' ;
89- parts .addAll ([
88+ params .addAll ([
9089 '\n $requiredKeyword ' ,
9190 param.type.code,
9291 ' ${param .identifier .name },' ,
9392 ]);
9493 }
9594 }
96- parts.add ('\n })' );
95+
96+ bool hasParams = params.isNotEmpty;
97+ List <Object > parts = [
98+ // Don't use the identifier here because it should just be the raw name.
99+ clazz.identifier.name,
100+ '.gen(' ,
101+ if (hasParams) '{' ,
102+ ...params,
103+ if (hasParams) '}' ,
104+ ')' ,
105+ ];
97106 if (superconstructor != null ) {
98107 parts.addAll ([' : super.' , superconstructor.identifier.name, '(' ]);
99108 for (var param in superconstructor.positionalParameters) {
@@ -111,11 +120,9 @@ macro class _AutoConstructor implements ClassDeclarationsMacro {
111120 }
112121}
113122
114- const copyWith = _CopyWith ();
115-
116123// TODO: How to deal with overriding nullable fields to `null`?
117- macro class _CopyWith implements ClassDeclarationsMacro {
118- const _CopyWith ();
124+ macro class CopyWith implements ClassDeclarationsMacro {
125+ const CopyWith ();
119126
120127 @override
121128 Future <void > buildDeclarationsForClass (
@@ -141,24 +148,25 @@ macro class _CopyWith implements ClassDeclarationsMacro {
141148 field.identifier,
142149 ]),
143150 ];
151+ var hasParams = namedParams.isNotEmpty;
144152 builder.declareInClass (DeclarationCode .fromParts ([
145153 clazz.identifier,
146- ' copyWith({' ,
154+ ' copyWith(' ,
155+ if (hasParams) '{' ,
147156 ...namedParams.joinAsCode (', ' ),
148- ',})' ,
157+ if (hasParams) '}' ,
158+ ')' ,
149159 // TODO: We assume this constructor exists, but should check
150160 '=> ' , clazz.identifier, '.gen(' ,
151161 ...args.joinAsCode (', ' ),
152- ', );' ,
162+ ');' ,
153163 ]));
154164 }
155165}
156166
157- const hashCode = _HashCode ();
158-
159- macro class _HashCode
167+ macro class HashCode
160168 implements ClassDeclarationsMacro , ClassDefinitionMacro {
161- const _HashCode ();
169+ const HashCode ();
162170
163171 @override
164172 Future <void > buildDeclarationsForClass (
@@ -189,11 +197,9 @@ macro class _HashCode
189197 }
190198}
191199
192- const equality = _Equality ();
193-
194- macro class _Equality
200+ macro class Equality
195201 implements ClassDeclarationsMacro , ClassDefinitionMacro {
196- const _Equality ();
202+ const Equality ();
197203
198204 @override
199205 Future <void > buildDeclarationsForClass (
@@ -234,11 +240,10 @@ macro class _Equality
234240 }
235241}
236242
237- const toString = _ToString ();
238243
239- macro class _ToString
244+ macro class ToString
240245 implements ClassDeclarationsMacro , ClassDefinitionMacro {
241- const _ToString ();
246+ const ToString ();
242247
243248 @override
244249 Future <void > buildDeclarationsForClass (
0 commit comments