Skip to content

Commit fcf79e0

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Adds working rename when shadowing global variable is assigned
[email protected] Bug: #59954 Change-Id: I39417d9e712a40b6992fbad86180c257e782e04a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/414200 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Phil Quitslund <[email protected]> Auto-Submit: Felipe Morschel <[email protected]>
1 parent a098bf3 commit fcf79e0

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

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

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -361,10 +361,23 @@ class _LocalElementsCollector extends GeneralizingAstVisitor<void> {
361361
@override
362362
void visitSimpleIdentifier(SimpleIdentifier node) {
363363
var element = node.element;
364-
if (element is GetterElement && element.name3 == name) {
365-
if (element.variable3 case TopLevelVariableElement2 variable) {
366-
elements.add(variable);
367-
}
364+
if (node.parent case AssignmentExpression(
365+
:var writeElement2,
366+
:var leftHandSide,
367+
) when node == leftHandSide) {
368+
element = writeElement2;
369+
}
370+
if (element is! PropertyAccessorElement2) {
371+
return;
372+
}
373+
if (element.name3 != name) {
374+
return;
375+
}
376+
if (element is! GetterElement && element is! SetterElement) {
377+
return;
378+
}
379+
if (element.variable3 case TopLevelVariableElement2 variable) {
380+
elements.add(variable);
368381
}
369382
super.visitSimpleIdentifier(node);
370383
}

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

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ class A {
14321432
''');
14331433
}
14341434

1435-
Future<void> test_shadowingTopLevelVariable_addsThis() async {
1435+
Future<void> test_shadowingTopLevelVariableGetter_addsThis() async {
14361436
await indexTestUnit('''
14371437
int? value = 0;
14381438
@@ -1464,6 +1464,38 @@ class A {
14641464
''');
14651465
}
14661466

1467+
Future<void> test_shadowingTopLevelVariableSetter_addsThis() async {
1468+
await indexTestUnit('''
1469+
int? value = 0;
1470+
1471+
class A {
1472+
final int? _value;
1473+
1474+
const A(this._value);
1475+
1476+
void m() {
1477+
value = _value ?? 0;
1478+
}
1479+
}
1480+
''');
1481+
createRenameRefactoringAtString('_value;');
1482+
// check status
1483+
refactoring.newName = 'value';
1484+
await assertSuccessfulRefactoring('''
1485+
int? value = 0;
1486+
1487+
class A {
1488+
final int? value;
1489+
1490+
const A(this.value);
1491+
1492+
void m() {
1493+
value = this.value ?? 0;
1494+
}
1495+
}
1496+
''');
1497+
}
1498+
14671499
Future<void> test_subclass_namedSuper_otherLibrary() async {
14681500
await indexTestUnit('''
14691501
class Base {

0 commit comments

Comments
 (0)