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

Commit a88aa0b

Browse files
authored
update tool/generate_* scripts and sync files generated by those scripts (#150)
* update tool/generate_* scripts and sync files generated by those scripts * add double converter support, fix related tests * ensure only fields of types annotated with BuiltValue are nested builders * remove redundant import * fix typo, sync build_value test goldens
1 parent debc16c commit a88aa0b

17 files changed

+425
-137
lines changed

goldens/foo/lib/built_value/built_value_test.analyzer.augmentations

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,26 @@ prefix1.PrimitiveFields rebuild(void Function(prefix1.PrimitiveFieldsBuilder) up
5050
augment class NestedFields {
5151
factory NestedFields([void Function(prefix1.NestedFieldsBuilder)? updates]) =>
5252
(prefix1.NestedFieldsBuilder()..update(updates)).build();
53-
NestedFields._({required this.aPrimitiveFields,required this.aString,}) {}
53+
NestedFields._({required this.aPrimitiveFields,required this.stringWrapper,required this.aString,}) {}
5454

5555
prefix1.NestedFieldsBuilder toBuilder() => prefix1.NestedFieldsBuilder()..replace(this);
5656
prefix1.NestedFields rebuild(void Function(prefix1.NestedFieldsBuilder) updates) =>
5757
(toBuilder()..update(updates)).build();
5858

59-
prefix2.int get hashCode => prefix2.Object.hashAll([aPrimitiveFields,aString,]);
59+
prefix2.int get hashCode => prefix2.Object.hashAll([aPrimitiveFields,stringWrapper,aString,]);
6060

6161
prefix2.bool operator==(prefix2.Object other) =>
62-
other is prefix1.NestedFields&& aPrimitiveFields == other.aPrimitiveFields&& aString == other.aString;
62+
other is prefix1.NestedFields&& aPrimitiveFields == other.aPrimitiveFields&& stringWrapper == other.stringWrapper&& aString == other.aString;
6363

64-
prefix2.String toString() => 'NestedFields(aPrimitiveFields: $aPrimitiveFields, aString: $aString)';
64+
prefix2.String toString() => 'NestedFields(aPrimitiveFields: $aPrimitiveFields, stringWrapper: $stringWrapper, aString: $aString)';
6565

6666
}
6767
augment class NestedFieldsBuilder {
68-
prefix1.PrimitiveFieldsBuilder aPrimitiveFields = prefix1.PrimitiveFieldsBuilder();prefix2.String? aString;
68+
prefix1.PrimitiveFieldsBuilder aPrimitiveFields = prefix1.PrimitiveFieldsBuilder();prefix1.StringWrapper? stringWrapper;prefix2.String? aString;
6969

70-
void replace(prefix1.NestedFields other) { this.aPrimitiveFields = other.aPrimitiveFields.toBuilder();this.aString = other.aString; }
70+
void replace(prefix1.NestedFields other) { this.aPrimitiveFields = other.aPrimitiveFields.toBuilder();this.stringWrapper = other.stringWrapper;this.aString = other.aString; }
7171
void update(void Function(prefix1.NestedFieldsBuilder)? updates) => updates?.call(this);
72-
prefix1.NestedFields build() => prefix1.NestedFields._(aPrimitiveFields: aPrimitiveFields.build(),aString: aString!,);
72+
prefix1.NestedFields build() => prefix1.NestedFields._(aPrimitiveFields: aPrimitiveFields.build(),stringWrapper: stringWrapper!,aString: aString!,);
7373

7474
}
7575
augment class PrimitiveFieldsBuilder {

goldens/foo/lib/built_value/built_value_test.dart

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,33 @@ void main() {
5454
b
5555
..aPrimitiveFields.anInt = 3
5656
..aPrimitiveFields.aString = 'four'
57-
..aString = 'five',
57+
..aString = 'five'
58+
..stringWrapper = StringWrapper('six'),
5859
);
5960
expect(
6061
value.toString(),
6162
'NestedFields(aPrimitiveFields: PrimitiveFields('
62-
'anInt: 3, aString: four, aNullableString: null), aString: five)',
63+
'anInt: 3, aString: four, aNullableString: null), '
64+
'stringWrapper: StringWrapper(aString: six), aString: five)',
6365
);
6466
});
6567
});
6668
}
6769

70+
class NonMacro {
71+
const NonMacro();
72+
}
73+
74+
@NonMacro()
75+
class StringWrapper {
76+
const StringWrapper(this.aString);
77+
78+
final String aString;
79+
80+
@override
81+
String toString() => 'StringWrapper(aString: $aString)';
82+
}
83+
6884
@BuiltValue()
6985
class Empty {}
7086

@@ -78,5 +94,6 @@ class PrimitiveFields {
7894
@BuiltValue()
7995
class NestedFields {
8096
final PrimitiveFields aPrimitiveFields;
97+
final StringWrapper stringWrapper;
8198
final String aString;
8299
}

goldens/foo/lib/foo.analyzer.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,18 @@
208208
"value": {
209209
"reference": {
210210
"type": "ClassReference",
211-
"value": {}
211+
"value": {
212+
"name": "QueryClass"
213+
}
212214
},
213215
"typeArguments": []
214216
}
215217
},
216218
"constructor": {
217219
"type": "ConstructorReference",
218-
"value": {}
220+
"value": {
221+
"name": "new"
222+
}
219223
},
220224
"arguments": []
221225
}
@@ -272,14 +276,18 @@
272276
"value": {
273277
"reference": {
274278
"type": "ClassReference",
275-
"value": {}
279+
"value": {
280+
"name": "QueryClass"
281+
}
276282
},
277283
"typeArguments": []
278284
}
279285
},
280286
"constructor": {
281287
"type": "ConstructorReference",
282-
"value": {}
288+
"value": {
289+
"name": "new"
290+
}
283291
},
284292
"arguments": []
285293
}

goldens/foo/lib/literal_params.analyzer.augmentations

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ augment class Foo {
55
// aNum: 8.0, double
66
// aDouble: 9.0, double
77
// aString: 10, String
8-
// anObject: {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}, String
8+
// anObject: {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {name: Bar}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {name: new}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {value: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {value: false}}}}]}, String
99
// ints: [11, 12], List<Object>
1010
// nums: [13.0, 14], List<Object>
1111
// doubles: [15.0, 16], List<Object>
1212
// strings: [17, eighteen], List<Object>
13-
// objects: [19, {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {text: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {text: false}}}}]}], List<Object>
13+
// objects: [19, {type: {type: NamedTypeAnnotation, value: {reference: {type: ClassReference, value: {name: Bar}}, typeArguments: []}}, constructor: {type: ConstructorReference, value: {name: new}}, arguments: [{type: NamedArgument, value: {name: a, expression: {type: BooleanLiteral, value: {value: true}}}}, {type: NamedArgument, value: {name: b, expression: {type: BooleanLiteral, value: {value: false}}}}]}], List<Object>
1414
}

goldens/foo/lib/metadata.analyzer.json

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,18 @@
2828
"value": {
2929
"reference": {
3030
"type": "ClassReference",
31-
"value": {}
31+
"value": {
32+
"name": "QueryClass"
33+
}
3234
},
3335
"typeArguments": []
3436
}
3537
},
3638
"constructor": {
3739
"type": "ConstructorReference",
38-
"value": {}
40+
"value": {
41+
"name": "new"
42+
}
3943
},
4044
"arguments": []
4145
}
@@ -50,14 +54,18 @@
5054
"value": {
5155
"reference": {
5256
"type": "ClassReference",
53-
"value": {}
57+
"value": {
58+
"name": "Annotation"
59+
}
5460
},
5561
"typeArguments": []
5662
}
5763
},
5864
"constructor": {
5965
"type": "ConstructorReference",
60-
"value": {}
66+
"value": {
67+
"name": "new"
68+
}
6169
},
6270
"arguments": [
6371
{
@@ -67,7 +75,7 @@
6775
"expression": {
6876
"type": "BooleanLiteral",
6977
"value": {
70-
"text": "true"
78+
"value": true
7179
}
7280
}
7381
}
@@ -79,7 +87,8 @@
7987
"expression": {
8088
"type": "IntegerLiteral",
8189
"value": {
82-
"text": "23"
90+
"text": "23",
91+
"value": 23
8392
}
8493
}
8594
}

pkgs/_analyzer_cfe_macros/lib/metadata_converter.dart

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@ dart_model.Reference? convertToReference(Object? object) => switch (object) {
186186
front_end.EnumReference o => dart_model.Reference.enumReference(
187187
convert<dart_model.EnumReference>(o)!,
188188
),
189+
front_end.MixinReference o => dart_model.Reference.mixinReference(
190+
convert<dart_model.MixinReference>(o)!,
191+
),
189192
front_end.FunctionTypeParameterReference o => dart_model
190193
.Reference.functionTypeParameterReference(
191194
convert<dart_model.FunctionTypeParameterReference>(o)!,
@@ -250,6 +253,8 @@ T? convert<T>(Object? object) => switch (object) {
250253
front_end.BinaryOperator o => o.name as T,
251254
front_end.LogicalOperator o => o.name as T,
252255
front_end.UnaryOperator o => o.name as T,
256+
front_end.AdjacentStringLiterals o =>
257+
dart_model.AdjacentStringLiterals(expressions: convert(o.expressions)) as T,
253258
front_end.AsExpression o =>
254259
dart_model.AsExpression(
255260
expression: convertToExpression(o.expression),
@@ -264,8 +269,9 @@ T? convert<T>(Object? object) => switch (object) {
264269
)
265270
as T,
266271
front_end.BooleanLiteral o =>
267-
dart_model.BooleanLiteral(text: convert(o.value.toString())) as T,
268-
front_end.ClassReference o => dart_model.ClassReference() as T,
272+
dart_model.BooleanLiteral(value: convert(o.value)) as T,
273+
front_end.ClassReference o =>
274+
dart_model.ClassReference(name: convert(o.name)) as T,
269275
front_end.ConditionalExpression o =>
270276
dart_model.ConditionalExpression(
271277
condition: convertToExpression(o.condition),
@@ -280,19 +286,22 @@ T? convert<T>(Object? object) => switch (object) {
280286
arguments: convert(o.arguments),
281287
)
282288
as T,
283-
front_end.ConstructorReference o => dart_model.ConstructorReference() as T,
289+
front_end.ConstructorReference o =>
290+
dart_model.ConstructorReference(name: convert(o.name)) as T,
284291
front_end.ConstructorTearOff o =>
285292
dart_model.ConstructorTearOff(
286293
type: convert(o.type),
287294
reference: convert(o.reference),
288295
)
289296
as T,
290297
front_end.DoubleLiteral o =>
291-
dart_model.DoubleLiteral(text: convert(o.text)) as T,
298+
dart_model.DoubleLiteral(text: convert(o.text), value: convert(o.value))
299+
as T,
292300
front_end.DynamicTypeAnnotation o =>
293301
dart_model.DynamicTypeAnnotation(reference: convertToReference(o.reference))
294302
as T,
295-
front_end.EnumReference o => dart_model.EnumReference() as T,
303+
front_end.EnumReference o =>
304+
dart_model.EnumReference(name: convert(o.name)) as T,
296305
front_end.EqualityExpression o =>
297306
dart_model.EqualityExpression(
298307
left: convertToExpression(o.left),
@@ -306,13 +315,16 @@ T? convert<T>(Object? object) => switch (object) {
306315
isNullAware: convert(o.isNullAware),
307316
)
308317
as T,
309-
front_end.ExtensionReference o => dart_model.ExtensionReference() as T,
318+
front_end.ExtensionReference o =>
319+
dart_model.ExtensionReference(name: convert(o.name)) as T,
310320
front_end.ExtensionTypeReference o =>
311-
dart_model.ExtensionTypeReference() as T,
312-
front_end.FieldReference o => dart_model.FieldReference() as T,
321+
dart_model.ExtensionTypeReference(name: convert(o.name)) as T,
322+
front_end.FieldReference o =>
323+
dart_model.FieldReference(name: convert(o.name)) as T,
313324
front_end.FormalParameter o => dart_model.FormalParameter() as T,
314325
front_end.FormalParameterGroup o => dart_model.FormalParameterGroup() as T,
315-
front_end.FunctionReference o => dart_model.FunctionReference() as T,
326+
front_end.FunctionReference o =>
327+
dart_model.FunctionReference(name: convert(o.name)) as T,
316328
front_end.FunctionTearOff o =>
317329
dart_model.FunctionTearOff(reference: convert(o.reference)) as T,
318330
front_end.FunctionTypeAnnotation o =>
@@ -324,7 +336,7 @@ T? convert<T>(Object? object) => switch (object) {
324336
as T,
325337
front_end.FunctionTypeParameter o => dart_model.FunctionTypeParameter() as T,
326338
front_end.FunctionTypeParameterReference o =>
327-
dart_model.FunctionTypeParameterReference() as T,
339+
dart_model.FunctionTypeParameterReference(name: convert(o.name)) as T,
328340
front_end.FunctionTypeParameterType o =>
329341
dart_model.FunctionTypeParameterType(
330342
functionTypeParameter: convert(o.functionTypeParameter),
@@ -357,7 +369,8 @@ T? convert<T>(Object? object) => switch (object) {
357369
)
358370
as T,
359371
front_end.IntegerLiteral o =>
360-
dart_model.IntegerLiteral(text: convert(o.text)) as T,
372+
dart_model.IntegerLiteral(text: convert(o.text), value: convert(o.value))
373+
as T,
361374
front_end.InterpolationPart o =>
362375
dart_model.InterpolationPart(expression: convertToExpression(o.expression))
363376
as T,
@@ -399,6 +412,8 @@ T? convert<T>(Object? object) => switch (object) {
399412
arguments: convert(o.arguments),
400413
)
401414
as T,
415+
front_end.MixinReference o =>
416+
dart_model.MixinReference(name: convert(o.name)) as T,
402417
front_end.NamedArgument o =>
403418
dart_model.NamedArgument(
404419
name: convert(o.name),
@@ -479,14 +494,13 @@ T? convert<T>(Object? object) => switch (object) {
479494
arguments: convert(o.arguments),
480495
)
481496
as T,
482-
front_end.AdjacentStringLiterals o =>
483-
dart_model.AdjacentStringLiterals(expressions: convert(o.expressions)) as T,
484497
front_end.StringLiteral o =>
485498
dart_model.StringLiteral(parts: convert(o.parts)) as T,
486499
front_end.StringPart o => dart_model.StringPart(text: convert(o.text)) as T,
487500
front_end.SymbolLiteral o =>
488501
dart_model.SymbolLiteral(parts: convert(o.parts)) as T,
489-
front_end.TypedefReference o => dart_model.TypedefReference() as T,
502+
front_end.TypedefReference o =>
503+
dart_model.TypedefReference(name: convert(o.name)) as T,
490504
front_end.TypeLiteral o =>
491505
dart_model.TypeLiteral(typeAnnotation: convert(o.typeAnnotation)) as T,
492506
front_end.TypeReference o => dart_model.TypeReference() as T,

pkgs/_analyzer_cfe_macros/test/metadata_converter_test.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ void main() {
2020
expect(convert<Object>(invocation), <String, Object?>{
2121
'receiver': {
2222
'type': 'DoubleLiteral',
23-
'value': {'text': '1.23'},
23+
'value': {'text': '1.23', 'value': 1.23},
2424
},
2525
'name': 'round',
2626
'typeArguments': [],
@@ -40,12 +40,12 @@ void main() {
4040
expect(convert<Object>(expression), <String, Object?>{
4141
'left': {
4242
'type': 'DoubleLiteral',
43-
'value': {'text': '1.23'},
43+
'value': {'text': '1.23', 'value': 1.23},
4444
},
4545
'operator': 'minus',
4646
'right': {
4747
'type': 'DoubleLiteral',
48-
'value': {'text': '1.24'},
48+
'value': {'text': '1.24', 'value': 1.24},
4949
},
5050
});
5151
});
@@ -63,7 +63,7 @@ void main() {
6363
expect(convert<Object>(invocation), <String, Object?>{
6464
'receiver': {
6565
'type': 'DoubleLiteral',
66-
'value': {'text': '1.23'},
66+
'value': {'text': '1.23', 'value': 1.23},
6767
},
6868
'name': 'round',
6969
'typeArguments': [],

pkgs/_test_macros/lib/built_value.dart

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,20 @@ class BuiltValueBuilderImplementation implements ClassDeclarationsMacro {
183183
TypeAnnotationType.namedTypeAnnotation) {
184184
continue;
185185
}
186-
// TODO(davidmorgan): macro metadata model doesn't actually have the
187-
// name yet, just assume any constructor annotation is `BuiltValue`.
186+
187+
final namedTypeAnnotation =
188+
constructorInvocation.type.asNamedTypeAnnotation;
189+
if (namedTypeAnnotation.reference.type !=
190+
ReferenceType.classReference) {
191+
continue;
192+
}
193+
194+
final constructorReference =
195+
namedTypeAnnotation.reference.asClassReference;
196+
if (constructorReference.name != 'BuiltValue') {
197+
continue;
198+
}
199+
188200
nestedBuilderTypes.add(qualifiedName.asString);
189201
}
190202
}

pkgs/_test_macros/lib/literal_params.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ class LiteralParamsImplementation implements ClassDeclarationsMacro {
9393
extension ExpressionExtension on Expression {
9494
Object get evaluate => switch (type) {
9595
ExpressionType.integerLiteral => int.parse(asIntegerLiteral.text),
96-
ExpressionType.doubleLiteral => double.parse(asDoubleLiteral.text),
96+
ExpressionType.doubleLiteral => asDoubleLiteral.value,
9797
ExpressionType.stringLiteral => asStringLiteral.evaluate,
98-
ExpressionType.booleanLiteral => bool.parse(asBooleanLiteral.text),
98+
ExpressionType.booleanLiteral => asBooleanLiteral.value,
9999
ExpressionType.listLiteral =>
100100
asListLiteral.elements.map((e) => e.evaluate).toList(),
101101
// TODO(davidmorgan): need the type name to do something useful here,

0 commit comments

Comments
 (0)