Skip to content

Commit 3029a69

Browse files
scheglovCommit Queue
authored andcommitted
Elements. Migrate RenameMethodParameter.
Change-Id: I2ce533dd2f9f6301ca089ddcc93d38e6de37690e Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/389621 Reviewed-by: Phil Quitslund <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 474c9fe commit 3029a69

File tree

3 files changed

+47
-38
lines changed

3 files changed

+47
-38
lines changed

pkg/analysis_server/analyzer_use_new_elements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ lib/src/services/correction/dart/remove_unused_local_variable.dart
423423
lib/src/services/correction/dart/remove_unused_parameter.dart
424424
lib/src/services/correction/dart/remove_var.dart
425425
lib/src/services/correction/dart/remove_var_keyword.dart
426+
lib/src/services/correction/dart/rename_method_parameter.dart
426427
lib/src/services/correction/dart/rename_to_camel_case.dart
427428
lib/src/services/correction/dart/replace_boolean_with_bool.dart
428429
lib/src/services/correction/dart/replace_cascade_with_dot.dart

pkg/analysis_server/lib/src/services/correction/dart/rename_method_parameter.dart

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
import 'package:analysis_server/src/services/correction/fix.dart';
66
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
7-
import 'package:analyzer/dart/ast/ast.dart';
87
import 'package:analyzer/dart/ast/token.dart';
98
import 'package:analyzer/dart/ast/visitor.dart';
10-
import 'package:analyzer/dart/element/element.dart';
9+
import 'package:analyzer/dart/element/element2.dart';
10+
import 'package:analyzer/src/dart/ast/ast.dart';
11+
import 'package:analyzer/src/dart/element/inheritance_manager3.dart';
1112
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
1213
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';
1314
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -38,26 +39,33 @@ class RenameMethodParameter extends ResolvedCorrectionProducer {
3839

3940
var method = parameter.thisOrAncestorOfType<MethodDeclaration>();
4041
if (method == null) return;
42+
4143
var methodParameters = method.parameters;
4244
if (methodParameters == null) return;
4345

44-
var classDeclaration = method.parent as Declaration;
45-
var classElement = classDeclaration.declaredElement;
46-
if (classElement is! InterfaceElement) return;
46+
Fragment? declaredFragment;
47+
if (method.parent case FragmentDeclaration declaration) {
48+
declaredFragment = declaration.declaredFragment;
49+
}
50+
51+
var classElement = declaredFragment?.element;
52+
if (classElement is! InterfaceElement2) return;
4753

48-
var parentMethod = classElement.lookUpInheritedMethod(
49-
method.name.lexeme, classElement.library);
54+
var parentMethod = inheritanceManager.getInherited4(
55+
classElement,
56+
Name.forLibrary(libraryElement2, method.name.lexeme),
57+
);
5058
if (parentMethod == null) return;
5159

5260
var parameters = methodParameters.parameters;
53-
var parentParameters = parentMethod.parameters;
61+
var parentParameters = parentMethod.formalParameters;
5462
var oldName = paramIdentifier.lexeme;
5563

5664
var i = parameters.indexOf(parameter);
5765
if (0 <= i && i < parentParameters.length) {
5866
var newName = parentParameters[i].name;
5967

60-
var collector = _Collector(newName, parameter.declaredElement!);
68+
var collector = _Collector(newName, parameter.declaredFragment!.element);
6169
method.accept(collector);
6270

6371
if (!collector.error) {
@@ -77,30 +85,30 @@ class RenameMethodParameter extends ResolvedCorrectionProducer {
7785
class _Collector extends RecursiveAstVisitor<void> {
7886
bool error = false;
7987
final String newName;
80-
final ParameterElement target;
88+
final FormalParameterElement target;
8189

8290
final oldTokens = <Token>[];
8391

8492
_Collector(this.newName, this.target);
8593

8694
@override
8795
void visitSimpleFormalParameter(SimpleFormalParameter node) {
88-
_addNameToken(node.name, node.declaredElement);
96+
_addNameToken(node.name, node.declaredFragment?.element);
8997
super.visitSimpleFormalParameter(node);
9098
}
9199

92100
@override
93101
void visitSimpleIdentifier(SimpleIdentifier node) {
94-
_addNameToken(node.token, node.staticElement);
102+
_addNameToken(node.token, node.element);
95103
}
96104

97105
@override
98106
void visitVariableDeclaration(VariableDeclaration node) {
99-
_addNameToken(node.name, node.declaredElement);
107+
_addNameToken(node.name, node.declaredFragment?.element);
100108
super.visitVariableDeclaration(node);
101109
}
102110

103-
void _addNameToken(Token? nameToken, Element? element) {
111+
void _addNameToken(Token? nameToken, Element2? element) {
104112
if (error) return;
105113

106114
if (nameToken != null) {

pkg/analyzer/lib/src/dart/ast/ast.dart

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,7 +2731,7 @@ class ChildEntity {
27312731
/// | 'abstract'? ('base' | 'interface' | 'final')?
27322732
/// | 'abstract'? 'base'? 'mixin'
27332733
abstract final class ClassDeclaration
2734-
implements NamedCompilationUnitMember, _FragmentDeclaration {
2734+
implements NamedCompilationUnitMember, FragmentDeclaration {
27352735
/// The `abstract` keyword, or `null` if the keyword was absent.
27362736
Token? get abstractKeyword;
27372737

@@ -2967,7 +2967,7 @@ sealed class ClassMemberImpl extends DeclarationImpl implements ClassMember {
29672967
///
29682968
/// mixinApplication ::=
29692969
/// [NamedType] [WithClause] [ImplementsClause]? ';'
2970-
abstract final class ClassTypeAlias implements TypeAlias, _FragmentDeclaration {
2970+
abstract final class ClassTypeAlias implements TypeAlias, FragmentDeclaration {
29712971
/// The token for the `abstract` keyword, or `null` if this isn't defining an
29722972
/// abstract class.
29732973
Token? get abstractKeyword;
@@ -4128,7 +4128,7 @@ final class ConstantPatternImpl extends DartPatternImpl
41284128
/// initializerList ::=
41294129
/// ':' [ConstructorInitializer] (',' [ConstructorInitializer])*
41304130
abstract final class ConstructorDeclaration
4131-
implements ClassMember, _FragmentDeclaration {
4131+
implements ClassMember, FragmentDeclaration {
41324132
/// The `augment` keyword, or `null` if the keyword was absent.
41334133
Token? get augmentKeyword;
41344134

@@ -5604,7 +5604,7 @@ final class EnumConstantArgumentsImpl extends AstNodeImpl
56045604
}
56055605

56065606
/// The declaration of an enum constant.
5607-
abstract final class EnumConstantDeclaration implements _FragmentDeclaration {
5607+
abstract final class EnumConstantDeclaration implements FragmentDeclaration {
56085608
/// The explicit arguments (there are always implicit `index` and `name`
56095609
/// leading arguments) to the invoked constructor, or `null` if this constant
56105610
/// doesn't provide any explicit arguments.
@@ -5707,7 +5707,7 @@ final class EnumConstantDeclarationImpl extends DeclarationImpl
57075707
/// [WithClause]? [ImplementsClause]? '{' [SimpleIdentifier]
57085708
/// (',' [SimpleIdentifier])* (';' [ClassMember]+)? '}'
57095709
abstract final class EnumDeclaration
5710-
implements NamedCompilationUnitMember, _FragmentDeclaration {
5710+
implements NamedCompilationUnitMember, FragmentDeclaration {
57115711
/// The `augment` keyword, or `null` if the keyword was absent.
57125712
@experimental
57135713
Token? get augmentKeyword;
@@ -6427,7 +6427,7 @@ final class ExtendsClauseImpl extends AstNodeImpl implements ExtendsClause {
64276427
/// 'on' [TypeAnnotation] [ShowClause]? [HideClause]?
64286428
/// '{' [ClassMember]* '}'
64296429
abstract final class ExtensionDeclaration
6430-
implements CompilationUnitMember, _FragmentDeclaration {
6430+
implements CompilationUnitMember, FragmentDeclaration {
64316431
/// The `augment` keyword, or `null` if the keyword was absent.
64326432
@experimental
64336433
Token? get augmentKeyword;
@@ -6774,7 +6774,7 @@ final class ExtensionOverrideImpl extends ExpressionImpl
67746774
/// '}'
67756775
@experimental
67766776
abstract final class ExtensionTypeDeclaration
6777-
implements NamedCompilationUnitMember, _FragmentDeclaration {
6777+
implements NamedCompilationUnitMember, FragmentDeclaration {
67786778
/// The `augment` keyword, or `null` if the keyword was absent.
67796779
@experimental
67806780
Token? get augmentKeyword;
@@ -8157,6 +8157,16 @@ final class ForStatementImpl extends StatementImpl
81578157
}
81588158
}
81598159

8160+
/// A declaration of a fragment of an element.
8161+
@experimental
8162+
abstract final class FragmentDeclaration implements Declaration {
8163+
/// The fragment declared by this declaration.
8164+
///
8165+
/// Returns `null` if the AST structure hasn't been resolved.
8166+
@experimental
8167+
Fragment? get declaredFragment;
8168+
}
8169+
81608170
/// A node representing the body of a function or method.
81618171
///
81628172
/// functionBody ::=
@@ -8274,7 +8284,7 @@ sealed class FunctionBodyImpl extends AstNodeImpl implements FunctionBody {
82748284
// that are only sometimes applicable. Consider changing the class hierarchy so
82758285
// that these two kinds of variables can be distinguished.
82768286
abstract final class FunctionDeclaration
8277-
implements NamedCompilationUnitMember, _FragmentDeclaration {
8287+
implements NamedCompilationUnitMember, FragmentDeclaration {
82788288
/// The `augment` keyword, or `null` if there is no `augment` keyword.
82798289
@experimental
82808290
Token? get augmentKeyword;
@@ -8821,7 +8831,7 @@ final class FunctionReferenceImpl extends CommentReferableExpressionImpl
88218831
/// functionPrefix ::=
88228832
/// [TypeAnnotation]? [SimpleIdentifier]
88238833
abstract final class FunctionTypeAlias
8824-
implements TypeAlias, _FragmentDeclaration {
8834+
implements TypeAlias, FragmentDeclaration {
88258835
@override
88268836
TypeAliasElement? get declaredElement;
88278837

@@ -9183,7 +9193,7 @@ final class GenericFunctionTypeImpl extends TypeAnnotationImpl
91839193
/// 'typedef' [SimpleIdentifier] [TypeParameterList]? =
91849194
/// [FunctionType] ';'
91859195
abstract final class GenericTypeAlias
9186-
implements TypeAlias, _FragmentDeclaration {
9196+
implements TypeAlias, FragmentDeclaration {
91879197
/// The equal sign separating the name being defined from the function type.
91889198
Token get equals;
91899199

@@ -11891,7 +11901,7 @@ final class MapPatternImpl extends DartPatternImpl implements MapPattern {
1189111901
/// children of a class declaration. When the experiment is enabled, these nodes
1189211902
/// can also be children of an extension declaration.
1189311903
abstract final class MethodDeclaration
11894-
implements ClassMember, _FragmentDeclaration {
11904+
implements ClassMember, FragmentDeclaration {
1189511905
/// The token for the `augment` keyword.
1189611906
Token? get augmentKeyword;
1189711907

@@ -12288,7 +12298,7 @@ abstract final class MethodReferenceExpression implements Expression {
1228812298
/// 'base'? 'mixin' name [TypeParameterList]?
1228912299
/// [OnClause]? [ImplementsClause]? '{' [ClassMember]* '}'
1229012300
abstract final class MixinDeclaration
12291-
implements NamedCompilationUnitMember, _FragmentDeclaration {
12301+
implements NamedCompilationUnitMember, FragmentDeclaration {
1229212302
/// The `augment` keyword, or `null` if the keyword was absent.
1229312303
Token? get augmentKeyword;
1229412304

@@ -18157,8 +18167,7 @@ final class TypeLiteralImpl extends CommentReferableExpressionImpl
1815718167
///
1815818168
/// typeParameter ::=
1815918169
/// name ('extends' [TypeAnnotation])?
18160-
abstract final class TypeParameter
18161-
implements Declaration, _FragmentDeclaration {
18170+
abstract final class TypeParameter implements Declaration, FragmentDeclaration {
1816218171
/// The upper bound for legal arguments, or `null` if there's no explicit
1816318172
/// upper bound.
1816418173
TypeAnnotation? get bound;
@@ -18409,7 +18418,7 @@ class UriValidationCode {
1840918418
// that are only sometimes applicable. Consider changing the class hierarchy so
1841018419
// that these two kinds of variables can be distinguished.
1841118420
abstract final class VariableDeclaration
18412-
implements Declaration, _FragmentDeclaration {
18421+
implements Declaration, FragmentDeclaration {
1841318422
/// The element declared by this declaration.
1841418423
///
1841518424
/// Returns `null` if the AST structure hasn't been resolved or if this node
@@ -19216,15 +19225,6 @@ base mixin _AnnotatedNodeMixin on AstNodeImpl implements AnnotatedNode {
1921619225
}
1921719226
}
1921819227

19219-
/// A declaration of a fragment of an element.
19220-
abstract final class _FragmentDeclaration implements Declaration {
19221-
/// The fragment declared by this declaration.
19222-
///
19223-
/// Returns `null` if the AST structure hasn't been resolved.
19224-
@experimental
19225-
Fragment? get declaredFragment;
19226-
}
19227-
1922819228
/// An indication of the resolved kind of a [SetOrMapLiteral].
1922919229
enum _SetOrMapKind {
1923019230
/// Indicates that the literal represents a map.

0 commit comments

Comments
 (0)