Skip to content

Commit

Permalink
Add configuration for not throwing on unresolved annotations (#683)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jakemac53 authored Sep 28, 2023
1 parent c953043 commit 5bed3d7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 5 deletions.
5 changes: 4 additions & 1 deletion source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## 1.4.1-wip
## 1.5.0-wip

- Add `throwOnUnresolved` configuration to the `GeneratorForAnnotation`
constructor.

## 1.4.0

Expand Down
12 changes: 10 additions & 2 deletions source_gen/lib/src/generator_for_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,23 @@ import 'type_checker.dart';
/// [T] and use the [Element] to iterate over fields. The [TypeChecker] utility
/// may be helpful to check which elements have a given annotation.
abstract class GeneratorForAnnotation<T> extends Generator {
const GeneratorForAnnotation();
final bool throwOnUnresolved;

/// By default, this generator will throw if it encounters unresolved
/// annotations. You can override this by setting [throwOnUnresolved] to
/// `false`.
const GeneratorForAnnotation({this.throwOnUnresolved = true});

TypeChecker get typeChecker => TypeChecker.fromRuntime(T);

@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
final values = <String>{};

for (var annotatedElement in library.annotatedWith(typeChecker)) {
for (var annotatedElement in library.annotatedWith(
typeChecker,
throwOnUnresolved: throwOnUnresolved,
)) {
final generatedValue = generateForAnnotatedElement(
annotatedElement.element,
annotatedElement.annotation,
Expand Down
2 changes: 1 addition & 1 deletion source_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 1.4.1-wip
version: 1.5.0-wip
description: >-
Source code generation builders and utilities for the Dart build system
repository: https://github.com/dart-lang/source_gen/tree/master/source_gen
Expand Down
49 changes: 48 additions & 1 deletion source_gen/test/generator_for_annotation_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,60 @@ void main() {
},
);
});

group('Unresolved annotations', () {
test('cause an error by default', () async {
final builder = LibraryBuilder(
_StubGenerator<Deprecated>(
'Deprecated',
(element) => '// ${element.displayName}',
),
);
expect(
testBuilder(
builder,
{
'a|lib/file.dart': '''
@doesNotExist
library foo;
''',
},
outputs: {},
),
throwsA(isA<UnresolvedAnnotationException>()),
);
});

test('do not cause an error if disabled', () async {
final builder = LibraryBuilder(
_StubGenerator<Deprecated>(
'Deprecated',
(element) => '// ${element.displayName}',
throwOnUnresolved: false,
),
);
expect(
testBuilder(
builder,
{
'a|lib/file.dart': '''
@doesNotExist
library foo;
''',
},
outputs: {},
),
completes,
);
});
});
}

class _StubGenerator<T> extends GeneratorForAnnotation<T> {
final String _name;
final Object? Function(Element) _behavior;

const _StubGenerator(this._name, this._behavior);
const _StubGenerator(this._name, this._behavior, {super.throwOnUnresolved});

@override
Object? generateForAnnotatedElement(
Expand Down

0 comments on commit 5bed3d7

Please sign in to comment.