Skip to content

Commit 5bed3d7

Browse files
authored
Add configuration for not throwing on unresolved annotations (#683)
Closes #682 This only enables configuration right now, mostly to avoid any change in default behavior. I am open to changing the default too though.
1 parent c953043 commit 5bed3d7

File tree

4 files changed

+63
-5
lines changed

4 files changed

+63
-5
lines changed

source_gen/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
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

source_gen/lib/src/generator_for_annotation.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff 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.
4343
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});
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,

source_gen/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 1.4.1-wip
2+
version: 1.5.0-wip
33
description: >-
44
Source code generation builders and utilities for the Dart build system
55
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen

source_gen/test/generator_for_annotation_test.dart

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff 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

183230
class _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(

0 commit comments

Comments
 (0)