Skip to content

Commit 5741f11

Browse files
DanTupCommit Bot
authored and
Commit Bot
committed
[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 <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 71e2aee commit 5741f11

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

pkg/analysis_server/lib/src/services/correction/organize_imports.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ class ImportOrganizer {
5959
bool _isUnusedImport(UriBasedDirective directive) {
6060
for (var error in errors) {
6161
if ((error.errorCode == HintCode.DUPLICATE_IMPORT ||
62-
error.errorCode == HintCode.UNUSED_IMPORT) &&
62+
error.errorCode == HintCode.UNUSED_IMPORT ||
63+
error.errorCode == HintCode.UNNECESSARY_IMPORT) &&
6364
directive.uri.offset == error.offset) {
6465
return true;
6566
}

pkg/analysis_server/test/services/correction/organize_directives_test.dart

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,31 @@ void f() {
245245
}''', removeUnused: true);
246246
}
247247

248+
Future<void> test_remove_unnecessaryImports() async {
249+
newFile(
250+
convertPath('$testPackageLibPath/declarations.dart'),
251+
'class A {} class B {}',
252+
);
253+
newFile(
254+
convertPath('$testPackageLibPath/exports.dart'),
255+
'export "a.dart" show A;',
256+
);
257+
await _computeUnitAndErrors(r'''
258+
import 'declarations.dart';
259+
import 'exports.dart';
260+
261+
A? a;
262+
B? b;
263+
''');
264+
// validate change
265+
_assertOrganize(r'''
266+
import 'declarations.dart';
267+
268+
A? a;
269+
B? b;
270+
''', removeUnused: true);
271+
}
272+
248273
Future<void> test_remove_unusedImports() async {
249274
await _computeUnitAndErrors(r'''
250275
library lib;

0 commit comments

Comments
 (0)