Skip to content
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

Element2 implementation revive.dart. #749

Merged
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
48 changes: 23 additions & 25 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// ignore_for_file: deprecated_member_use

import 'package:analyzer/dart/constant/value.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/constant/value.dart' show DartObjectImpl;
Expand All @@ -22,39 +22,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.
@Deprecated('use reviveInstance2 instead')
Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
Revivable reviveInstance(DartObject object, [LibraryElement2? origin]) {
final objectType = object.type;
Element? element = objectType!.alias?.element;
Element2? element = objectType!.alias?.element2;
if (element == null) {
if (objectType is InterfaceType) {
element = objectType.element;
element = objectType.element3;
} else {
element = object.toFunctionValue();
element = object.toFunctionValue2();
}
}
origin ??= element!.library;
var url = Uri.parse(urlOfElement(element!));
if (element is FunctionElement) {
return Revivable._(
source: url.removeFragment(),
accessor: element.name,
);
origin ??= element!.library2;
var url = Uri.parse(urlOfElement2(element!));
if (element is TopLevelFunctionElement || element is LocalFunctionElement) {
return Revivable._(source: url.removeFragment(), accessor: element.name3!);
}
if (element is MethodElement && element.isStatic) {

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

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

for (final type in origin!.definingCompilationUnit.classes) {
for (final e in type.fields
for (final type in origin!.classes) {
for (final e in type.fields2
.where((f) => f.isConst && f.computeConstantValue() == object)) {
final result = Revivable._(
source: url.removeFragment(),
accessor: '${type.name}.${e.name}',
accessor: '${type.name3}.${e.name3}',
);
if (tryResult(result)) {
return result;
Expand All @@ -82,7 +80,7 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
}
final i = (object as DartObjectImpl).getInvocation();
if (i != null) {
url = Uri.parse(urlOfElement(i.constructor.enclosingElement3));
url = Uri.parse(urlOfElement2(i.constructor2.enclosingElement2));
final result = Revivable._(
source: url,
accessor: i.constructor.name,
Expand All @@ -93,12 +91,12 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
return result;
}
}
for (final e in origin.definingCompilationUnit.topLevelVariables.where(
for (final e in origin.topLevelVariables.where(
(f) => f.isConst && f.computeConstantValue() == object,
)) {
final result = Revivable._(
source: Uri.parse(urlOfElement(origin)).replace(fragment: ''),
accessor: e.name,
source: Uri.parse(urlOfElement2(origin)).replace(fragment: ''),
accessor: e.name3!,
);
if (tryResult(result)) {
return result;
Expand Down