follow ups from https://dart-review.googlesource.com/c/sdk/+/296066
(The last change was prematurely merged.)
Change-Id: Idc161ef596fa43927cf7eda223635a798d3292af
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/296402
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/destructure_local_variable_assignment.dart b/pkg/analysis_server/lib/src/services/correction/dart/destructure_local_variable_assignment.dart
index 2935ed7..84638c1 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/destructure_local_variable_assignment.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/destructure_local_variable_assignment.dart
@@ -12,6 +12,7 @@
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
+import 'package:analyzer/src/utilities/extensions/map.dart';
import 'package:analyzer_plugin/utilities/assist/assist.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/range_factory.dart';
@@ -65,7 +66,7 @@
var references = propertyReference.value;
for (var reference in references) {
- if (reference.isAssignedTo) return;
+ if (reference.inSetterContext) return;
excludes.addAll(correctionUtils
.findPossibleLocalVariableConflicts(reference.offset));
}
@@ -250,12 +251,13 @@
void visitSimpleIdentifier(SimpleIdentifier node) {
if (node.staticElement == element) {
var parent = node.parent;
- if (parent is PrefixedIdentifier) {
- propertyReferences.update(
- parent.identifier.name, (nodes) => nodes..add(parent),
- ifAbsent: () => [parent]);
- } else {
- objectReferences.add(node);
+ switch (parent) {
+ case PrefixedIdentifier(:var identifier):
+ propertyReferences.add(identifier.name, parent);
+ case PropertyAccess(:var propertyName):
+ propertyReferences.add(propertyName.name, parent);
+ case _:
+ objectReferences.add(node);
}
}
super.visitSimpleIdentifier(node);
@@ -271,10 +273,10 @@
}
extension on AstNode {
- bool get isAssignedTo {
+ bool get inSetterContext {
var node = this;
- var assignment = node.thisOrAncestorOfType<AssignmentExpression>();
- if (assignment == null) return false;
- return assignment.leftHandSide == node;
+ if (node is PrefixedIdentifier) node = node.identifier;
+ if (node is PropertyAccess) node = node.propertyName;
+ return (node is SimpleIdentifier) ? node.inSetterContext() : false;
}
}
diff --git a/pkg/analysis_server/test/src/services/correction/assist/destructure_local_variable_assignment_test.dart b/pkg/analysis_server/test/src/services/correction/assist/destructure_local_variable_assignment_test.dart
index 6645994..a0db2eb 100644
--- a/pkg/analysis_server/test/src/services/correction/assist/destructure_local_variable_assignment_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/assist/destructure_local_variable_assignment_test.dart
@@ -42,7 +42,7 @@
''');
}
- Future<void> test_object_propertyWritten_noAssist() async {
+ Future<void> test_object_propertyAssigned_noAssist() async {
await resolveTestCode('''
class A {
set a(int a) {}
@@ -58,6 +58,38 @@
await assertNoAssistAt('obj');
}
+ Future<void> test_object_propertyPostIncremented_noAssist() async {
+ await resolveTestCode('''
+class A {
+ int a = 1;
+}
+
+A f() => A();
+
+m() {
+ var obj = f();
+ obj.a++;
+}
+''');
+ await assertNoAssistAt('obj');
+ }
+
+ Future<void> test_object_propertyPreIncremented_noAssist() async {
+ await resolveTestCode('''
+class A {
+ int a = 1;
+}
+
+A f() => A();
+
+m() {
+ var obj = f();
+ ++obj.a;
+}
+''');
+ await assertNoAssistAt('obj');
+ }
+
Future<void> test_object_reassigned_noAssist() async {
await resolveTestCode('''
class A { }
diff --git a/pkg/analyzer/lib/src/utilities/extensions/map.dart b/pkg/analyzer/lib/src/utilities/extensions/map.dart
new file mode 100644
index 0000000..44447ae
--- /dev/null
+++ b/pkg/analyzer/lib/src/utilities/extensions/map.dart
@@ -0,0 +1,10 @@
+// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+// todo(pq): move to collections
+extension MapExtension<K, V> on Map<K, List<V>> {
+ void add(K key, V value) {
+ (this[key] ??= []).add(value);
+ }
+}