diff --git a/pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart b/pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart index 470ba446563..74000de5a45 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/create_local_variable.dart @@ -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(); if (target == null) { diff --git a/pkg/analysis_server/test/src/services/correction/fix/create_local_variable_test.dart b/pkg/analysis_server/test/src/services/correction/fix/create_local_variable_test.dart index fa3427a266c..ac3361d8761 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/create_local_variable_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/create_local_variable_test.dart @@ -95,13 +95,33 @@ void f() { '''); } - @failingTest - Future test_propertyAccess() async { - // We should not offer to define a local variable named 'g'. + Future test_read_prefixedIdentifier_identifier() async { await resolveTestCode(''' -void f(String s) { - s.g; +void f(C c) { + c.test; } + +class C {} +'''); + await assertNoFix(); + } + + Future test_read_prefixedIdentifier_prefix() async { + await resolveTestCode(''' +void f() { + test.foo; +} +'''); + await assertNoFix(); + } + + Future test_read_propertyAccess_propertyName() async { + await resolveTestCode(''' +void f(C c) { + (c).test; +} + +class C {} '''); await assertNoFix(); } @@ -287,4 +307,35 @@ void f() { } '''); } + + Future test_write_prefixedIdentifier_identifier() async { + await resolveTestCode(''' +void f(C c) { + c.test = 0; +} + +class C {} +'''); + await assertNoFix(); + } + + Future test_write_prefixedIdentifier_prefix() async { + await resolveTestCode(''' +void f() { + test.foo = 0; +} +'''); + await assertNoFix(); + } + + Future test_write_propertyAccess_propertyName() async { + await resolveTestCode(''' +void f(C c) { + (c).test = 0; +} + +class C {} +'''); + await assertNoFix(); + } }