Skip to content

Commit f051bcc

Browse files
committed
Changes to satisfy analyzer_use_new_elements lint.
1 parent ee4960c commit f051bcc

18 files changed

+194
-171
lines changed

source_gen/lib/builder.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ class CombiningBuilder implements Builder {
119119
.join('\n\n');
120120
if (assets.isEmpty) return;
121121

122-
final inputLibrary = await buildStep.inputLibrary;
122+
final inputLibrary = await buildStep.inputLibrary2;
123123
final outputId = buildStep.allowedOutputs.single;
124-
final partOfUri = uriOfPartial(inputLibrary, buildStep.inputId, outputId);
124+
final partOfUri = uriOfPartial2(inputLibrary, buildStep.inputId, outputId);
125125

126126
// Ensure that the input has a correct `part` statement.
127127
final libraryUnit =
@@ -143,7 +143,7 @@ class CombiningBuilder implements Builder {
143143

144144
final output = '''
145145
$defaultFileHeader
146-
${languageOverrideForLibrary(inputLibrary)}$ignoreForFile$preamble
146+
${languageOverrideForLibrary2(inputLibrary)}$ignoreForFile$preamble
147147
part of '$partOfUri';
148148
149149
$assets

source_gen/lib/src/builder.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import 'dart:convert';
77
import 'package:analyzer/dart/ast/ast.dart';
88
import 'package:analyzer/dart/element/element.dart';
99
import 'package:analyzer/dart/element/element2.dart';
10-
// ignore: implementation_imports
11-
import 'package:analyzer/src/utilities/extensions/element.dart';
1210
import 'package:build/build.dart';
1311
import 'package:dart_style/dart_style.dart';
1412
import 'package:pub_semver/pub_semver.dart';
@@ -114,7 +112,6 @@ class _Builder extends Builder {
114112
LibraryElement2 library2,
115113
BuildStep buildStep,
116114
) async {
117-
final library = library2.asElement;
118115
final generatedOutputs =
119116
await _generate(library2, _generators, buildStep).toList();
120117

@@ -133,12 +130,12 @@ class _Builder extends Builder {
133130

134131
if (!_isLibraryBuilder) {
135132
final asset = buildStep.inputId;
136-
final partOfUri = uriOfPartial(library, asset, outputId);
133+
final partOfUri = uriOfPartial2(library2, asset, outputId);
137134
contentBuffer.writeln();
138135

139136
if (this is PartBuilder) {
140137
contentBuffer
141-
..write(languageOverrideForLibrary(library))
138+
..write(languageOverrideForLibrary2(library2))
142139
..writeln('part of \'$partOfUri\';');
143140
final part = computePartUrl(buildStep.inputId, outputId);
144141

@@ -181,12 +178,12 @@ class _Builder extends Builder {
181178

182179
try {
183180
genPartContent =
184-
formatOutput(genPartContent, library.languageVersion.effective);
181+
formatOutput(genPartContent, library2.languageVersion.effective);
185182
} catch (e, stack) {
186183
log.severe(
187184
'''
188185
An error `${e.runtimeType}` occurred while formatting the generated source for
189-
`${library.identifier}`
186+
`${library2.identifier}`
190187
which was output to
191188
`${outputId.path}`.
192189
This may indicate an issue in the generator, the input source code, or in the
@@ -428,13 +425,21 @@ const partIdRegExpLiteral = r'[A-Za-z_\d-]+';
428425

429426
final _partIdRegExp = RegExp('^$partIdRegExpLiteral\$');
430427

428+
@deprecated
431429
String languageOverrideForLibrary(LibraryElement library) {
432430
final override = library.languageVersion.override;
433431
return override == null
434432
? ''
435433
: '// @dart=${override.major}.${override.minor}\n';
436434
}
437435

436+
String languageOverrideForLibrary2(LibraryElement2 library) {
437+
final override = library.languageVersion.override;
438+
return override == null
439+
? ''
440+
: '// @dart=${override.major}.${override.minor}\n';
441+
}
442+
438443
/// A comment configuring `dart_style` to use the default code width so no
439444
/// configuration discovery is required.
440445
const dartFormatWidth = '// dart format width=80';

source_gen/lib/src/constants/revive.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import '../utils.dart';
2222
/// **NOTE**: Some returned [Revivable] instances are not representable as valid
2323
/// Dart source code (such as referencing private constructors). It is up to the
2424
/// build tool(s) using this library to surface error messages to the user.
25+
@deprecated
2526
Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
2627
final objectType = object.type;
2728
Element? element = objectType!.alias?.element;

source_gen/lib/src/constants/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void assertHasField(InterfaceElement root, String name) {
3232

3333
/// Throws a [FormatException] if [root] does not have a given field [name].
3434
///
35-
/// Super types [InterfaceElement.supertype] are also checked before throwing.
35+
/// Super types [InterfaceElement2.supertype] are also checked before throwing.
3636
void assertHasField2(InterfaceElement2 root, String name) {
3737
InterfaceElement2? element = root;
3838
while (element != null) {

source_gen/lib/src/generator.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class InvalidGenerationSource implements Exception {
6060
/// code, or if the location was passed with [element].
6161
final AstNode? node;
6262

63+
@deprecated
6364
InvalidGenerationSource(
6465
this.message, {
6566
this.todo = '',
@@ -74,15 +75,16 @@ class InvalidGenerationSource implements Exception {
7475
this.node,
7576
}) : element2 = element;
7677

78+
@deprecated
7779
Element? get element => element2?.asElement;
7880

7981
@override
8082
String toString() {
8183
final buffer = StringBuffer(message);
8284

83-
if (element case final element?) {
85+
if (element2 case final element2?) {
8486
try {
85-
final span = spanForElement(element);
87+
final span = spanForElement2(element2);
8688
buffer
8789
..writeln()
8890
..writeln(span.start.toolString)
@@ -92,7 +94,7 @@ class InvalidGenerationSource implements Exception {
9294
// associated source. We can still give the name.
9395
buffer
9496
..writeln()
95-
..writeln('Cause: $element');
97+
..writeln('Cause: $element2');
9698
}
9799
}
98100

source_gen/lib/src/generator_for_annotation.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import 'type_checker.dart';
3939
/// Elements which are not at the top level, such as the members of a class or
4040
/// extension, are not searched for annotations. To operate on, for instance,
4141
/// annotated fields of a class ensure that the class itself is annotated with
42-
/// [T] and use the [Element] to iterate over fields. The [TypeChecker] utility
42+
/// [T] and use the [Element2] to iterate over fields. The [TypeChecker] utility
4343
/// may be helpful to check which elements have a given annotation.
4444
abstract class GeneratorForAnnotation<T> extends Generator {
4545
final bool throwOnUnresolved;
@@ -74,13 +74,15 @@ abstract class GeneratorForAnnotation<T> extends Generator {
7474
typeChecker,
7575
throwOnUnresolved: throwOnUnresolved,
7676
)) {
77-
var generatedValue = generateForAnnotatedElement(
78-
annotatedElement.element,
77+
var generatedValue = generateForAnnotatedElement2(
78+
annotatedElement.element2,
7979
annotatedElement.annotation,
8080
buildStep,
8181
);
82-
generatedValue ??= generateForAnnotatedElement2(
83-
annotatedElement.element2,
82+
generatedValue ??= generateForAnnotatedElement(
83+
// Support "generateForAnnotatedElements" until it's removed.
84+
// ignore: analyzer_use_new_elements
85+
annotatedElement.element,
8486
annotatedElement.annotation,
8587
buildStep,
8688
);
@@ -110,6 +112,7 @@ abstract class GeneratorForAnnotation<T> extends Generator {
110112
///
111113
/// Implementations should return `null` when no content is generated. Empty
112114
/// or whitespace-only [String] instances are also ignored.
115+
@deprecated
113116
dynamic generateForAnnotatedElement(
114117
Element element,
115118
ConstantReader annotation,

source_gen/lib/src/library.dart

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class AnnotatedElement {
3030

3131
const AnnotatedElement(this.annotation, this.element2);
3232

33+
@deprecated
3334
Element get element => element2.asElement!;
3435

3536
Metadata? get metadata2 {
@@ -40,17 +41,20 @@ class AnnotatedElement {
4041
}
4142
}
4243

43-
/// A high-level wrapper API with common functionality for [LibraryElement].
44+
/// A high-level wrapper API with common functionality for [LibraryElement2].
4445
class LibraryReader {
4546
final LibraryElement2 element2;
4647

48+
@deprecated
4749
LibraryReader(LibraryElement element) : this.v2(element.asElement2);
4850

4951
LibraryReader.v2(this.element2);
5052

53+
@deprecated
5154
LibraryElement get element => element2.asElement;
5255

5356
/// All of the declarations in this library.
57+
@deprecated
5458
Iterable<Element> get allElements => [
5559
element,
5660
...element.topLevelElements,
@@ -63,13 +67,15 @@ class LibraryReader {
6367
Iterable<Element2> get allElements2 => [element2, ...element2.children2];
6468

6569
/// All of the elements representing classes in this library.
70+
@deprecated
6671
Iterable<ClassElement> get classes =>
6772
element.units.expand((cu) => cu.classes);
6873

6974
/// All of the elements representing classes in this library.
7075
Iterable<ClassElement2> get classes2 => element2.classes;
7176

7277
/// All of the elements representing enums in this library.
78+
@deprecated
7379
Iterable<EnumElement> get enums => element.units.expand((cu) => cu.enums);
7480

7581
/// All of the elements representing enums in this library.
@@ -80,19 +86,14 @@ class LibraryReader {
8086
TypeChecker checker, {
8187
bool throwOnUnresolved = true,
8288
}) sync* {
83-
for (final element in allElements) {
84-
final annotation = checker.firstAnnotationOf(
89+
for (final element in allElements2) {
90+
final annotation = checker.firstAnnotationOf2(
8591
element,
8692
throwOnUnresolved: throwOnUnresolved,
8793
);
8894

89-
final element2 = element.asElement2;
90-
if (element2 == null) {
91-
return;
92-
}
93-
9495
if (annotation != null) {
95-
yield AnnotatedElement(ConstantReader(annotation), element2);
96+
yield AnnotatedElement(ConstantReader(annotation), element);
9697
}
9798
}
9899
}
@@ -126,13 +127,13 @@ class LibraryReader {
126127
TypeChecker checker, {
127128
bool throwOnUnresolved = true,
128129
}) sync* {
129-
for (final element in allElements) {
130-
final annotation = checker.firstAnnotationOfExact(
130+
for (final element in allElements2) {
131+
final annotation = checker.firstAnnotationOfExact2(
131132
element,
132133
throwOnUnresolved: throwOnUnresolved,
133134
);
134135
if (annotation != null) {
135-
yield AnnotatedElement(ConstantReader(annotation), element.asElement2!);
136+
yield AnnotatedElement(ConstantReader(annotation), element);
136137
}
137138
}
138139
}
@@ -147,12 +148,12 @@ class LibraryReader {
147148
return type is ClassElement ? type : null;
148149
}
149150

150-
/// Returns a top-level [ClassElement] publicly visible in by [name].
151+
/// Returns a top-level [ClassElement2] publicly visible in by [name].
151152
///
152-
/// Unlike [LibraryElement.getClass], this also correctly traverses
153+
/// Unlike [LibraryElement2.getClass], this also correctly traverses
153154
/// identifiers that are accessible via one or more `export` directives.
154155
ClassElement2? findType2(String name) {
155-
final type = element.exportNamespace.get2(name);
156+
final type = element2.exportNamespace.get2(name);
156157
return type is ClassElement2 ? type : null;
157158
}
158159

@@ -166,6 +167,7 @@ class LibraryReader {
166167
///
167168
/// This is a typed convenience function for using [pathToUrl], and the same
168169
/// API restrictions hold around supported schemes and relative paths.
170+
@deprecated
169171
Uri pathToElement(Element element) => pathToUrl(element.source!.uri);
170172

171173
/// Returns a [Uri] from the current library to the target [element].
@@ -210,7 +212,7 @@ class LibraryReader {
210212
if (to.pathSegments.length > 1 && to.pathSegments[1] == 'lib') {
211213
return assetToPackageUrl(to);
212214
}
213-
var from = element.source.uri;
215+
var from = element2.uri;
214216
// Normalize (convert to an asset: URL).
215217
from = normalizeUrl(from);
216218
if (_isRelative(from, to)) {

source_gen/lib/src/span_for_element.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import 'utils.dart';
2020
///
2121
/// Not all results from the analyzer API may return source information as part
2222
/// of the element, so [file] may need to be manually provided in those cases.
23+
@deprecated
2324
SourceSpan spanForElement(Element element, [SourceFile? file]) {
2425
final url = assetToPackageUrl(element.source!.uri);
2526
if (file == null) {

0 commit comments

Comments
 (0)