Skip to content

Commit 117ab26

Browse files
authored
Add APIs to expose analyzer elements2 models. (dart-lang#735)
* publish_to: none * Directives are not elements anymore, don't have annotations.
1 parent 49a96d8 commit 117ab26

19 files changed

+395
-124
lines changed

analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ analyzer:
88

99
linter:
1010
rules:
11+
- analyzer_use_new_elements
1112
- avoid_bool_literals_in_conditional_expressions
1213
- avoid_classes_with_only_static_members
1314
- avoid_private_typedef_functions

example/lib/src/member_count_library_generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class MemberCountLibraryGenerator extends Generator {
1313
final topLevelVarCount = topLevelNumVariables(library).length;
1414

1515
return '''
16-
// Source library: ${library.element.source.uri}
16+
// Source library: ${library.element2.uri}
1717
const topLevelNumVarCount = $topLevelVarCount;
1818
''';
1919
}

example/lib/src/multiplier_generator.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/element/element.dart';
5+
import 'package:analyzer/dart/element/element2.dart';
66
import 'package:build/build.dart';
77
import 'package:source_gen/source_gen.dart';
88

99
import '../annotations.dart';
1010

1111
class MultiplierGenerator extends GeneratorForAnnotation<Multiplier> {
1212
@override
13-
String generateForAnnotatedElement(
14-
Element element,
13+
String generateForAnnotatedElement2(
14+
Element2 element,
1515
ConstantReader annotation,
1616
BuildStep buildStep,
1717
) {
1818
final numValue = annotation.read('value').literalValue as num;
1919

20-
return 'num ${element.name}Multiplied() => ${element.name} * $numValue;';
20+
return 'num ${element.name3}Multiplied() => ${element.name3} * $numValue;';
2121
}
2222
}

example/lib/src/property_product_generator.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class PropertyProductGenerator extends Generator {
1111
@override
1212
String generate(LibraryReader library, BuildStep buildStep) {
1313
final productNames = topLevelNumVariables(library)
14-
.map((element) => element.name)
14+
.map((element) => element.name3)
15+
.nonNulls
1516
.join(' * ');
1617

1718
return '''

example/lib/src/property_sum_generator.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ class PropertySumGenerator extends Generator {
1111
@override
1212
String generate(LibraryReader library, BuildStep buildStep) {
1313
final sumNames = topLevelNumVariables(library)
14-
.map((element) => element.name)
14+
.map((element) => element.name3)
15+
.nonNulls
1516
.join(' + ');
1617

1718
return '''

example/lib/src/utils.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import 'package:analyzer/dart/element/element.dart';
5+
import 'package:analyzer/dart/element/element2.dart';
66
import 'package:source_gen/source_gen.dart';
77

8-
/// Returns all [TopLevelVariableElement] members in [reader]'s library that
8+
/// Returns all [TopLevelVariableElement2] members in [reader]'s library that
99
/// have a type of [num].
10-
Iterable<TopLevelVariableElement> topLevelNumVariables(LibraryReader reader) =>
11-
reader.allElements.whereType<TopLevelVariableElement>().where(
10+
Iterable<TopLevelVariableElement2> topLevelNumVariables(LibraryReader reader) =>
11+
reader.allElements2.whereType<TopLevelVariableElement2>().where(
1212
(element) =>
1313
element.type.isDartCoreNum ||
1414
element.type.isDartCoreInt ||

source_gen/lib/source_gen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ export 'src/generator.dart'
1010
show Generator, InvalidGenerationSource, InvalidGenerationSourceError;
1111
export 'src/generator_for_annotation.dart' show GeneratorForAnnotation;
1212
export 'src/library.dart' show AnnotatedElement, LibraryReader;
13-
export 'src/span_for_element.dart' show spanForElement;
13+
export 'src/span_for_element.dart' show spanForElement, spanForElement2;
1414
export 'src/type_checker.dart' show TypeChecker, UnresolvedAnnotationException;
1515
export 'src/utils.dart' show typeNameOf;

source_gen/lib/src/builder.dart

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import 'dart:convert';
66

77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
10+
// ignore: implementation_imports
11+
import 'package:analyzer/src/utilities/extensions/element.dart';
912
import 'package:build/build.dart';
1013
import 'package:dart_style/dart_style.dart';
1114
import 'package:pub_semver/pub_semver.dart';
@@ -103,16 +106,17 @@ class _Builder extends Builder {
103106
}
104107

105108
final lib = await buildStep.resolver
106-
.libraryFor(buildStep.inputId, allowSyntaxErrors: allowSyntaxErrors);
109+
.libraryFor2(buildStep.inputId, allowSyntaxErrors: allowSyntaxErrors);
107110
await _generateForLibrary(lib, buildStep);
108111
}
109112

110113
Future<void> _generateForLibrary(
111-
LibraryElement library,
114+
LibraryElement2 library2,
112115
BuildStep buildStep,
113116
) async {
117+
final library = library2.asElement;
114118
final generatedOutputs =
115-
await _generate(library, _generators, buildStep).toList();
119+
await _generate(library2, _generators, buildStep).toList();
116120

117121
// Don't output useless files.
118122
//
@@ -353,11 +357,11 @@ class LibraryBuilder extends _Builder {
353357
}
354358

355359
Stream<GeneratedOutput> _generate(
356-
LibraryElement library,
360+
LibraryElement2 library2,
357361
List<Generator> generators,
358362
BuildStep buildStep,
359363
) async* {
360-
final libraryReader = LibraryReader(library);
364+
final libraryReader = LibraryReader.v2(library2);
361365
for (var i = 0; i < generators.length; i++) {
362366
final gen = generators[i];
363367
var msg = 'Running $gen';

source_gen/lib/src/generator.dart

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import 'dart:async';
66

77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
10+
// ignore: implementation_imports
11+
import 'package:analyzer/src/utilities/extensions/element.dart';
912
import 'package:build/build.dart';
1013

1114
import 'library.dart';
@@ -49,7 +52,7 @@ class InvalidGenerationSource implements Exception {
4952
///
5053
/// May be `null` if the error had no associated element, or if the location
5154
/// was passed with [node].
52-
final Element? element;
55+
final Element2? element2;
5356

5457
/// The AST Node associated with this error.
5558
///
@@ -60,9 +63,18 @@ class InvalidGenerationSource implements Exception {
6063
InvalidGenerationSource(
6164
this.message, {
6265
this.todo = '',
63-
this.element,
66+
Element? element,
6467
this.node,
65-
});
68+
}) : element2 = element?.asElement2;
69+
70+
InvalidGenerationSource.v2(
71+
this.message, {
72+
this.todo = '',
73+
Element2? element,
74+
this.node,
75+
}) : element2 = element;
76+
77+
Element? get element => element2?.asElement;
6678

6779
@override
6880
String toString() {

source_gen/lib/src/generator_for_annotation.dart

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import 'dart:async';
66

77
import 'package:analyzer/dart/element/element.dart';
8+
import 'package:analyzer/dart/element/element2.dart';
89
import 'package:build/build.dart';
910

1011
import 'constants/reader.dart';
@@ -58,11 +59,16 @@ abstract class GeneratorForAnnotation<T> extends Generator {
5859
typeChecker,
5960
throwOnUnresolved: throwOnUnresolved,
6061
)) {
61-
final generatedValue = generateForAnnotatedElement(
62+
var generatedValue = generateForAnnotatedElement(
6263
annotatedElement.element,
6364
annotatedElement.annotation,
6465
buildStep,
6566
);
67+
generatedValue ??= generateForAnnotatedElement2(
68+
annotatedElement.element2,
69+
annotatedElement.annotation,
70+
buildStep,
71+
);
6672
await for (var value in normalizeGeneratorOutput(generatedValue)) {
6773
assert(value.length == value.trim().length);
6874
values.add(value);
@@ -93,5 +99,28 @@ abstract class GeneratorForAnnotation<T> extends Generator {
9399
Element element,
94100
ConstantReader annotation,
95101
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+
) {}
97126
}

0 commit comments

Comments
 (0)