Skip to content

Commit 83a16ea

Browse files
FMorschelCommit Queue
authored and
Commit Queue
committed
[DAS] Fixes renaming fields at constructor parameters
Fixes: #52305 Change-Id: I39a547c391ffd3538b3ccaafeab3af65bb4088b0 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/413340 Reviewed-by: Samuel Rawlins <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Phil Quitslund <[email protected]>
1 parent 49ad066 commit 83a16ea

File tree

3 files changed

+139
-23
lines changed

3 files changed

+139
-23
lines changed

pkg/analysis_server/lib/src/services/refactoring/legacy/refactoring.dart

+11
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,17 @@ abstract class RenameRefactoring implements Refactoring {
495495
if (element is LibraryElement2) {
496496
return RenameLibraryRefactoringImpl(workspace, sessionHelper, element);
497497
}
498+
if (enclosingElement?.thisOrAncestorOfType2<InterfaceElement2>()
499+
case var enclosingElement?) {
500+
if (element case FieldFormalParameterElement2(:var field2?)) {
501+
return RenameClassMemberRefactoringImpl(
502+
workspace,
503+
sessionHelper,
504+
enclosingElement,
505+
field2,
506+
);
507+
}
508+
}
498509
if (element is FormalParameterElement) {
499510
return RenameParameterRefactoringImpl(workspace, sessionHelper, element);
500511
}

pkg/analysis_server/test/edit/refactoring_test.dart

-23
Original file line numberDiff line numberDiff line change
@@ -2036,29 +2036,6 @@ void f() {
20362036
);
20372037
}
20382038

2039-
Future<void> test_classMember_field_onFieldFormalParameter_named_private() {
2040-
addTestFile('''
2041-
class A {
2042-
final int test;
2043-
A({this.test = 0});
2044-
}
2045-
void f() {
2046-
A(test: 42);
2047-
}
2048-
''');
2049-
2050-
return getRefactoringResult(() {
2051-
return sendRenameRequest('test: 42', '_new');
2052-
}).then((result) {
2053-
var problems = result.finalProblems;
2054-
expect(problems, hasLength(1));
2055-
assertResultProblemsError(
2056-
problems,
2057-
"The parameter 'test' is named and can not be private.",
2058-
);
2059-
});
2060-
}
2061-
20622039
Future<void> test_classMember_getter() {
20632040
addTestFile('''
20642041
class A {

pkg/analysis_server/test/services/refactoring/legacy/rename_class_member_test.dart

+128
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,134 @@ void main() {
1919

2020
@reflectiveTest
2121
class RenameClassMemberClassTest extends RenameRefactoringTest {
22+
Future<void> test_atConstructor_named() async {
23+
await indexTestUnit('''
24+
class A {
25+
final int foo;
26+
27+
A({this.foo = 0});
28+
}
29+
''');
30+
createRenameRefactoringAtString('foo = 0');
31+
// check status
32+
refactoring.newName = 'bar';
33+
var status = await refactoring.checkFinalConditions();
34+
assertRefactoringStatusOK(status);
35+
}
36+
37+
Future<void> test_atConstructor_named_subclasses() async {
38+
await indexTestUnit('''
39+
class A {
40+
final int foo;
41+
42+
A({this.foo = 0});
43+
}
44+
45+
class B extends A {
46+
B({super.foo});
47+
}
48+
49+
class C extends A {
50+
C(int foo) : super(foo: foo);
51+
}
52+
''');
53+
createRenameRefactoringAtString('foo = 0');
54+
// check status
55+
refactoring.newName = 'bar';
56+
var status = await refactoring.checkFinalConditions();
57+
assertRefactoringStatusOK(status);
58+
await assertSuccessfulRefactoring('''
59+
class A {
60+
final int bar;
61+
62+
A({this.bar = 0});
63+
}
64+
65+
class B extends A {
66+
B({super.bar});
67+
}
68+
69+
class C extends A {
70+
C(int foo) : super(bar: foo);
71+
}
72+
''');
73+
}
74+
75+
Future<void> test_atConstructor_named_subclasses_toPrivate() async {
76+
await indexTestUnit('''
77+
class A {
78+
final int foo;
79+
80+
A({this.foo = 0});
81+
}
82+
83+
class B extends A {
84+
B({super.foo});
85+
}
86+
''');
87+
createRenameRefactoringAtString('foo = 0');
88+
// check status
89+
refactoring.newName = '_foo';
90+
var status = await refactoring.checkFinalConditions();
91+
assertRefactoringStatusOK(status);
92+
await assertSuccessfulRefactoring('''
93+
class A {
94+
final int _foo;
95+
96+
A({int foo = 0}) : _foo = foo;
97+
}
98+
99+
class B extends A {
100+
B({super.foo});
101+
}
102+
''');
103+
}
104+
105+
Future<void> test_atConstructor_positional() async {
106+
await indexTestUnit('''
107+
class A {
108+
int foo = 0;
109+
110+
A(this.foo);
111+
}
112+
''');
113+
createRenameRefactoringAtString('foo);');
114+
// check status
115+
refactoring.newName = 'bar';
116+
var status = await refactoring.checkFinalConditions();
117+
assertRefactoringStatusOK(status);
118+
}
119+
120+
Future<void> test_atConstructor_positional_subclasses_toPrivate() async {
121+
await indexTestUnit('''
122+
class A {
123+
int foo = 0;
124+
125+
A(this.foo); // marker
126+
}
127+
128+
class B extends A {
129+
B(super.foo);
130+
}
131+
''');
132+
createRenameRefactoringAtString('foo); // marker');
133+
// check status
134+
refactoring.newName = '_foo';
135+
var status = await refactoring.checkFinalConditions();
136+
assertRefactoringStatusOK(status);
137+
await assertSuccessfulRefactoring('''
138+
class A {
139+
int _foo = 0;
140+
141+
A(this._foo); // marker
142+
}
143+
144+
class B extends A {
145+
B(super.foo);
146+
}
147+
''');
148+
}
149+
22150
Future<void> test_checkFinalConditions_classNameConflict_sameClass() async {
23151
await indexTestUnit('''
24152
class NewName {

0 commit comments

Comments
 (0)