|
5 | 5 | import 'dart:async'; |
6 | 6 |
|
7 | 7 | import 'package:analyzer/dart/element/element.dart'; |
| 8 | +import 'package:analyzer/dart/element/element2.dart'; |
8 | 9 | import 'package:build/build.dart'; |
9 | 10 |
|
10 | 11 | import 'constants/reader.dart'; |
@@ -58,11 +59,16 @@ abstract class GeneratorForAnnotation<T> extends Generator { |
58 | 59 | typeChecker, |
59 | 60 | throwOnUnresolved: throwOnUnresolved, |
60 | 61 | )) { |
61 | | - final generatedValue = generateForAnnotatedElement( |
| 62 | + var generatedValue = generateForAnnotatedElement( |
62 | 63 | annotatedElement.element, |
63 | 64 | annotatedElement.annotation, |
64 | 65 | buildStep, |
65 | 66 | ); |
| 67 | + generatedValue ??= generateForAnnotatedElement2( |
| 68 | + annotatedElement.element2, |
| 69 | + annotatedElement.annotation, |
| 70 | + buildStep, |
| 71 | + ); |
66 | 72 | await for (var value in normalizeGeneratorOutput(generatedValue)) { |
67 | 73 | assert(value.length == value.trim().length); |
68 | 74 | values.add(value); |
@@ -93,5 +99,28 @@ abstract class GeneratorForAnnotation<T> extends Generator { |
93 | 99 | Element element, |
94 | 100 | ConstantReader annotation, |
95 | 101 | BuildStep buildStep, |
96 | | - ); |
| 102 | + ) {} |
| 103 | + |
| 104 | + /// Implement to return source code to generate for [element]. |
| 105 | + /// |
| 106 | + /// This method is invoked based on finding elements annotated with an |
| 107 | + /// instance of [T]. The [annotation] is provided as a [ConstantReader]. |
| 108 | + /// |
| 109 | + /// Supported return values include a single [String] or multiple [String] |
| 110 | + /// instances within an [Iterable] or [Stream]. It is also valid to return a |
| 111 | + /// [Future] of [String], [Iterable], or [Stream]. When multiple values are |
| 112 | + /// returned through an iterable or stream they will be deduplicated. |
| 113 | + /// Typically each value will be an independent unit of code and the |
| 114 | + /// deduplication prevents re-defining the same member multiple times. For |
| 115 | + /// example if multiple annotated elements may need a specific utility method |
| 116 | + /// available it can be output for each one, and the single deduplicated |
| 117 | + /// definition can be shared. |
| 118 | + /// |
| 119 | + /// Implementations should return `null` when no content is generated. Empty |
| 120 | + /// or whitespace-only [String] instances are also ignored. |
| 121 | + dynamic generateForAnnotatedElement2( |
| 122 | + Element2 element, |
| 123 | + ConstantReader annotation, |
| 124 | + BuildStep buildStep, |
| 125 | + ) {} |
97 | 126 | } |
0 commit comments