Skip to content

Commit cb9eefc

Browse files
scheglovcommit-bot@chromium.org
authored andcommitted
Issue 142793249. Never remove directive with unresolved URIs.
Bug: https://buganizer.corp.google.com/issues/142793249 Change-Id: I5ced1bcf4cb5d634f8e11555bb464f10c4bac68c Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/121926 Reviewed-by: Samuel Rawlins <srawlins@google.com> Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
1 parent 4f0c8cb commit cb9eefc

File tree

3 files changed

+40
-108
lines changed

3 files changed

+40
-108
lines changed

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ class DirectiveOrganizer {
1717
final String initialCode;
1818
final CompilationUnit unit;
1919
final List<AnalysisError> errors;
20-
final bool removeUnresolved;
2120
final bool removeUnused;
2221

2322
String code;
2423
String endOfLine;
2524
bool hasUnresolvedIdentifierError;
2625

2726
DirectiveOrganizer(this.initialCode, this.unit, this.errors,
28-
{this.removeUnresolved = true, this.removeUnused = true}) {
27+
{this.removeUnused = true}) {
2928
this.code = initialCode;
3029
this.endOfLine = getEOL(code);
3130
this.hasUnresolvedIdentifierError = errors.any((error) {
@@ -49,18 +48,6 @@ class DirectiveOrganizer {
4948
return edits;
5049
}
5150

52-
bool _isUnresolvedUri(UriBasedDirective directive) {
53-
for (AnalysisError error in errors) {
54-
ErrorCode errorCode = error.errorCode;
55-
if ((errorCode == CompileTimeErrorCode.URI_DOES_NOT_EXIST ||
56-
errorCode == CompileTimeErrorCode.URI_HAS_NOT_BEEN_GENERATED) &&
57-
directive.uri.offset == error.offset) {
58-
return true;
59-
}
60-
}
61-
return false;
62-
}
63-
6451
bool _isUnusedImport(UriBasedDirective directive) {
6552
for (AnalysisError error in errors) {
6653
if ((error.errorCode == HintCode.DUPLICATE_IMPORT ||
@@ -125,9 +112,6 @@ class DirectiveOrganizer {
125112
for (_DirectiveInfo directiveInfo in directives) {
126113
if (!hasUnresolvedIdentifierError) {
127114
UriBasedDirective directive = directiveInfo.directive;
128-
if (removeUnresolved && _isUnresolvedUri(directive)) {
129-
continue;
130-
}
131115
if (removeUnused && _isUnusedImport(directive)) {
132116
continue;
133117
}

pkg/analysis_server/test/edit/organize_directives_test.dart

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ main() {}
8383
);
8484
}
8585

86+
Future test_keep_unresolvedDirectives() {
87+
var code = r'''
88+
import 'dart:noSuchImportSdkLibrary';
89+
90+
import 'package:noSuchImportPackage/andLib.dart';
91+
92+
export 'dart:noSuchExportSdkLibrary';
93+
94+
export 'package:noSuchExportPackage/andLib.dart';
95+
96+
part 'no_such_part.dart';
97+
''';
98+
addTestFile(code);
99+
return _assertOrganized(code);
100+
}
101+
86102
Future test_OK_remove_duplicateImports_withSamePrefix() {
87103
addTestFile('''
88104
library lib;
@@ -105,48 +121,6 @@ main() {
105121
''');
106122
}
107123

108-
Future test_OK_remove_unresolvedDirectives() {
109-
newFile(join(testFolder, 'existing_part1.dart'), content: 'part of lib;');
110-
newFile(join(testFolder, 'existing_part2.dart'), content: 'part of lib;');
111-
addTestFile('''
112-
library lib;
113-
114-
export 'dart:noSuchExportSdkLibrary';
115-
export 'dart:async';
116-
export 'package:noSuchExportPackage/andLib.dart';
117-
export 'dart:math';
118-
119-
import 'dart:async';
120-
import 'dart:noSuchImportSdkLibrary';
121-
import 'dart:math';
122-
import 'package:noSuchImportPackage/andLib.dart';
123-
124-
part 'existing_part1.dart';
125-
part 'no_such_part.dart';
126-
part 'existing_part2.dart';
127-
128-
main(Future f) {
129-
print(PI);
130-
}
131-
''');
132-
return _assertOrganized(r'''
133-
library lib;
134-
135-
import 'dart:async';
136-
import 'dart:math';
137-
138-
export 'dart:async';
139-
export 'dart:math';
140-
141-
part 'existing_part1.dart';
142-
part 'existing_part2.dart';
143-
144-
main(Future f) {
145-
print(PI);
146-
}
147-
''');
148-
}
149-
150124
Future test_OK_remove_unusedImports() {
151125
addTestFile('''
152126
library lib;

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

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,23 @@ import 'dart:async' as async2;
6161
main() {
6262
async1.Future f;
6363
async2.Stream s;
64-
}''', removeUnresolved: true, removeUnused: true);
64+
}''', removeUnused: true);
65+
}
66+
67+
test_keep_unresolvedDirectives() async {
68+
var code = r'''
69+
import 'dart:noSuchImportSdkLibrary';
70+
71+
import 'package:noSuchImportPackage/andLib.dart';
72+
73+
export 'dart:noSuchExportSdkLibrary';
74+
75+
export 'package:noSuchExportPackage/andLib.dart';
76+
77+
part 'no_such_part.dart';
78+
''';
79+
await _computeUnitAndErrors(code);
80+
_assertOrganize(code);
6581
}
6682

6783
test_remove_duplicateImports() async {
@@ -78,7 +94,7 @@ import 'dart:async';
7894
7995
main() {
8096
Future f;
81-
}''', removeUnresolved: true, removeUnused: true);
97+
}''', removeUnused: true);
8298
}
8399

84100
test_remove_duplicateImports_differentText_uri() async {
@@ -95,7 +111,7 @@ import 'dart:async' as async;
95111
96112
main() {
97113
async.Future f;
98-
}''', removeUnresolved: true, removeUnused: true);
114+
}''', removeUnused: true);
99115
}
100116

101117
test_remove_duplicateImports_withSamePrefix() async {
@@ -112,48 +128,7 @@ import 'dart:async' as async;
112128
113129
main() {
114130
async.Future f;
115-
}''', removeUnresolved: true, removeUnused: true);
116-
}
117-
118-
test_remove_unresolvedDirectives() async {
119-
addSource('/home/test/lib/existing_part1.dart', 'part of lib;');
120-
addSource('/home/test/lib/existing_part2.dart', 'part of lib;');
121-
await _computeUnitAndErrors(r'''
122-
library lib;
123-
124-
import 'dart:async';
125-
import 'dart:noSuchImportSdkLibrary';
126-
import 'dart:math';
127-
import 'package:noSuchImportPackage/andLib.dart';
128-
129-
export 'dart:noSuchExportSdkLibrary';
130-
export 'dart:async';
131-
export 'package:noSuchExportPackage/andLib.dart';
132-
export 'dart:math';
133-
134-
part 'existing_part1.dart';
135-
part 'no_such_part.dart';
136-
part 'existing_part2.dart';
137-
138-
main() {
139-
}
140-
''');
141-
// validate change
142-
_assertOrganize(r'''
143-
library lib;
144-
145-
import 'dart:async';
146-
import 'dart:math';
147-
148-
export 'dart:async';
149-
export 'dart:math';
150-
151-
part 'existing_part1.dart';
152-
part 'existing_part2.dart';
153-
154-
main() {
155-
}
156-
''', removeUnresolved: true);
131+
}''', removeUnused: true);
157132
}
158133

159134
test_remove_unusedImports() async {
@@ -202,7 +177,7 @@ class A {}
202177
203178
main() {
204179
Future f;
205-
}''', removeUnresolved: true, removeUnused: true);
180+
}''', removeUnused: true);
206181
}
207182

208183
test_remove_unusedImports_hasUnresolvedError() async {
@@ -340,11 +315,10 @@ import 'package:product2.client/entity.dart';
340315
''');
341316
}
342317

343-
void _assertOrganize(String expectedCode,
344-
{bool removeUnresolved = false, bool removeUnused = false}) {
318+
void _assertOrganize(String expectedCode, {bool removeUnused = false}) {
345319
DirectiveOrganizer organizer = new DirectiveOrganizer(
346320
testCode, testUnit, testErrors,
347-
removeUnresolved: removeUnresolved, removeUnused: removeUnused);
321+
removeUnused: removeUnused);
348322
List<SourceEdit> edits = organizer.organize();
349323
String result = SourceEdit.applySequence(testCode, edits);
350324
expect(result, expectedCode);

0 commit comments

Comments
 (0)