QuickFix. Issue 55804. Don't suggest creating local vriable when it is a name of property.

Bug: https://github.com/dart-lang/sdk/issues/55804
Change-Id: Ib61c025855464e1b7f01116234624d936ee9bc89
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/368701
Reviewed-by: Phil Quitslund <pquitslund@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
This commit is contained in:
Konstantin Shcheglov 2024-05-29 20:22:59 +00:00 committed by Commit Queue
parent 04d7213fe8
commit 28da71b42d
2 changed files with 67 additions and 5 deletions

View file

@ -44,6 +44,17 @@ class CreateLocalVariable extends ResolvedCorrectionProducer {
return;
}
}
// In `foo.bar`, `bar` is not a local variable.
// It also does not seem useful to suggest `foo`.
// So, always skip with these parents.
var parent = nameNode.parent;
switch (parent) {
case PrefixedIdentifier():
case PropertyAccess():
return;
}
// prepare target Statement
var target = node.thisOrAncestorOfType<Statement>();
if (target == null) {

View file

@ -95,13 +95,33 @@ void f() {
''');
}
@failingTest
Future<void> test_propertyAccess() async {
// We should not offer to define a local variable named 'g'.
Future<void> test_read_prefixedIdentifier_identifier() async {
await resolveTestCode('''
void f(String s) {
s.g;
void f(C c) {
c.test;
}
class C {}
''');
await assertNoFix();
}
Future<void> test_read_prefixedIdentifier_prefix() async {
await resolveTestCode('''
void f() {
test.foo;
}
''');
await assertNoFix();
}
Future<void> test_read_propertyAccess_propertyName() async {
await resolveTestCode('''
void f(C c) {
(c).test;
}
class C {}
''');
await assertNoFix();
}
@ -287,4 +307,35 @@ void f() {
}
''');
}
Future<void> test_write_prefixedIdentifier_identifier() async {
await resolveTestCode('''
void f(C c) {
c.test = 0;
}
class C {}
''');
await assertNoFix();
}
Future<void> test_write_prefixedIdentifier_prefix() async {
await resolveTestCode('''
void f() {
test.foo = 0;
}
''');
await assertNoFix();
}
Future<void> test_write_propertyAccess_propertyName() async {
await resolveTestCode('''
void f(C c) {
(c).test = 0;
}
class C {}
''');
await assertNoFix();
}
}