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.
2
5
3
6
## 1.4.0
4
7
Original file line number Diff line number Diff line change @@ -41,15 +41,23 @@ import 'type_checker.dart';
41
41
/// [T] and use the [Element] to iterate over fields. The [TypeChecker] utility
42
42
/// may be helpful to check which elements have a given annotation.
43
43
abstract 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 });
45
50
46
51
TypeChecker get typeChecker => TypeChecker .fromRuntime (T );
47
52
48
53
@override
49
54
FutureOr <String > generate (LibraryReader library, BuildStep buildStep) async {
50
55
final values = < String > {};
51
56
52
- for (var annotatedElement in library.annotatedWith (typeChecker)) {
57
+ for (var annotatedElement in library.annotatedWith (
58
+ typeChecker,
59
+ throwOnUnresolved: throwOnUnresolved,
60
+ )) {
53
61
final generatedValue = generateForAnnotatedElement (
54
62
annotatedElement.element,
55
63
annotatedElement.annotation,
Original file line number Diff line number Diff line change 1
1
name : source_gen
2
- version : 1.4.1 -wip
2
+ version : 1.5.0 -wip
3
3
description : >-
4
4
Source code generation builders and utilities for the Dart build system
5
5
repository : 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() {
178
178
},
179
179
);
180
180
});
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
+ });
181
228
}
182
229
183
230
class _StubGenerator <T > extends GeneratorForAnnotation <T > {
184
231
final String _name;
185
232
final Object ? Function (Element ) _behavior;
186
233
187
- const _StubGenerator (this ._name, this ._behavior);
234
+ const _StubGenerator (this ._name, this ._behavior, { super .throwOnUnresolved} );
188
235
189
236
@override
190
237
Object ? generateForAnnotatedElement (
You can’t perform that action at this time.
0 commit comments