mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 00:13:50 +00:00
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:
parent
ad9afa1656
commit
ce1d52481c
|
@ -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) {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue