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

Commit 23860ee

Browse files
committed
ensure only fields of types annotated with BuiltValue are nested builders
1 parent 210929f commit 23860ee

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

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 = StringWraper('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 StringWraper {
76+
const StringWraper(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 StringWraper stringWrapper;
8198
final String aString;
8299
}

pkgs/_test_macros/lib/built_value.dart

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'dart:async';
6+
import 'dart:io';
67

78
import 'package:dart_model/dart_model.dart';
89
// ignore: implementation_imports
@@ -183,8 +184,20 @@ class BuiltValueBuilderImplementation implements ClassDeclarationsMacro {
183184
TypeAnnotationType.namedTypeAnnotation) {
184185
continue;
185186
}
186-
// TODO(davidmorgan): macro metadata model doesn't actually have the
187-
// name yet, just assume any constructor annotation is `BuiltValue`.
187+
188+
final namedTypeAnnotation =
189+
constructorInvocation.type.asNamedTypeAnnotation;
190+
if (namedTypeAnnotation.reference.type !=
191+
ReferenceType.classReference) {
192+
continue;
193+
}
194+
195+
final constructorReference =
196+
namedTypeAnnotation.reference.asClassReference;
197+
if (constructorReference.name != 'BuiltValue') {
198+
continue;
199+
}
200+
188201
nestedBuilderTypes.add(qualifiedName.asString);
189202
}
190203
}

0 commit comments

Comments
 (0)