File tree Expand file tree Collapse file tree 4 files changed +63
-5
lines changed
Expand file tree Collapse file tree 4 files changed +63
-5
lines changed Original file line number Diff line number Diff line change 1- ## 1.4.1-wip
1+ ## 1.5.0-wip
2+
3+ - Add ` throwOnUnresolved ` configuration to the ` GeneratorForAnnotation `
4+ constructor.
25
36## 1.4.0
47
Original file line number Diff line number Diff line change @@ -41,15 +41,23 @@ import 'type_checker.dart';
4141/// [T] and use the [Element] to iterate over fields. The [TypeChecker] utility
4242/// may be helpful to check which elements have a given annotation.
4343abstract class GeneratorForAnnotation <T > extends Generator {
44- const GeneratorForAnnotation ();
44+ final bool throwOnUnresolved;
45+
46+ /// By default, this generator will throw if it encounters unresolved
47+ /// annotations. You can override this by setting [throwOnUnresolved] to
48+ /// `false` .
49+ const GeneratorForAnnotation ({this .throwOnUnresolved = true });
4550
4651 TypeChecker get typeChecker => TypeChecker .fromRuntime (T );
4752
4853 @override
4954 FutureOr <String > generate (LibraryReader library, BuildStep buildStep) async {
5055 final values = < String > {};
5156
52- for (var annotatedElement in library.annotatedWith (typeChecker)) {
57+ for (var annotatedElement in library.annotatedWith (
58+ typeChecker,
59+ throwOnUnresolved: throwOnUnresolved,
60+ )) {
5361 final generatedValue = generateForAnnotatedElement (
5462 annotatedElement.element,
5563 annotatedElement.annotation,
Original file line number Diff line number Diff line change 11name : source_gen
2- version : 1.4.1 -wip
2+ version : 1.5.0 -wip
33description : >-
44 Source code generation builders and utilities for the Dart build system
55repository : https://github.com/dart-lang/source_gen/tree/master/source_gen
Original file line number Diff line number Diff line change @@ -178,13 +178,60 @@ void main() {
178178 },
179179 );
180180 });
181+
182+ group ('Unresolved annotations' , () {
183+ test ('cause an error by default' , () async {
184+ final builder = LibraryBuilder (
185+ _StubGenerator <Deprecated >(
186+ 'Deprecated' ,
187+ (element) => '// ${element .displayName }' ,
188+ ),
189+ );
190+ expect (
191+ testBuilder (
192+ builder,
193+ {
194+ 'a|lib/file.dart' : '''
195+ @doesNotExist
196+ library foo;
197+ ''' ,
198+ },
199+ outputs: {},
200+ ),
201+ throwsA (isA <UnresolvedAnnotationException >()),
202+ );
203+ });
204+
205+ test ('do not cause an error if disabled' , () async {
206+ final builder = LibraryBuilder (
207+ _StubGenerator <Deprecated >(
208+ 'Deprecated' ,
209+ (element) => '// ${element .displayName }' ,
210+ throwOnUnresolved: false ,
211+ ),
212+ );
213+ expect (
214+ testBuilder (
215+ builder,
216+ {
217+ 'a|lib/file.dart' : '''
218+ @doesNotExist
219+ library foo;
220+ ''' ,
221+ },
222+ outputs: {},
223+ ),
224+ completes,
225+ );
226+ });
227+ });
181228}
182229
183230class _StubGenerator <T > extends GeneratorForAnnotation <T > {
184231 final String _name;
185232 final Object ? Function (Element ) _behavior;
186233
187- const _StubGenerator (this ._name, this ._behavior);
234+ const _StubGenerator (this ._name, this ._behavior, { super .throwOnUnresolved} );
188235
189236 @override
190237 Object ? generateForAnnotatedElement (
You can’t perform that action at this time.
0 commit comments