bulk fix for `prefer_const_constructors`
Change-Id: I54579790f7fd35cb79ac2f28609a620d1a4f9c2b
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/164423
Commit-Queue: Phil Quitslund <pquitslund@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart b/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart
index 78b882b..66c8c45 100644
--- a/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart
+++ b/pkg/analysis_server/lib/src/services/correction/bulk_fix_processor.dart
@@ -140,6 +140,11 @@
LintNames.prefer_conditional_assignment: [
ReplaceWithConditionalAssignment.newInstance,
],
+ // TODO (pq): can produce results incompatible w/ `unnecessary_const`
+ // LintNames.prefer_const_constructors: [
+ // AddConst.newInstance,
+ // ReplaceNewWithConst.newInstance,
+ // ],
LintNames.prefer_const_constructors_in_immutables: [
AddConst.newInstance,
],
diff --git a/pkg/analysis_server/test/src/services/correction/fix/bulk/add_const_test.dart b/pkg/analysis_server/test/src/services/correction/fix/bulk/add_const_test.dart
index 7962f4f..735e1c5 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/bulk/add_const_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/bulk/add_const_test.dart
@@ -9,11 +9,37 @@
void main() {
defineReflectiveSuite(() {
+ defineReflectiveTests(AddConstToConstructorTest);
defineReflectiveTests(AddConstToImmutableConstructorTest);
});
}
@reflectiveTest
+class AddConstToConstructorTest extends BulkFixProcessorTest {
+ @override
+ String get lintCode => LintNames.prefer_const_constructors;
+
+ /// Disabled in BulkFixProcessor.
+ @failingTest
+ Future<void> test_singleFile() async {
+ addMetaPackage();
+ await resolveTestUnit(r'''
+class C {
+ const C([C c]);
+}
+var c = C(C());
+''');
+ // TODO (pq): results are incompatible w/ `unnecessary_const`
+ await assertHasFix(r'''
+class C {
+ const C([C c]);
+}
+var c = const C(const C());
+''');
+ }
+}
+
+@reflectiveTest
class AddConstToImmutableConstructorTest extends BulkFixProcessorTest {
@override
String get lintCode => LintNames.prefer_const_constructors_in_immutables;
diff --git a/pkg/analysis_server/test/src/services/correction/fix/bulk/replace_new_with_const_test.dart b/pkg/analysis_server/test/src/services/correction/fix/bulk/replace_new_with_const_test.dart
new file mode 100644
index 0000000..a6c5170
--- /dev/null
+++ b/pkg/analysis_server/test/src/services/correction/fix/bulk/replace_new_with_const_test.dart
@@ -0,0 +1,41 @@
+// Copyright (c) 2020, 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.
+
+import 'package:analysis_server/src/services/linter/lint_names.dart';
+import 'package:test_reflective_loader/test_reflective_loader.dart';
+
+import 'bulk_fix_processor.dart';
+
+void main() {
+ defineReflectiveSuite(() {
+ defineReflectiveTests(ReplaceNewWithConstTest);
+ });
+}
+
+@reflectiveTest
+class ReplaceNewWithConstTest extends BulkFixProcessorTest {
+ @override
+ String get lintCode => LintNames.prefer_const_constructors;
+
+ /// Disabled in BulkFixProcessor.
+ @failingTest
+ Future<void> test_singleFile() async {
+ await resolveTestUnit(r'''
+class C {
+ const C();
+}
+main() {
+ print('${new C()} ${new C()}');
+}
+''');
+ await assertHasFix(r'''
+class C {
+ const C();
+}
+main() {
+ print('${const C()} ${const C()}');
+}
+''');
+ }
+}
diff --git a/pkg/analysis_server/test/src/services/correction/fix/bulk/test_all.dart b/pkg/analysis_server/test/src/services/correction/fix/bulk/test_all.dart
index fd34894..5fb51b9 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/bulk/test_all.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/bulk/test_all.dart
@@ -49,6 +49,7 @@
import 'rename_to_camel_case_test.dart' as rename_to_camel_case;
import 'replace_colon_with_equals_test.dart' as replace_colon_with_equals;
import 'replace_final_with_const_test.dart' as replace_final_with_const;
+import 'replace_new_with_const_test.dart' as replace_new_with_const;
import 'replace_null_with_closure_test.dart' as replace_null_with_closure;
import 'replace_with_conditional_assignment_test.dart'
as replace_with_conditional_assignment;
@@ -102,6 +103,7 @@
replace_with_conditional_assignment.main();
replace_colon_with_equals.main();
replace_final_with_const.main();
+ replace_new_with_const.main();
replace_null_with_closure.main();
replace_with_is_empty.main();
replace_with_tear_off.main();