Skip to content

Commit

Permalink
better handling of different prefixes same imported library
Browse files Browse the repository at this point in the history
  • Loading branch information
FMorschel committed Oct 29, 2024
1 parent 387e666 commit 3be7ffe
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,15 @@ class ImportAddHide extends MultiCorrectionProducer {
Future<List<ResolvedCorrectionProducer>> get producers async {
var node = this.node;
Element? element;
String? prefix;
if (node is NamedType) {
element = node.element;
prefix = node.importPrefix?.name.lexeme;
} else if (node is SimpleIdentifier) {
element = node.staticElement;
if (node.parent case PrefixedIdentifier(prefix: var currentPrefix)) {
prefix = currentPrefix.name;
}
}
if (element is! MultiplyDefinedElement) {
return const [];
Expand All @@ -37,18 +42,20 @@ class ImportAddHide extends MultiCorrectionProducer {

var directives = <ImportDirective>[];
// find all ImportDirective that import this library in this unit
// and have the same prefix
for (var directive in unit.directives.whereType<ImportDirective>()) {
// Get import directive that
var imported = directive.element?.importedLibrary;
if (imported == null) {
continue;
}
if (imported == library) {
if (imported == library && directive.prefix?.name == prefix) {
directives.add(directive);
}
// If the directive exports the library, then the library is also
// imported.
if (imported.exportedLibraries.contains(library)) {
if (imported.exportedLibraries.contains(library) &&
directive.prefix?.name == prefix) {
directives.add(directive);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,56 @@ void f() {
''', matchFixMessage: "Use 'foo' from 'lib1.dart'");
}

Future<void> test_tripleImports_oneAliased() async {
newFile('$testPackageLibPath/lib1.dart', '''
const foo = 0;''');
newFile('$testPackageLibPath/lib2.dart', '''
const foo = 0;''');
await resolveTestCode('''
import 'lib1.dart' as lib;
import 'lib1.dart';
import 'lib2.dart';
void f() {
print(foo);
}
''');
await assertHasFix('''
import 'lib1.dart' as lib;
import 'lib1.dart';
import 'lib2.dart' hide foo;
void f() {
print(foo);
}
''', matchFixMessage: "Use 'foo' from 'lib1.dart'");
}

Future<void> test_tripleImports_twoAliased() async {
newFile('$testPackageLibPath/lib1.dart', '''
const foo = 0;''');
newFile('$testPackageLibPath/lib2.dart', '''
const foo = 0;''');
await resolveTestCode('''
import 'lib1.dart';
import 'lib1.dart' as lib;
import 'lib2.dart' as lib;
void f() {
print(lib.foo);
}
''');
await assertHasFix('''
import 'lib1.dart';
import 'lib1.dart' as lib;
import 'lib2.dart' as lib hide foo;
void f() {
print(lib.foo);
}
''', matchFixMessage: "Use 'foo' from 'lib1.dart' as lib");
}

Future<void> test_doubleImports_exportedByImport() async {
newFile('$testPackageLibPath/lib1.dart', '''
export 'lib3.dart';''');
Expand Down

0 comments on commit 3be7ffe

Please sign in to comment.