Skip to content

Commit

Permalink
Reseparate args to generateForAnnotatedElement (dart-lang/source_gen#361
Browse files Browse the repository at this point in the history
)

Partial revert of 92fe9af

While migrating usages it became apparent that repeatedly needing to
dereference `annotatedElement` is annoying.
  • Loading branch information
natebosch authored Jul 18, 2018
1 parent 9177e2f commit 2c3fe5d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
8 changes: 2 additions & 6 deletions source_gen/source_gen/source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,9 @@
* Introduce `SharedPartBuilder` for creating part files that can be merged
with a new `CombiningBuilder`. Note that `CombiningBuilder` only outputs
`.g.dart` files.
* `PartBuilder` now requires a `generatedExtensions` argument. The value should
not be `.g.dart`. To produce `.g.dart` files please use the
* **Breaking** `PartBuilder` now requires a `generatedExtensions` argument. The
value should not be `.g.dart`. To produce `.g.dart` files please use the
`SharedPartBuilder`.
* `GeneratorForAnnotation`
* **BREAKING** `generateForAnnotatedElement` now takes two arguments instead
of three: `(AnnotatedElement annotatedElement, BuildStep buildStep)`.
`AnnotatedElement` contains the `element` and `annotation` values.

## 0.8.3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

import 'dart:async';

import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';

import 'constants/reader.dart';
import 'generator.dart';
import 'library.dart';
import 'output_helpers.dart';
Expand Down Expand Up @@ -42,8 +44,8 @@ abstract class GeneratorForAnnotation<T> extends Generator {
var values = new Set<String>();

for (var annotatedElement in library.annotatedWith(typeChecker)) {
var generatedValue =
generateForAnnotatedElement(annotatedElement, buildStep);
var generatedValue = generateForAnnotatedElement(
annotatedElement.element, annotatedElement.annotation, buildStep);
await for (var value in normalizeGeneratorOutput(generatedValue)) {
assert(value == null || (value.length == value.trim().length));
values.add(value);
Expand All @@ -53,10 +55,10 @@ abstract class GeneratorForAnnotation<T> extends Generator {
return values.join('\n\n');
}

/// Implement to return source code to generate for [annotatedElement].
/// Implement to return source code to generate for [element].
///
/// This method is invoked based on finding elements annotated with an
/// instance of [T].
/// instance of [T]. The [annotation] is provided as a [ConstantReader].
///
/// Supported return values include a single [String] or multiple [String]
/// instances within an [Iterable] or [Stream]. It is also valid to return a
Expand All @@ -65,5 +67,5 @@ abstract class GeneratorForAnnotation<T> extends Generator {
/// Implementations should return `null` when no content is generated. Empty
/// or whitespace-only [String] instances are also ignored.
generateForAnnotatedElement(
AnnotatedElement annotatedElement, BuildStep buildStep);
Element element, ConstantReader annotation, BuildStep buildStep);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// The first test that runs `testBuilder` takes a LOT longer than the rest.
@Timeout.factor(3)

import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:build_test/build_test.dart';
import 'package:test/test.dart';
Expand Down Expand Up @@ -78,7 +79,7 @@ class FailingIterableGenerator extends GeneratorForAnnotation<Deprecated> {

@override
Iterable<String> generateForAnnotatedElement(
AnnotatedElement annotatedElement, BuildStep buildStep) sync* {
Element element, ConstantReader annotation, BuildStep buildStep) sync* {
yield '// There are deprecated values in this library!';
throw new StateError('not supported!');
}
Expand All @@ -92,7 +93,7 @@ class FailingGenerator extends GeneratorForAnnotation<Deprecated> {

@override
generateForAnnotatedElement(
AnnotatedElement annotatedElement, BuildStep buildStep) {
Element element, ConstantReader annotation, BuildStep buildStep) {
throw new StateError('not supported!');
}
}
Expand All @@ -102,10 +103,10 @@ class RepeatingGenerator extends GeneratorForAnnotation<Deprecated> {

@override
Iterable<String> generateForAnnotatedElement(
AnnotatedElement annotatedElement, BuildStep buildStep) sync* {
Element element, ConstantReader annotation, BuildStep buildStep) sync* {
yield '// There are deprecated values in this library!';

yield '// ${annotatedElement.element}';
yield '// $element';
}
}

Expand All @@ -116,7 +117,7 @@ class LiteralOutput<T> extends GeneratorForAnnotation<Deprecated> {

@override
T generateForAnnotatedElement(
AnnotatedElement annotatedElement, BuildStep buildStep) =>
Element element, ConstantReader annotation, BuildStep buildStep) =>
null;
}

Expand Down

0 comments on commit 2c3fe5d

Please sign in to comment.