Skip to content

Commit

Permalink
Elements. Migrate CreateMethod.
Browse files Browse the repository at this point in the history
Change-Id: I7e631dd63baf74060a94bd413f5ccdd527fd21f1
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/387352
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Sep 30, 2024
1 parent 4038b97 commit 3c5bdb6
Show file tree
Hide file tree
Showing 12 changed files with 310 additions and 39 deletions.
1 change: 1 addition & 0 deletions pkg/analysis_server/analyzer_use_new_elements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ lib/src/services/correction/dart/create_field.dart
lib/src/services/correction/dart/create_function.dart
lib/src/services/correction/dart/create_getter.dart
lib/src/services/correction/dart/create_local_variable.dart
lib/src/services/correction/dart/create_method.dart
lib/src/services/correction/dart/create_method_or_function.dart
lib/src/services/correction/dart/create_no_such_method.dart
lib/src/services/correction/dart/create_parameter.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:analysis_server/src/services/correction/util.dart';
import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server/src/services/correction/util.dart';
import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
Expand Down Expand Up @@ -59,27 +60,31 @@ class CreateMethod extends ResolvedCorrectionProducer {
return;
}

var classElement = classDecl.declaredElement!;
var classElement = classDecl.declaredFragment!.element;
var missingEquals = memberDecl is FieldDeclaration ||
(memberDecl as MethodDeclaration).name.lexeme == 'hashCode';

await builder.addDartFileEdit(file, (fileBuilder) {
fileBuilder.insertIntoUnitMember(classDecl, (builder) {
ExecutableElement? element;
ExecutableElement2? element;
if (missingEquals) {
_memberName = '==';
element = classElement.lookUpInheritedMethod(
_memberName, classElement.library);
element = inheritanceManager.getInherited4(
classElement,
Name.forLibrary(classElement.library2, _memberName),
);
} else {
_memberName = 'hashCode';
element = classElement.lookUpInheritedConcreteGetter(
_memberName, classElement.library);
element = inheritanceManager.getInherited4(
classElement,
Name.forLibrary(classElement.library2, _memberName),
);
}
if (element == null) {
return;
}

builder.writeOverride(element, invokeSuper: true);
builder.writeOverride2(element, invokeSuper: true);
});
});
}
Expand All @@ -91,31 +96,30 @@ class CreateMethod extends ResolvedCorrectionProducer {
_memberName = (node as SimpleIdentifier).name;
var invocation = node.parent as MethodInvocation;
// Prepare environment.
Element? targetElement;
Fragment? targetFragment;
var staticModifier = false;

CompilationUnitMember? targetNode;
var target = invocation.realTarget;
if (target is ExtensionOverride) {
targetElement = target.element;
if (targetElement is ExtensionElement) {
targetNode = await getExtensionDeclaration(targetElement);
targetFragment = target.element2.firstFragment;
if (targetFragment is ExtensionFragment) {
targetNode = await getExtensionDeclaration2(targetFragment);
if (targetNode == null) {
return;
}
}
} else if (target is Identifier &&
target.staticElement is ExtensionElement) {
targetElement = target.staticElement;
if (targetElement is ExtensionElement) {
targetNode = await getExtensionDeclaration(targetElement);
} else if (target is Identifier && target.element is ExtensionElement2) {
targetFragment = (target.element as ExtensionElement2).firstFragment;
if (targetFragment is ExtensionFragment) {
targetNode = await getExtensionDeclaration2(targetFragment);
if (targetNode == null) {
return;
}
}
staticModifier = true;
} else if (target == null) {
targetElement = unit.declaredElement;
targetFragment = unit.declaredFragment;
var enclosingMember = node.thisOrAncestorOfType<ClassMember>();
if (enclosingMember == null) {
// If the undefined identifier isn't inside a class member, then it
Expand All @@ -128,40 +132,43 @@ class CreateMethod extends ResolvedCorrectionProducer {
staticModifier = inStaticContext;
}
} else {
var targetClassElement = getTargetInterfaceElement(target);
var targetClassElement = getTargetInterfaceElement2(target);
if (targetClassElement == null) {
return;
}
targetElement = targetClassElement;
if (targetClassElement.library.isInSdk) {
targetFragment = targetClassElement.firstFragment;
if (targetClassElement.library2.isInSdk) {
return;
}
// Prepare target ClassDeclaration.
if (targetClassElement is MixinElement) {
targetNode = await getMixinDeclaration(targetClassElement);
} else if (targetClassElement is ClassElement) {
targetNode = await getClassDeclaration(targetClassElement);
} else if (targetClassElement is ExtensionTypeElement) {
targetNode = await getExtensionTypeDeclaration(targetClassElement);
if (targetClassElement is MixinElement2) {
var fragment = targetClassElement.firstFragment;
targetNode = await getMixinDeclaration2(fragment);
} else if (targetClassElement is ClassElement2) {
var fragment = targetClassElement.firstFragment;
targetNode = await getClassDeclaration2(fragment);
} else if (targetClassElement is ExtensionTypeElement2) {
var fragment = targetClassElement.firstFragment;
targetNode = await getExtensionTypeDeclaration2(fragment);
}
if (targetNode == null) {
return;
}
// Maybe static.
if (target is Identifier) {
staticModifier = target.staticElement?.kind == ElementKind.CLASS ||
target.staticElement?.kind == ElementKind.EXTENSION_TYPE ||
target.staticElement?.kind == ElementKind.MIXIN;
staticModifier = target.element?.kind == ElementKind.CLASS ||
target.element?.kind == ElementKind.EXTENSION_TYPE ||
target.element?.kind == ElementKind.MIXIN;
}
// Use different utils.
var targetPath = targetClassElement.source.fullName;
var targetPath = targetFragment.libraryFragment.source.fullName;
var targetResolveResult =
await unitResult.session.getResolvedUnit(targetPath);
if (targetResolveResult is! ResolvedUnitResult) {
return;
}
}
var targetSource = targetElement?.source;
var targetSource = targetFragment?.libraryFragment.source;
if (targetSource == null) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:analysis_server/src/services/correction/util.dart';
import 'package:analysis_server/src/utilities/extensions/ast.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import 'package:analysis_server/src/services/correction/fix/data_driven/transfor
import 'package:analysis_server/src/services/correction/fix/data_driven/transform_set.dart';
import 'package:analysis_server/src/services/correction/fix/data_driven/transform_set_manager.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/element/element.dart'
show DirectiveUriWithRelativeUri;
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
Expand Down
24 changes: 24 additions & 0 deletions pkg/analysis_server_plugin/lib/edit/dart/correction_producer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,18 @@ abstract class ResolvedCorrectionProducer
return null;
}

/// Returns the extension declaration for the given [fragment], or `null` if
/// there is no such extension.
Future<ExtensionDeclaration?> getExtensionDeclaration2(
ExtensionFragment fragment) async {
var result = await sessionHelper.getElementDeclaration2(fragment);
var node = result?.node;
if (node is ExtensionDeclaration) {
return node;
}
return null;
}

/// Returns the extension type for the given [element], or `null` if there
/// is no such extension type.
Future<ExtensionTypeDeclaration?> getExtensionTypeDeclaration(
Expand All @@ -446,6 +458,18 @@ abstract class ResolvedCorrectionProducer
return null;
}

/// Returns the extension type for the given [fragment], or `null` if there
/// is no such extension type.
Future<ExtensionTypeDeclaration?> getExtensionTypeDeclaration2(
ExtensionTypeFragment fragment) async {
var result = await sessionHelper.getElementDeclaration2(fragment);
var node = result?.node;
if (node is ExtensionTypeDeclaration) {
return node;
}
return null;
}

/// Returns the mixin declaration for the given [element], or `null` if there
/// is no such mixin.
Future<MixinDeclaration?> getMixinDeclaration(MixinElement element) async {
Expand Down
17 changes: 16 additions & 1 deletion pkg/analyzer/lib/dart/element/element2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,16 @@ import 'package:analyzer/source/source.dart';
import 'package:analyzer/src/dart/resolver/scope.dart';
import 'package:pub_semver/pub_semver.dart';

export 'package:analyzer/dart/element/element.dart' show DirectiveUriWithSource;
export 'package:analyzer/dart/element/element.dart'
show
DirectiveUri,
DirectiveUriWithLibrary,
DirectiveUriWithRelativeUri,
DirectiveUriWithRelativeUriString,
DirectiveUriWithSource,
DirectiveUriWithUnit,
ElementAnnotation,
ElementKind;

abstract class BindPatternVariableElement2 implements PatternVariableElement2 {}

Expand Down Expand Up @@ -483,6 +492,9 @@ abstract class ExecutableFragment implements FunctionTypedFragment {
abstract class ExtensionElement2 implements InstanceElement2 {
/// The type that is extended by this extension.
DartType get extendedType;

@override
ExtensionFragment get firstFragment;
}

/// The portion of an [ExtensionElement2] contributed by a single
Expand All @@ -498,6 +510,9 @@ abstract class ExtensionFragment implements InstanceFragment {
///
/// Clients may not extend, implement or mix-in this class.
abstract class ExtensionTypeElement2 implements InterfaceElement2 {
@override
ExtensionTypeFragment get firstFragment;

/// The primary constructor of this extension.
ConstructorElement2 get primaryConstructor2;

Expand Down
6 changes: 6 additions & 0 deletions pkg/analyzer/lib/src/dart/element/element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6714,6 +6714,9 @@ mixin MaybeAugmentedExtensionElementMixin on MaybeAugmentedInstanceElementMixin
@override
ExtensionElementImpl get declaration;

@override
ExtensionElementImpl get firstFragment => declaration;

@override
DartType get thisType => extendedType;
}
Expand All @@ -6733,6 +6736,9 @@ mixin MaybeAugmentedExtensionTypeElementMixin
@override
ExtensionTypeElementImpl get declaration;

@override
ExtensionTypeElementImpl get firstFragment => declaration;

@override
ConstructorElement2 get primaryConstructor2 =>
representation as ConstructorElement2;
Expand Down
Loading

0 comments on commit 3c5bdb6

Please sign in to comment.