From 3be7ffeff11ea33d1cfe883b99b4f60ca30b4b8f Mon Sep 17 00:00:00 2001 From: FMorschel <52160996+FMorschel@users.noreply.github.com> Date: Tue, 22 Oct 2024 10:37:24 -0300 Subject: [PATCH] better handling of different prefixes same imported library --- .../correction/dart/import_add_hide.dart | 11 +++- .../correction/fix/import_add_hide_test.dart | 50 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/pkg/analysis_server/lib/src/services/correction/dart/import_add_hide.dart b/pkg/analysis_server/lib/src/services/correction/dart/import_add_hide.dart index 38bacd7a3092..6455ccae5129 100644 --- a/pkg/analysis_server/lib/src/services/correction/dart/import_add_hide.dart +++ b/pkg/analysis_server/lib/src/services/correction/dart/import_add_hide.dart @@ -18,10 +18,15 @@ class ImportAddHide extends MultiCorrectionProducer { Future> 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 []; @@ -37,18 +42,20 @@ class ImportAddHide extends MultiCorrectionProducer { var directives = []; // find all ImportDirective that import this library in this unit + // and have the same prefix for (var directive in unit.directives.whereType()) { // 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); } } diff --git a/pkg/analysis_server/test/src/services/correction/fix/import_add_hide_test.dart b/pkg/analysis_server/test/src/services/correction/fix/import_add_hide_test.dart index 9a58122bbc92..808e37df2053 100644 --- a/pkg/analysis_server/test/src/services/correction/fix/import_add_hide_test.dart +++ b/pkg/analysis_server/test/src/services/correction/fix/import_add_hide_test.dart @@ -154,6 +154,56 @@ void f() { ''', matchFixMessage: "Use 'foo' from 'lib1.dart'"); } + Future 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 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 test_doubleImports_exportedByImport() async { newFile('$testPackageLibPath/lib1.dart', ''' export 'lib3.dart';''');