Add tests and enable bulk fixes for invalid_null_aware_operator
Closes: https://github.com/dart-lang/sdk/issues/46013
Change-Id: If7fad2ba8f859897e08bb78046ce45bd57ad0289
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/200883
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
Reviewed-by: Phil Quitslund <pquitslund@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart
index a46a17e..7998030 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/replace_with_not_null_aware.dart
@@ -21,6 +21,9 @@
FixKind get fixKind => DartFixKind.REPLACE_WITH_NOT_NULL_AWARE;
@override
+ FixKind get multiFixKind => DartFixKind.REPLACE_WITH_NOT_NULL_AWARE_MULTI;
+
+ @override
Future<void> compute(ChangeBuilder builder) async {
var node = coveredNode;
if (node is MethodInvocation) {
diff --git a/pkg/analysis_server/lib/src/services/correction/fix.dart b/pkg/analysis_server/lib/src/services/correction/fix.dart
index 7bb4b16..365dc29 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix.dart
@@ -839,6 +839,10 @@
'dart.fix.replace.withNotNullAware',
DartFixKindPriority.DEFAULT,
"Replace with '{0}'");
+ static const REPLACE_WITH_NOT_NULL_AWARE_MULTI = FixKind(
+ 'dart.fix.replace.withNotNullAware.multi',
+ DartFixKindPriority.IN_FILE,
+ 'Replace with non-null-aware operator everywhere in file.');
static const REPLACE_WITH_NULL_AWARE = FixKind(
'dart.fix.replace.withNullAware',
DartFixKindPriority.DEFAULT,
diff --git a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
index 406dcd01..2ff9ef9 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -1690,10 +1690,22 @@
FixInfo.single([RemoveDeadIfNull.newInstance]),
],
StaticWarningCode.INVALID_NULL_AWARE_OPERATOR: [
- FixInfo.single([ReplaceWithNotNullAware.newInstance]),
+ FixInfo(
+ canBeAppliedToFile: true,
+ canBeBulkApplied: true,
+ generators: [
+ ReplaceWithNotNullAware.newInstance,
+ ],
+ ),
],
StaticWarningCode.INVALID_NULL_AWARE_OPERATOR_AFTER_SHORT_CIRCUIT: [
- FixInfo.single([ReplaceWithNotNullAware.newInstance]),
+ FixInfo(
+ canBeAppliedToFile: true,
+ canBeBulkApplied: true,
+ generators: [
+ ReplaceWithNotNullAware.newInstance,
+ ],
+ ),
],
StaticWarningCode.MISSING_ENUM_CONSTANT_IN_SWITCH: [
FixInfo.single([AddMissingEnumCaseClauses.newInstance]),
diff --git a/pkg/analysis_server/test/src/services/correction/fix/replace_with_not_null_aware_test.dart b/pkg/analysis_server/test/src/services/correction/fix/replace_with_not_null_aware_test.dart
index ccbc1cf..35c3f0c 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/replace_with_not_null_aware_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/replace_with_not_null_aware_test.dart
@@ -7,15 +7,65 @@
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../../../abstract_context.dart';
+import 'bulk/bulk_fix_processor.dart';
import 'fix_processor.dart';
void main() {
defineReflectiveSuite(() {
+ defineReflectiveTests(ReplaceWithNotNullAwareBulkTest);
defineReflectiveTests(ReplaceWithNotNullAwareTest);
});
}
@reflectiveTest
+class ReplaceWithNotNullAwareBulkTest extends BulkFixProcessorTest
+ with WithNullSafetyMixin {
+ Future<void> test_notShortCircuit() async {
+ await resolveTestCode('''
+void f(A a) {
+ a?.b?.c;
+}
+class A {
+ A get b => this;
+ A get c => this;
+}
+''');
+ await assertHasFix('''
+void f(A a) {
+ a.b.c;
+}
+class A {
+ A get b => this;
+ A get c => this;
+}
+''');
+ }
+
+ Future<void> test_shortCircuit() async {
+ await resolveTestCode('''
+void f(A? a) {
+ a?.b?.c?.d;
+}
+class A {
+ A get b => this;
+ A get c => this;
+ A get d => this;
+}
+''');
+ await assertHasFix('''
+void f(A? a) {
+ a?.b.c.d;
+}
+class A {
+ A get b => this;
+ A get c => this;
+ A get d => this;
+}
+''');
+ }
+}
+
+@reflectiveTest
class ReplaceWithNotNullAwareTest extends FixProcessorTest
with WithNullSafetyMixin {
@override