Skip to content

Commit

Permalink
Elements. Migrate EncapsulateField.
Browse files Browse the repository at this point in the history
Change-Id: I505623f21211f1e529962b563f1898919f080537
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/387401
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
  • Loading branch information
scheglov authored and Commit Queue committed Sep 30, 2024
1 parent 3920bba commit 8a77fa0
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 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 @@ -285,6 +285,7 @@ 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
lib/src/services/correction/dart/data_driven.dart
lib/src/services/correction/dart/encapsulate_field.dart
lib/src/services/correction/dart/exchange_operands.dart
lib/src/services/correction/dart/extend_class_for_mixin.dart
lib/src/services/correction/dart/flutter_move_down.dart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import 'package:analysis_server/src/services/correction/assist.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/src/dart/element/inheritance_manager3.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
Expand Down Expand Up @@ -50,7 +50,8 @@ class EncapsulateField extends ResolvedCorrectionProducer {
}
var field = fields.first;
var nameToken = field.name;
var fieldElement = field.declaredElement as FieldElement;
var fieldFragment = field.declaredFragment as FieldFragment;
var fieldElement = fieldFragment.element;
// should have a public name
var name = nameToken.lexeme;
if (Identifier.isPrivateName(name)) {
Expand All @@ -63,15 +64,15 @@ class EncapsulateField extends ResolvedCorrectionProducer {

// Should be in a class or mixin.
List<ClassMember> classMembers;
InterfaceElement parentElement;
InterfaceElement2 parentElement;
var parent = fieldDeclaration.parent;
switch (parent) {
case ClassDeclaration():
classMembers = parent.members;
parentElement = parent.declaredElement!;
parentElement = parent.declaredFragment!.element;
case MixinDeclaration():
classMembers = parent.members;
parentElement = parent.declaredElement!;
parentElement = parent.declaredFragment!.element;
default:
return;
}
Expand Down Expand Up @@ -129,15 +130,15 @@ class EncapsulateField extends ResolvedCorrectionProducer {
}

// Write getter.
var overriddenGetters = inheritanceManager.getOverridden2(
var overriddenGetters = inheritanceManager.getOverridden4(
parentElement,
Name(null, name),
);
writeHeader(overriddenGetters != null);
builder.write(' ${typeCode}get $name => _$name;');

// Write setter.
var overriddenSetters = inheritanceManager.getOverridden2(
var overriddenSetters = inheritanceManager.getOverridden4(
parentElement,
Name(null, '$name='),
);
Expand All @@ -152,15 +153,15 @@ class EncapsulateField extends ResolvedCorrectionProducer {
void _updateReferencesInConstructor(
DartFileEditBuilder builder,
ConstructorDeclaration constructor,
FieldElement fieldElement,
FieldElement2 fieldElement,
String name,
) {
for (var parameter in constructor.parameters.parameters) {
var identifier = parameter.name;
var parameterElement = parameter.declaredElement;
var parameterElement = parameter.declaredFragment?.element;
if (identifier != null &&
parameterElement is FieldFormalParameterElement &&
parameterElement.field == fieldElement) {
parameterElement is FieldFormalParameterElement2 &&
parameterElement.field2 == fieldElement) {
if (parameter.isNamed && parameter is DefaultFormalParameter) {
var normalParam = parameter.parameter;
if (normalParam is FieldFormalParameter) {
Expand All @@ -182,7 +183,7 @@ class EncapsulateField extends ResolvedCorrectionProducer {
}
for (var initializer in constructor.initializers) {
if (initializer is ConstructorFieldInitializer &&
initializer.fieldName.staticElement == fieldElement) {
initializer.fieldName.element == fieldElement) {
builder.addSimpleReplacement(
range.node(initializer.fieldName),
'_$name',
Expand All @@ -194,7 +195,7 @@ class EncapsulateField extends ResolvedCorrectionProducer {
void _updateReferencesInConstructors(
DartFileEditBuilder builder,
List<ClassMember> classMembers,
FieldElement fieldElement,
FieldElement2 fieldElement,
String name,
) {
for (var constructor in classMembers) {
Expand Down
13 changes: 13 additions & 0 deletions pkg/analyzer/lib/src/dart/element/inheritance_manager3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:analyzer/src/dart/element/member.dart';
import 'package:analyzer/src/dart/element/type_algebra.dart';
import 'package:analyzer/src/dart/element/type_system.dart';
import 'package:analyzer/src/utilities/extensions/collection.dart';
import 'package:analyzer/src/utilities/extensions/element.dart';
import 'package:meta/meta.dart';

/// Failure because of there is no most specific signature in [candidates].
Expand Down Expand Up @@ -410,6 +411,18 @@ class InheritanceManager3 {
return interface.overridden[name];
}

/// Return all members of mixins, superclasses, and interfaces that a member
/// with the given [name], defined in the [element], would override; or `null`
/// if no members would be overridden.
List<ExecutableElement2>? getOverridden4(
InterfaceElement2 element, Name name) {
var interface = getInterface2(element);
var fragments = interface.overridden[name];
return fragments
?.map((fragment) => fragment.asElement2 as ExecutableElement2)
.toList();
}

/// Remove interfaces for classes defined in specified libraries.
void removeOfLibraries(Set<Uri> uriSet) {
_interfaces.removeWhere((element, _) {
Expand Down

0 comments on commit 8a77fa0

Please sign in to comment.