@@ -13,29 +13,27 @@ macro class DataClass
13
13
Future <void > buildDeclarationsForClass (
14
14
ClassDeclaration clazz, ClassMemberDeclarationBuilder context) async {
15
15
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),
21
21
]);
22
22
}
23
23
24
24
@override
25
25
Future <void > buildDefinitionForClass (
26
26
ClassDeclaration clazz, ClassDefinitionBuilder builder) async {
27
27
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),
31
31
]);
32
32
}
33
33
}
34
34
35
- const autoConstructor = _AutoConstructor ();
36
-
37
- macro class _AutoConstructor implements ClassDeclarationsMacro {
38
- const _AutoConstructor ();
35
+ macro class AutoConstructor implements ClassDeclarationsMacro {
36
+ const AutoConstructor ();
39
37
40
38
@override
41
39
Future <void > buildDeclarationsForClass (
@@ -46,13 +44,14 @@ macro class _AutoConstructor implements ClassDeclarationsMacro {
46
44
'Cannot generate an unnamed constructor because one already exists' );
47
45
}
48
46
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 > [];
51
48
// Add all the fields of `declaration` as named parameters.
52
49
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
+ }
56
55
}
57
56
58
57
// The object type from dart:core.
@@ -78,22 +77,32 @@ macro class _AutoConstructor implements ClassDeclarationsMacro {
78
77
// parameters in this constructor.
79
78
for (var param in superconstructor.positionalParameters) {
80
79
var requiredKeyword = param.isRequired ? 'required' : '' ;
81
- parts .addAll ([
80
+ params .addAll ([
82
81
'\n $requiredKeyword ' ,
83
82
param.type.code,
84
83
' ${param .identifier .name },' ,
85
84
]);
86
85
}
87
86
for (var param in superconstructor.namedParameters) {
88
87
var requiredKeyword = param.isRequired ? '' : 'required ' ;
89
- parts .addAll ([
88
+ params .addAll ([
90
89
'\n $requiredKeyword ' ,
91
90
param.type.code,
92
91
' ${param .identifier .name },' ,
93
92
]);
94
93
}
95
94
}
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
+ ];
97
106
if (superconstructor != null ) {
98
107
parts.addAll ([' : super.' , superconstructor.identifier.name, '(' ]);
99
108
for (var param in superconstructor.positionalParameters) {
@@ -111,11 +120,9 @@ macro class _AutoConstructor implements ClassDeclarationsMacro {
111
120
}
112
121
}
113
122
114
- const copyWith = _CopyWith ();
115
-
116
123
// 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 ();
119
126
120
127
@override
121
128
Future <void > buildDeclarationsForClass (
@@ -141,24 +148,25 @@ macro class _CopyWith implements ClassDeclarationsMacro {
141
148
field.identifier,
142
149
]),
143
150
];
151
+ var hasParams = namedParams.isNotEmpty;
144
152
builder.declareInClass (DeclarationCode .fromParts ([
145
153
clazz.identifier,
146
- ' copyWith({' ,
154
+ ' copyWith(' ,
155
+ if (hasParams) '{' ,
147
156
...namedParams.joinAsCode (', ' ),
148
- ',})' ,
157
+ if (hasParams) '}' ,
158
+ ')' ,
149
159
// TODO: We assume this constructor exists, but should check
150
160
'=> ' , clazz.identifier, '.gen(' ,
151
161
...args.joinAsCode (', ' ),
152
- ', );' ,
162
+ ');' ,
153
163
]));
154
164
}
155
165
}
156
166
157
- const hashCode = _HashCode ();
158
-
159
- macro class _HashCode
167
+ macro class HashCode
160
168
implements ClassDeclarationsMacro , ClassDefinitionMacro {
161
- const _HashCode ();
169
+ const HashCode ();
162
170
163
171
@override
164
172
Future <void > buildDeclarationsForClass (
@@ -189,11 +197,9 @@ macro class _HashCode
189
197
}
190
198
}
191
199
192
- const equality = _Equality ();
193
-
194
- macro class _Equality
200
+ macro class Equality
195
201
implements ClassDeclarationsMacro , ClassDefinitionMacro {
196
- const _Equality ();
202
+ const Equality ();
197
203
198
204
@override
199
205
Future <void > buildDeclarationsForClass (
@@ -234,11 +240,10 @@ macro class _Equality
234
240
}
235
241
}
236
242
237
- const toString = _ToString ();
238
243
239
- macro class _ToString
244
+ macro class ToString
240
245
implements ClassDeclarationsMacro , ClassDefinitionMacro {
241
- const _ToString ();
246
+ const ToString ();
242
247
243
248
@override
244
249
Future <void > buildDeclarationsForClass (
0 commit comments