Skip to content

Commit

Permalink
Avoid deprecated analyzer APIs (#616)
Browse files Browse the repository at this point in the history
Bump min analyzer to `^4.6.0` which introduces some of the new APIs
used.

Switch from `ClassElement` to `InterfaceElement` in a utility method to
match the `element2` return type.
  • Loading branch information
natebosch authored Aug 12, 2022
1 parent faa080c commit bd6f791
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 69 deletions.
4 changes: 2 additions & 2 deletions source_gen/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## 1.2.3
## 1.2.3-dev

* Require `analyzer: '>=4.3.0 <5.0.0'`
* Require `analyzer: ^4.4.0`
* Require `sdk: '>=2.17.0 <3.0.0'`

## 1.2.2
Expand Down
2 changes: 1 addition & 1 deletion source_gen/lib/src/constants/reader.dart
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class _DartObjectConstant extends ConstantReader {
ConstantReader read(String field) {
final reader = peek(field);
if (reader == null) {
assertHasField(objectValue.type?.element as ClassElement, field);
assertHasField(objectValue.type?.element2 as ClassElement, field);
return const _NullConstant();
}
return reader;
Expand Down
6 changes: 3 additions & 3 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
Element? element = objectType!.alias?.element;
if (element == null) {
if (objectType is InterfaceType) {
element = objectType.element;
element = objectType.element2;
} else {
element = object.toFunctionValue();
}
Expand All @@ -40,7 +40,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
if (element is MethodElement && element.isStatic) {
return Revivable._(
source: url.removeFragment(),
accessor: '${element.enclosingElement2.name}.${element.name}',
accessor: '${element.enclosingElement3.name}.${element.name}',
);
}

Expand Down Expand Up @@ -78,7 +78,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
}
final i = (object as DartObjectImpl).getInvocation();
if (i != null) {
url = Uri.parse(urlOfElement(i.constructor.enclosingElement2));
url = Uri.parse(urlOfElement(i.constructor.enclosingElement3));
final result = Revivable._(
source: url,
accessor: i.constructor.name,
Expand Down
10 changes: 5 additions & 5 deletions source_gen/lib/src/constants/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import 'package:analyzer/dart/element/element.dart';

/// Throws a [FormatException] if [root] does not have a given field [name].
///
/// Super types [ClassElement.supertype] are also checked before throwing.
void assertHasField(ClassElement root, String name) {
ClassElement? 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.getField(name);
if (field != null) {
return;
}
element = element.supertype?.element;
element = element.supertype?.element2;
}
final allFields = root.fields.toSet()
..addAll(root.allSupertypes.expand((t) => t.element.fields));
..addAll(root.allSupertypes.expand((t) => t.element2.fields));
throw FormatException(
'Class ${root.name} does not have field "$name".',
'Fields: \n - ${allFields.map((e) => e.name).join('\n - ')}',
Expand Down
6 changes: 3 additions & 3 deletions source_gen/lib/src/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class LibraryReader {

/// Returns a top-level [ClassElement] publicly visible in by [name].
///
/// Unlike [LibraryElement.getType], this also correctly traverses identifiers
/// that are accessible via one or more `export` directives.
/// Unlike [LibraryElement.getClass], this also correctly traverses
/// identifiers that are accessible via one or more `export` directives.
ClassElement? findType(String name) {
final type = element.exportNamespace.get(name);
return type is ClassElement ? type : null;
Expand Down Expand Up @@ -163,5 +163,5 @@ class LibraryReader {
element.units.expand((cu) => cu.classes);

/// All of the elements representing enums in this library.
Iterable<ClassElement> get enums => element.units.expand((cu) => cu.enums);
Iterable<EnumElement> get enums => element.units.expand((cu) => cu.enums2);
}
10 changes: 5 additions & 5 deletions source_gen/lib/src/type_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,13 @@ abstract class TypeChecker {

/// Returns `true` if [staticType] can be assigned to this type.
bool isAssignableFromType(DartType staticType) =>
isAssignableFrom(staticType.element!);
isAssignableFrom(staticType.element2!);

/// Returns `true` if representing the exact same class as [element].
bool isExactly(Element element);

/// Returns `true` if representing the exact same type as [staticType].
bool isExactlyType(DartType staticType) => isExactly(staticType.element!);
bool isExactlyType(DartType staticType) => isExactly(staticType.element2!);

/// Returns `true` if representing a super class of [element].
///
Expand All @@ -200,7 +200,7 @@ abstract class TypeChecker {
///
/// This only takes into account the *extends* hierarchy. If you wish
/// to check mixins and interfaces, use [isAssignableFromType].
bool isSuperTypeOf(DartType staticType) => isSuperOf(staticType.element!);
bool isSuperTypeOf(DartType staticType) => isSuperOf(staticType.element2!);
}

// Checks a static type against another static type;
Expand All @@ -211,10 +211,10 @@ class _LibraryTypeChecker extends TypeChecker {

@override
bool isExactly(Element element) =>
element is ClassElement && element == _type.element;
element is ClassElement && element == _type.element2;

@override
String toString() => urlOfElement(_type.element!);
String toString() => urlOfElement(_type.element2!);
}

// Checks a runtime type against a static type.
Expand Down
4 changes: 2 additions & 2 deletions source_gen/lib/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ String typeNameOf(DartType type) {
return 'dynamic';
}
if (type is InterfaceType) {
return type.element.name;
return type.element2.name;
}
if (type is TypeParameterType) {
return type.element.name;
return type.element2.name;
}
throw UnimplementedError('(${type.runtimeType}) $type');
}
Expand Down
4 changes: 2 additions & 2 deletions source_gen/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 1.2.3
version: 1.2.3-dev
description: >-
Source code generation builders and utilities for the Dart build system
repository: https://github.com/dart-lang/source_gen
Expand All @@ -8,7 +8,7 @@ environment:
sdk: '>=2.17.0 <3.0.0'

dependencies:
analyzer: '>=4.3.0 <5.0.0'
analyzer: ^4.6.0
async: ^2.5.0
build: ^2.1.0
dart_style: ^2.0.0
Expand Down
8 changes: 4 additions & 4 deletions source_gen/test/constants/utils_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,17 @@ void main() {
});

test('should not throw when a class contains a field', () {
final $A = testLib.getType('A')!;
final $A = testLib.getClass('A')!;
expect(() => assertHasField($A, 'a'), returnsNormally);
});

test('should not throw when a super class contains a field', () {
final $B = testLib.getType('B')!;
final $B = testLib.getClass('B')!;
expect(() => assertHasField($B, 'a'), returnsNormally);
});

test('should throw when a class does not contain a field', () {
final $C = testLib.getType('C')!;
final $C = testLib.getClass('C')!;
expect(() => assertHasField($C, 'a'), throwsFormatException);
});
});
Expand Down Expand Up @@ -83,7 +83,7 @@ void main() {
(resolver) async => (await resolver.findLibraryByName('test_lib'))!,
);
objects = testLib
.getType('Example')!
.getClass('Example')!
.metadata
.map((e) => e.computeConstantValue()!)
.toList();
Expand Down
6 changes: 3 additions & 3 deletions source_gen/test/constants_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void main() {
(resolver) async => (await resolver.findLibraryByName('test_lib'))!,
);
constants = library
.getType('Example')!
.getClass('Example')!
.metadata
.map((e) => ConstantReader(e.computeConstantValue()!))
.toList();
Expand Down Expand Up @@ -156,7 +156,7 @@ void main() {

test('should read a Type', () {
expect(constants[11].isType, isTrue);
expect(constants[11].typeValue.element!.name, 'DateTime');
expect(constants[11].typeValue.element2!.name, 'DateTime');
expect(constants[11].isLiteral, isFalse);
expect(() => constants[11].literalValue, throwsFormatException);
});
Expand Down Expand Up @@ -301,7 +301,7 @@ void main() {
(resolver) async => (await resolver.findLibraryByName('test_lib'))!,
);
constants = library
.getType('Example')!
.getClass('Example')!
.metadata
.map((e) => ConstantReader(e.computeConstantValue()))
.toList();
Expand Down
4 changes: 2 additions & 2 deletions source_gen/test/external_only_type_checker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ void main() {
expect(
checkNonPublic().isExactlyType(staticNonPublic),
isTrue,
reason: '${checkNonPublic()} != ${staticNonPublic.element.name}',
reason: '${checkNonPublic()} != ${staticNonPublic.element2.name}',
);
});

Expand All @@ -60,7 +60,7 @@ void main() {
checkNonPublic().isAssignableFromType(staticNonPublic),
isTrue,
reason: '${checkNonPublic()} is not assignable from '
'${staticNonPublic.element.name}',
'${staticNonPublic.element2.name}',
);
});
});
Expand Down
10 changes: 5 additions & 5 deletions source_gen/test/span_for_element_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class Example implements List {

test('should highlight the use of "class Example"', () async {
expect(
spanForElement(library.getType('Example')!).message('Here it is'),
spanForElement(library.getClass('Example')!).message('Here it is'),
r"""
line 3, column 16 of package:test_lib/test_lib.dart: Here it is
,
Expand All @@ -47,7 +47,7 @@ line 3, column 16 of package:test_lib/test_lib.dart: Here it is

test('should correctly highlight getter', () async {
expect(
spanForElement(library.getType('Example')!.getField('getter')!)
spanForElement(library.getClass('Example')!.getField('getter')!)
.message('Here it is'),
r"""
line 4, column 15 of package:test_lib/test_lib.dart: Here it is
Expand All @@ -60,7 +60,7 @@ line 4, column 15 of package:test_lib/test_lib.dart: Here it is

test('should correctly highlight setter', () async {
expect(
spanForElement(library.getType('Example')!.getField('setter')!)
spanForElement(library.getClass('Example')!.getField('setter')!)
.message('Here it is'),
r"""
line 5, column 7 of package:test_lib/test_lib.dart: Here it is
Expand All @@ -73,7 +73,7 @@ line 5, column 7 of package:test_lib/test_lib.dart: Here it is

test('should correctly highlight field', () async {
expect(
spanForElement(library.getType('Example')!.getField('field')!)
spanForElement(library.getClass('Example')!.getField('field')!)
.message('Here it is'),
r"""
line 6, column 7 of package:test_lib/test_lib.dart: Here it is
Expand All @@ -86,7 +86,7 @@ line 6, column 7 of package:test_lib/test_lib.dart: Here it is

test('highlight getter with getter/setter property', () async {
expect(
spanForElement(library.getType('Example')!.getField('fieldProp')!)
spanForElement(library.getClass('Example')!.getField('fieldProp')!)
.message('Here it is'),
r"""
line 7, column 11 of package:test_lib/test_lib.dart: Here it is
Expand Down
Loading

0 comments on commit bd6f791

Please sign in to comment.