Skip to content

Commit 9612b71

Browse files
FMorschelCommit Queue
authored andcommitted
[DAS] Update combinator - Use aliased import fix
[email protected] Fixes #54786 Change-Id: I1354e3cde559229d5167a27e51efb6d030e5f8bc Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/392400 Reviewed-by: Samuel Rawlins <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]> Reviewed-by: Konstantin Shcheglov <[email protected]> Auto-Submit: Felipe Morschel <[email protected]> Commit-Queue: Konstantin Shcheglov <[email protected]>
1 parent 3f717df commit 9612b71

File tree

2 files changed

+74
-13
lines changed

2 files changed

+74
-13
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,8 @@ class ImportLibrary extends MultiCorrectionProducer {
194194
if (!kinds.contains(element.kind)) {
195195
continue;
196196
}
197-
// Maybe apply a prefix.
198-
var prefix = import.prefix?.element;
199-
if (prefix != null) {
200-
producers.add(
201-
_ImportLibraryPrefix(libraryElement, prefix, context: context));
202-
continue;
203-
}
204-
// Maybe update a "show"/"hide" directive.
197+
_ImportLibraryCombinator? combinatorProducer;
198+
// Maybe update a "show" directive.
205199
var combinators = import.combinators;
206200
if (combinators.length == 1) {
207201
// Prepare library name - unit name or 'dart:name' for SDK library.
@@ -214,15 +208,27 @@ class ImportLibrary extends MultiCorrectionProducer {
214208
if (combinator is HideElementCombinator) {
215209
// Don't add this library again.
216210
alreadyImportedWithPrefix.add(libraryElement);
217-
producers.add(_ImportLibraryCombinator(libraryName, combinator, name,
218-
context: context));
211+
combinatorProducer = _ImportLibraryCombinator(
212+
libraryName, combinator, name,
213+
context: context);
219214
} else if (combinator is ShowElementCombinator) {
220215
// Don't add this library again.
221216
alreadyImportedWithPrefix.add(libraryElement);
222-
producers.add(_ImportLibraryCombinator(libraryName, combinator, name,
223-
context: context));
217+
combinatorProducer = _ImportLibraryCombinator(
218+
libraryName, combinator, name,
219+
context: context);
224220
}
225221
}
222+
// Maybe apply a prefix.
223+
var prefix = import.prefix?.element;
224+
if (prefix != null) {
225+
producers.add(_ImportLibraryPrefix(
226+
libraryElement, prefix, combinatorProducer,
227+
context: context));
228+
continue;
229+
} else if (combinatorProducer != null) {
230+
producers.add(combinatorProducer);
231+
}
226232
}
227233
// Find new top-level declarations.
228234
var librariesWithElements = await getTopLevelDeclarations(name);
@@ -564,10 +570,12 @@ class _ImportLibraryContainingExtension extends ResolvedCorrectionProducer {
564570
class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
565571
final LibraryElement _importedLibrary;
566572
final PrefixElement _importPrefix;
573+
final _ImportLibraryCombinator? _editCombinator;
567574

568575
_ImportLibraryPrefix(
569576
this._importedLibrary,
570-
this._importPrefix, {
577+
this._importPrefix,
578+
this._editCombinator, {
571579
required super.context,
572580
});
573581

@@ -595,6 +603,8 @@ class _ImportLibraryPrefix extends ResolvedCorrectionProducer {
595603
targetNode = targetNode.name;
596604
}
597605

606+
await _editCombinator?.compute(builder);
607+
598608
await builder.addDartFileEdit(file, (builder) {
599609
builder.addSimpleInsertion(targetNode.offset, '$_prefixName.');
600610
});

pkg/analysis_server/test/src/services/correction/fix/import_library_prefix_test.dart

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,57 @@ class ImportLibraryPrefixTest extends FixProcessorTest {
1919
@override
2020
FixKind get kind => DartFixKind.IMPORT_LIBRARY_PREFIX;
2121

22+
Future<void> test_double_with_showCombinator() async {
23+
newFile('$testPackageLibPath/lib1.dart', '''
24+
class A {}
25+
class B {}
26+
''');
27+
newFile('$testPackageLibPath/lib2.dart', '''
28+
class C {}
29+
''');
30+
await resolveTestCode(r'''
31+
import 'lib1.dart' as lib show A;
32+
import 'lib2.dart' as lib show C;
33+
void f(lib.C c) {
34+
lib.A? a;
35+
B b;
36+
print('$a $b');
37+
}
38+
''');
39+
await assertHasFix(r'''
40+
import 'lib1.dart' as lib show A, B;
41+
import 'lib2.dart' as lib show C;
42+
void f(lib.C c) {
43+
lib.A? a;
44+
lib.B b;
45+
print('$a $b');
46+
}
47+
''');
48+
}
49+
50+
Future<void> test_with_showCombinator() async {
51+
newFile('$testPackageLibPath/lib.dart', '''
52+
class A {}
53+
class B {}
54+
''');
55+
await resolveTestCode(r'''
56+
import 'lib.dart' as lib show A;
57+
void f() {
58+
lib.A? a;
59+
B b;
60+
print('$a $b');
61+
}
62+
''');
63+
await assertHasFix(r'''
64+
import 'lib.dart' as lib show A, B;
65+
void f() {
66+
lib.A? a;
67+
lib.B b;
68+
print('$a $b');
69+
}
70+
''');
71+
}
72+
2273
Future<void> test_withAnnotation() async {
2374
newFile('$testPackageLibPath/a.dart', '''
2475
class MyAnnotation {

0 commit comments

Comments
 (0)