Don't remove unused imports when there are unresolved symbols.

R=brianwilkerson@google.com, devoncarew@google.com

Bug: https://github.com/dart-lang/sdk/issues/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>
This commit is contained in:
Konstantin Shcheglov 2018-03-29 21:44:19 +00:00 committed by commit-bot@chromium.org
parent ad9afa1656
commit ce1d52481c
2 changed files with 35 additions and 6 deletions

View file

@ -19,13 +19,18 @@ class DirectiveOrganizer {
final List<AnalysisError> errors;
final bool removeUnresolved;
final bool removeUnused;
String code;
String endOfLine;
bool hasUnresolvedIdentifierError;
DirectiveOrganizer(this.initialCode, this.unit, this.errors,
{this.removeUnresolved: true, this.removeUnused: true}) {
this.code = initialCode;
this.endOfLine = getEOL(code);
this.hasUnresolvedIdentifierError = errors.any((error) {
return error.errorCode.isUnresolvedIdentifier;
});
}
/**
@ -68,7 +73,7 @@ class DirectiveOrganizer {
}
/**
* Oraganize all [Directive]s.
* Organize all [Directive]s.
*/
void _organizeDirectives() {
List<_DirectiveInfo> directives = [];
@ -99,11 +104,14 @@ class DirectiveOrganizer {
StringBuffer sb = new StringBuffer();
_DirectivePriority currentPriority = null;
for (_DirectiveInfo directiveInfo in directives) {
if (removeUnresolved && _isUnresolvedUri(directiveInfo.directive)) {
continue;
}
if (removeUnused && _isUnusedImport(directiveInfo.directive)) {
continue;
if (!hasUnresolvedIdentifierError) {
UriBasedDirective directive = directiveInfo.directive;
if (removeUnresolved && _isUnresolvedUri(directive)) {
continue;
}
if (removeUnused && _isUnusedImport(directive)) {
continue;
}
}
if (currentPriority != directiveInfo.priority) {
if (sb.length != 0) {

View file

@ -185,6 +185,27 @@ main() {
}''', removeUnresolved: true, removeUnused: true);
}
test_remove_unusedImports_hasUnresolvedError() async {
Future<void> check(String declaration) async {
String code = '''
import 'dart:async';
$declaration
''';
await _computeUnitAndErrors(code);
_assertOrganize(code, removeUnused: true);
}
await check('main() { Unresolved v; }');
await check('main() { new Unresolved(); }');
await check('main() { const Unresolved(); }');
await check('main() { unresolvedFunction(); }');
await check('main() { print(unresolvedVariable); }');
await check('main() { unresolvedVariable = 0; }');
await check('main() { Unresolved.field = 0; }');
await check('class A extends Unresolved {}');
await check('List<Unresolved> v;');
}
test_sort() async {
await _computeUnitAndErrors(r'''
library lib;