Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit ce1d524

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Don't remove unused imports when there are unresolved symbols.
R=brianwilkerson@google.com, devoncarew@google.com Bug: dart-lang/sdk#32124 Change-Id: If1b7b57bd98922768f28584325f668ba612c69d7 Reviewed-on: https://dart-review.googlesource.com/48705 Commit-Queue: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
1 parent ad9afa1 commit ce1d524

File tree

2 files changed

+35
-6
lines changed

2 files changed

+35
-6
lines changed

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

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,18 @@ class DirectiveOrganizer {
1919
final List<AnalysisError> errors;
2020
final bool removeUnresolved;
2121
final bool removeUnused;
22+
2223
String code;
2324
String endOfLine;
25+
bool hasUnresolvedIdentifierError;
2426

2527
DirectiveOrganizer(this.initialCode, this.unit, this.errors,
2628
{this.removeUnresolved: true, this.removeUnused: true}) {
2729
this.code = initialCode;
2830
this.endOfLine = getEOL(code);
31+
this.hasUnresolvedIdentifierError = errors.any((error) {
32+
return error.errorCode.isUnresolvedIdentifier;
33+
});
2934
}
3035

3136
/**
@@ -68,7 +73,7 @@ class DirectiveOrganizer {
6873
}
6974

7075
/**
71-
* Oraganize all [Directive]s.
76+
* Organize all [Directive]s.
7277
*/
7378
void _organizeDirectives() {
7479
List<_DirectiveInfo> directives = [];
@@ -99,11 +104,14 @@ class DirectiveOrganizer {
99104
StringBuffer sb = new StringBuffer();
100105
_DirectivePriority currentPriority = null;
101106
for (_DirectiveInfo directiveInfo in directives) {
102-
if (removeUnresolved && _isUnresolvedUri(directiveInfo.directive)) {
103-
continue;
104-
}
105-
if (removeUnused && _isUnusedImport(directiveInfo.directive)) {
106-
continue;
107+
if (!hasUnresolvedIdentifierError) {
108+
UriBasedDirective directive = directiveInfo.directive;
109+
if (removeUnresolved && _isUnresolvedUri(directive)) {
110+
continue;
111+
}
112+
if (removeUnused && _isUnusedImport(directive)) {
113+
continue;
114+
}
107115
}
108116
if (currentPriority != directiveInfo.priority) {
109117
if (sb.length != 0) {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,27 @@ main() {
185185
}''', removeUnresolved: true, removeUnused: true);
186186
}
187187

188+
test_remove_unusedImports_hasUnresolvedError() async {
189+
Future<void> check(String declaration) async {
190+
String code = '''
191+
import 'dart:async';
192+
$declaration
193+
''';
194+
await _computeUnitAndErrors(code);
195+
_assertOrganize(code, removeUnused: true);
196+
}
197+
198+
await check('main() { Unresolved v; }');
199+
await check('main() { new Unresolved(); }');
200+
await check('main() { const Unresolved(); }');
201+
await check('main() { unresolvedFunction(); }');
202+
await check('main() { print(unresolvedVariable); }');
203+
await check('main() { unresolvedVariable = 0; }');
204+
await check('main() { Unresolved.field = 0; }');
205+
await check('class A extends Unresolved {}');
206+
await check('List<Unresolved> v;');
207+
}
208+
188209
test_sort() async {
189210
await _computeUnitAndErrors(r'''
190211
library lib;

0 commit comments

Comments
 (0)