[analysis_server] Remove unnecessary imports when organizing imports

Change-Id: I7a39a9b07adbacc7d3fd5abb3b165b47668e7938
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252420
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
Commit-Queue: Brian Wilkerson <brianwilkerson@google.com>
This commit is contained in:
Danny Tuppeny 2022-07-21 15:10:55 +00:00 committed by Commit Bot
parent 71e2aee8a5
commit 5741f11f7b
2 changed files with 27 additions and 1 deletions

View file

@ -59,7 +59,8 @@ class ImportOrganizer {
bool _isUnusedImport(UriBasedDirective directive) {
for (var error in errors) {
if ((error.errorCode == HintCode.DUPLICATE_IMPORT ||
error.errorCode == HintCode.UNUSED_IMPORT) &&
error.errorCode == HintCode.UNUSED_IMPORT ||
error.errorCode == HintCode.UNNECESSARY_IMPORT) &&
directive.uri.offset == error.offset) {
return true;
}

View file

@ -245,6 +245,31 @@ void f() {
}''', removeUnused: true);
}
Future<void> test_remove_unnecessaryImports() async {
newFile(
convertPath('$testPackageLibPath/declarations.dart'),
'class A {} class B {}',
);
newFile(
convertPath('$testPackageLibPath/exports.dart'),
'export "a.dart" show A;',
);
await _computeUnitAndErrors(r'''
import 'declarations.dart';
import 'exports.dart';
A? a;
B? b;
''');
// validate change
_assertOrganize(r'''
import 'declarations.dart';
A? a;
B? b;
''', removeUnused: true);
}
Future<void> test_remove_unusedImports() async {
await _computeUnitAndErrors(r'''
library lib;