Skip to content

Commit f7d62a5

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Fixes rename FieldFormalParameter outside library
Fixes: #60182 Change-Id: Ic833f07f2158bedcd196e06a7feebc761109fdef Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/413380 Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 7382fe9 commit f7d62a5

File tree

4 files changed

+126
-3
lines changed

4 files changed

+126
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class RenameClassMemberRefactoringImpl extends RenameRefactoringImpl {
110110
for (var parameter in constructor.formalParameters) {
111111
if (parameter is FieldFormalParameterElement2 &&
112112
parameter.field2 == renameElement) {
113-
await workspace.searchEngine
113+
await searchEngine
114114
.searchReferences(parameter)
115115
.then(processor.addReferenceEdits);
116116
}

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,101 @@ class A<NewName> {
12391239
A(this.field);
12401240
NewName method(NewName p) => field;
12411241
}
1242+
''');
1243+
}
1244+
1245+
Future<void> test_subclass_namedSuper_otherLibrary() async {
1246+
await indexTestUnit('''
1247+
class Base {
1248+
final int foo;
1249+
1250+
Base({required this.foo});
1251+
}
1252+
''');
1253+
await indexUnit('$testPackageLibPath/sub1.dart', r'''
1254+
import 'test.dart';
1255+
1256+
class Sub1 extends Base {
1257+
Sub1({required super.foo});
1258+
}
1259+
''');
1260+
await indexUnit('$testPackageLibPath/sub2.dart', r'''
1261+
import 'test.dart';
1262+
1263+
class Sub2 extends Base {
1264+
@override
1265+
int get foo => 0;
1266+
1267+
Sub2({required super.foo});
1268+
}
1269+
''');
1270+
createRenameRefactoringAtString('foo;');
1271+
// check status
1272+
refactoring.newName = 'bar';
1273+
var status = await refactoring.checkFinalConditions();
1274+
assertRefactoringStatusOK(status);
1275+
refactoringChange = await refactoring.createChange();
1276+
assertFileChangeResult('$testPackageLibPath/sub2.dart', '''
1277+
import 'test.dart';
1278+
1279+
class Sub2 extends Base {
1280+
@override
1281+
int get bar => 0;
1282+
1283+
Sub2({required super.bar});
1284+
}
1285+
''');
1286+
assertFileChangeResult('$testPackageLibPath/sub1.dart', '''
1287+
import 'test.dart';
1288+
1289+
class Sub1 extends Base {
1290+
Sub1({required super.bar});
1291+
}
1292+
''');
1293+
}
1294+
1295+
Future<void> test_subclass_namedSuper_sameLibrary() async {
1296+
await indexTestUnit('''
1297+
class Base {
1298+
final int foo;
1299+
1300+
Base({required this.foo});
1301+
}
1302+
1303+
class Sub1 extends Base {
1304+
Sub1({required super.foo});
1305+
}
1306+
1307+
class Sub2 extends Base {
1308+
@override
1309+
int get foo => 0;
1310+
1311+
Sub2({required super.foo});
1312+
}
1313+
''');
1314+
createRenameRefactoringAtString('foo;');
1315+
// check status
1316+
refactoring.newName = 'bar';
1317+
var status = await refactoring.checkFinalConditions();
1318+
assertRefactoringStatusOK(status);
1319+
refactoringChange = await refactoring.createChange();
1320+
assertTestChangeResult('''
1321+
class Base {
1322+
final int bar;
1323+
1324+
Base({required this.bar});
1325+
}
1326+
1327+
class Sub1 extends Base {
1328+
Sub1({required super.bar});
1329+
}
1330+
1331+
class Sub2 extends Base {
1332+
@override
1333+
int get bar => 0;
1334+
1335+
Sub2({required super.bar});
1336+
}
12421337
''');
12431338
}
12441339
}

pkg/analyzer/lib/src/dart/analysis/search.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,12 @@ class Search {
587587
{List<FileState>? filesToCheck}) async {
588588
// Prepare the element name.
589589
String name = element.displayName;
590-
if (element is ConstructorElement2) {
591-
name = element.enclosingElement2.displayName;
590+
var externalElement = element;
591+
if (externalElement case FormalParameterElement(:var enclosingElement2?)) {
592+
externalElement = enclosingElement2;
593+
}
594+
if (externalElement is ConstructorElement2) {
595+
name = externalElement.enclosingElement2.displayName;
592596
}
593597

594598
var elementPath = element.firstFragment.libraryFragment!.source.fullName;

pkg/analyzer/test/src/dart/analysis/search_test.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,30 @@ enum E {
13111311
''');
13121312
}
13131313

1314+
test_searchReferences_constructorField_outsideFile() async {
1315+
// Create an external file with a class that has a constructor field.
1316+
newFile('$testPackageLibPath/other.dart', r'''
1317+
import 'test.dart';
1318+
1319+
class B extends A {
1320+
B({super.x});
1321+
}
1322+
''');
1323+
// Resolve test code that imports the external file and references the field.
1324+
await resolveTestCode(r'''
1325+
class A {
1326+
int? x;
1327+
A({this.x});
1328+
}
1329+
''');
1330+
// Look up field 'x' and assert that its reference is correctly found.
1331+
var field = findElement2.fieldFormalParameter('x');
1332+
await assertElementReferencesText(field, r'''
1333+
package:test/other.dart::<fragment>::@class::B::@constructor::new::@parameter::x
1334+
52 4:12 |x| REFERENCE qualified
1335+
''');
1336+
}
1337+
13141338
test_searchReferences_ExtensionElement() async {
13151339
await resolveTestCode('''
13161340
extension E on int {

0 commit comments

Comments
 (0)