fix all support for REMOVE_UNUSED_IMPORT
* adds support for non lint fix all fixes
* enables multi fix for REMOVE_UNUSED_IMPORT
Change-Id: Ib08e82686622d449592cd899df8d63873232a703
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/188587
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/remove_unused_import.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart
index bb5bf5a..3fa7457 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_unused_import.dart
@@ -14,6 +14,9 @@
FixKind get fixKind => DartFixKind.REMOVE_UNUSED_IMPORT;
@override
+ FixKind get multiFixKind => DartFixKind.REMOVE_UNUSED_IMPORT_MULTI;
+
+ @override
Future<void> compute(ChangeBuilder builder) async {
// prepare ImportDirective
var importDirective = node.thisOrAncestorOfType<ImportDirective>();
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 e8bc6be..c6ddc15 100644
--- a/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
+++ b/pkg/analysis_server/lib/src/services/correction/fix_internal.dart
@@ -287,13 +287,12 @@
}
}
} else {
- // todo (pq): add support for non-lint producers and update to a new nonLintProducerMap2
- // var generators = FixProcessor.nonLintProducerMap[errorCode];
- // if (generators != null) {
- // if (generators != null) {
- // producers.addAll(generators);
- // }
- // }
+ var fixInfos = FixProcessor.nonLintProducerMap2[errorCode] ?? [];
+ for (var fixInfo in fixInfos) {
+ if (fixInfo.canBeAppliedToFile) {
+ producers.addAll(fixInfo.generators);
+ }
+ }
// todo (pq): consider support for multiGenerators
}
return producers;
@@ -313,6 +312,19 @@
/// The computer for Dart fixes.
class FixProcessor extends BaseProcessor {
+ /// todo (pq): to replace nonLintProducerMap.
+ static const Map<ErrorCode, List<FixInfo>> nonLintProducerMap2 = {
+ HintCode.UNUSED_IMPORT: [
+ FixInfo(
+ canBeAppliedToFile: true,
+ canBeBulkApplied: false,
+ generators: [
+ RemoveUnusedImport.newInstance,
+ ],
+ ),
+ ],
+ };
+
/// todo (pq): to replace lintProducerMap.
static const Map<String, List<FixInfo>> lintProducerMap2 = {
LintNames.always_declare_return_types: [
diff --git a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_import_test.dart b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_import_test.dart
index 6a9cc3e..020945a 100644
--- a/pkg/analysis_server/test/src/services/correction/fix/remove_unused_import_test.dart
+++ b/pkg/analysis_server/test/src/services/correction/fix/remove_unused_import_test.dart
@@ -12,13 +12,14 @@
void main() {
defineReflectiveSuite(() {
defineReflectiveTests(RemoveUnusedImportTest);
+ defineReflectiveTests(RemoveUnusedImportMultiTest);
});
}
@reflectiveTest
-class RemoveUnusedImportTest extends FixProcessorTest {
+class RemoveUnusedImportMultiTest extends FixProcessorTest {
@override
- FixKind get kind => DartFixKind.REMOVE_UNUSED_IMPORT;
+ FixKind get kind => DartFixKind.REMOVE_UNUSED_IMPORT_MULTI;
@override
void setUp() {
@@ -27,7 +28,6 @@
useLineEndingsForPlatform = false;
}
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_all_diverseImports() async {
await resolveTestCode('''
import 'dart:math';
@@ -42,7 +42,6 @@
''');
}
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
Future<void> test_all_diverseImports2() async {
await resolveTestCode('''
import 'dart:async';
@@ -64,7 +63,7 @@
''');
}
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
+ @FailingTest(reason: 'one unused import remains unremoved')
Future<void> test_all_singleLine() async {
await resolveTestCode('''
import 'dart:math'; import 'dart:math'; import 'dart:math';
@@ -77,6 +76,33 @@
''');
}
+ Future<void> test_multipleOfSame_all() async {
+ await resolveTestCode('''
+import 'dart:math';
+import 'dart:math';
+import 'dart:math';
+main() {
+}
+''');
+ await assertHasFixAllFix(HintCode.UNUSED_IMPORT, '''
+main() {
+}
+''');
+ }
+}
+
+@reflectiveTest
+class RemoveUnusedImportTest extends FixProcessorTest {
+ @override
+ FixKind get kind => DartFixKind.REMOVE_UNUSED_IMPORT;
+
+ @override
+ void setUp() {
+ super.setUp();
+ // TODO(dantup): Get these tests passing with either line ending.
+ useLineEndingsForPlatform = false;
+ }
+
Future<void> test_anotherImportOnLine() async {
await resolveTestCode('''
import 'dart:math'; import 'dart:async';
@@ -114,21 +140,6 @@
''');
}
- @FailingTest(issue: 'https://github.com/dart-lang/sdk/issues/45026')
- Future<void> test_multipleOfSame_all() async {
- await resolveTestCode('''
-import 'dart:math';
-import 'dart:math';
-import 'dart:math';
-main() {
-}
-''');
- await assertHasFixAllFix(HintCode.UNUSED_IMPORT, '''
-main() {
-}
-''');
- }
-
Future<void> test_severalLines() async {
await resolveTestCode('''
import