Skip to content

Bring up to analyzer 8.1.1, get rid of deprecated classes and methods. #782

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 3 additions & 5 deletions example/lib/src/multiplier_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: deprecated_member_use until analyzer 7 support is dropped.

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

Expand All @@ -13,13 +11,13 @@ import '../annotations.dart';
class MultiplierGenerator extends GeneratorForAnnotation<Multiplier> {
@override
String generateForAnnotatedElement(
Element2 element,
Element element,
ConstantReader annotation,
BuildStep buildStep,
) {
final numValue = annotation.read('value').literalValue as num;

return 'num ${element.name3}Multiplied() => ${element.name3} * $numValue;';
return 'num ${element.name}Multiplied() => ${element.name} * $numValue;';
}

// Override this method to respond to annotations on directives: `import`,
Expand Down
3 changes: 1 addition & 2 deletions example/lib/src/property_product_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class PropertyProductGenerator extends Generator {
String generate(LibraryReader library, BuildStep buildStep) {
final productNames = topLevelNumVariables(
library,
// ignore: deprecated_member_use until analyzer 7 support is dropped.
).map((element) => element.name3).join(' * ');
).map((element) => element.name).join(' * ');

return '''
num allProduct() => $productNames;
Expand Down
3 changes: 1 addition & 2 deletions example/lib/src/property_sum_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ class PropertySumGenerator extends Generator {
String generate(LibraryReader library, BuildStep buildStep) {
final sumNames = topLevelNumVariables(
library,
// ignore: deprecated_member_use until analyzer 7 support is dropped.
).map((element) => element.name3).join(' + ');
).map((element) => element.name).join(' + ');

return '''
num allSum() => $sumNames;
Expand Down
10 changes: 4 additions & 6 deletions example/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: deprecated_member_use until analyzer 7 support is dropped.

import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:source_gen/source_gen.dart';

/// Returns all [TopLevelVariableElement2] members in [reader]'s library that
/// Returns all [TopLevelVariableElement] members in [reader]'s library that
/// have a type of [num].
Iterable<TopLevelVariableElement2> topLevelNumVariables(LibraryReader reader) =>
reader.allElements.whereType<TopLevelVariableElement2>().where(
Iterable<TopLevelVariableElement> topLevelNumVariables(LibraryReader reader) =>
reader.allElements.whereType<TopLevelVariableElement>().where(
(element) =>
element.type.isDartCoreNum ||
element.type.isDartCoreInt ||
Expand Down
12 changes: 4 additions & 8 deletions source_gen/lib/src/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import 'dart:convert';

import 'package:analyzer/dart/ast/ast.dart';
// ignore: deprecated_member_use until analyzer 7 support is dropped.
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:dart_style/dart_style.dart';
import 'package:pub_semver/pub_semver.dart';
Expand Down Expand Up @@ -110,8 +109,7 @@ class _Builder extends Builder {
}

Future<void> _generateForLibrary(
// ignore: deprecated_member_use until analyzer 7 support is dropped.
LibraryElement2 library,
LibraryElement library,
BuildStep buildStep,
) async {
final generatedOutputs =
Expand Down Expand Up @@ -357,8 +355,7 @@ class LibraryBuilder extends _Builder {
}

Stream<GeneratedOutput> _generate(
// ignore: deprecated_member_use until analyzer 7 support is dropped.
LibraryElement2 library,
LibraryElement library,
List<Generator> generators,
BuildStep buildStep,
) async* {
Expand Down Expand Up @@ -443,8 +440,7 @@ const partIdRegExpLiteral = r'[A-Za-z_\d-]+';

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

// ignore: deprecated_member_use until analyzer 7 support is dropped.
String languageOverrideForLibrary(LibraryElement2 library) {
String languageOverrideForLibrary(LibraryElement library) {
final override = library.languageVersion.override;
return override == null
? ''
Expand Down
6 changes: 2 additions & 4 deletions source_gen/lib/src/constants/reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/constant/value.dart';
// ignore: deprecated_member_use until analyzer 7 support is dropped.
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';

import '../type_checker.dart';
Expand Down Expand Up @@ -269,8 +268,7 @@ class _DartObjectConstant extends ConstantReader {
ConstantReader read(String field) {
final reader = peek(field);
if (reader == null) {
// ignore: deprecated_member_use until analyzer 7 support is dropped.
assertHasField(objectValue.type!.element3 as InterfaceElement2, field);
assertHasField(objectValue.type!.element as InterfaceElement, field);
return const _NullConstant();
}
return reader;
Expand Down
39 changes: 18 additions & 21 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
// BSD-style license that can be found in the LICENSE file.

// TODO(kevmoo): migrate analyzer APIs when we can get latest with a stable SDK
// ignore_for_file: deprecated_member_use

import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/constant/value.dart' show DartObjectImpl;

import '../utils.dart';

Expand All @@ -22,37 +19,37 @@ import '../utils.dart';
/// **NOTE**: Some returned [Revivable] instances are not representable as valid
/// Dart source code (such as referencing private constructors). It is up to the
/// build tool(s) using this library to surface error messages to the user.
Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
final objectType = object.type;
Element2? element = objectType!.alias?.element2;
Element? element = objectType!.alias?.element;
if (element == null) {
if (objectType is InterfaceType) {
element = objectType.element3;
element = objectType.element;
} else {
element = object.toFunctionValue2();
element = object.toFunctionValue();
}
}
origin ??= element!.library2;
origin ??= element!.library;
var url = Uri.parse(urlOfElement(element!));
if (element is TopLevelFunctionElement || element is LocalFunctionElement) {
return Revivable._(source: url.removeFragment(), accessor: element.name3!);
return Revivable._(source: url.removeFragment(), accessor: element.name!);
}

if (element is MethodElement2 && element.isStatic) {
if (element is MethodElement && element.isStatic) {
return Revivable._(
source: url.removeFragment(),
accessor:
'${element.firstFragment.enclosingFragment!.name2}.${element.name3}',
'${element.firstFragment.enclosingFragment!.name}.${element.name}',
);
}

if (element is InterfaceElement2) {
for (final e in element.fields2.where(
if (element is InterfaceElement) {
for (final e in element.fields.where(
(f) => f.isPublic && f.isConst && f.computeConstantValue() == object,
)) {
return Revivable._(
source: url.removeFragment(),
accessor: '${element.name3}.${e.name3}',
accessor: '${element.name}.${e.name}',
);
}
}
Expand All @@ -67,25 +64,25 @@ Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
}

for (final type in origin!.classes) {
for (final e in type.fields2.where(
for (final e in type.fields.where(
(f) => f.isConst && f.computeConstantValue() == object,
)) {
final result = Revivable._(
source: url.removeFragment(),
accessor: '${type.name3}.${e.name3}',
accessor: '${type.name}.${e.name}',
);
if (tryResult(result)) {
return result;
}
}
}
final i = (object as DartObjectImpl).getInvocation();
final i = object.constructorInvocation;
if (i != null) {
url = Uri.parse(urlOfElement(i.constructor2.enclosingElement2));
url = Uri.parse(urlOfElement(i.constructor.enclosingElement));
String newToEmpty(String string) => string == 'new' ? '' : string;
final result = Revivable._(
source: url,
accessor: newToEmpty(i.constructor2.name3!),
accessor: newToEmpty(i.constructor.name!),
namedArguments: i.namedArguments,
positionalArguments: i.positionalArguments,
);
Expand All @@ -98,7 +95,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
)) {
final result = Revivable._(
source: Uri.parse(urlOfElement(origin)).replace(fragment: ''),
accessor: e.name3!,
accessor: e.name!,
);
if (tryResult(result)) {
return result;
Expand Down
22 changes: 10 additions & 12 deletions source_gen/lib/src/constants/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,29 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// ignore_for_file: deprecated_member_use until analyzer 7 support is dropped.

import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';

/// Throws a [FormatException] if [root] does not have a given field [name].
///
/// Super types [InterfaceElement2.supertype] are also checked before throwing.
void assertHasField(InterfaceElement2 root, String name) {
InterfaceElement2? element = root;
/// Super types [InterfaceElement.supertype] are also checked before throwing.
void assertHasField(InterfaceElement root, String name) {
InterfaceElement? element = root;
while (element != null) {
final field = element.getField2(name);
final field = element.getField(name);
if (field != null) {
return;
}
element = element.supertype?.element3;
element = element.supertype?.element;
}
final allFields = {
...root.fields2,
for (var t in root.allSupertypes) ...t.element3.fields2,
...root.fields,
for (var t in root.allSupertypes) ...t.element.fields,
};

throw FormatException(
'Class ${root.name3} does not have field "$name".',
'Fields: \n - ${allFields.map((e) => e.name3).join('\n - ')}',
'Class ${root.name} does not have field "$name".',
'Fields: \n - ${allFields.map((e) => e.name).join('\n - ')}',
);
}

Expand Down
6 changes: 2 additions & 4 deletions source_gen/lib/src/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import 'dart:async';

import 'package:analyzer/dart/ast/ast.dart';
// ignore: deprecated_member_use until analyzer 7 support is dropped.
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';
import 'package:source_span/source_span.dart';

Expand Down Expand Up @@ -48,8 +47,7 @@ class InvalidGenerationSource implements Exception {
final String todo;

/// The `Element2` associated with this error, if any.
/// ignore: deprecated_member_use until analyzer 7 support is dropped.
final Element2? element;
final Element? element;

/// The [ElementDirective] associated with this error, if any.
final ElementDirective? elementDirective;
Expand Down
10 changes: 3 additions & 7 deletions source_gen/lib/src/generator_for_annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@

import 'dart:async';

// ignore: deprecated_member_use until analyzer 7 support is dropped.
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:build/build.dart';

import 'constants/reader.dart';
Expand Down Expand Up @@ -68,9 +67,7 @@ abstract class GeneratorForAnnotation<T> extends Generator {
this.inSdk,
});

// This will switch to `typeNamed` in 4.0.0.
// ignore: deprecated_member_use_from_same_package
TypeChecker get typeChecker => TypeChecker.fromRuntime(T);
TypeChecker get typeChecker => TypeChecker.typeNamed(T);

@override
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
Expand Down Expand Up @@ -127,8 +124,7 @@ abstract class GeneratorForAnnotation<T> extends Generator {
/// Implementations should return `null` when no content is generated. Empty
/// or whitespace-only [String] instances are also ignored.
dynamic generateForAnnotatedElement(
// ignore: deprecated_member_use until analyzer 7 support is dropped.
Element2 element,
Element element,
ConstantReader annotation,
BuildStep buildStep,
) {}
Expand Down
Loading
Loading