Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New assist to add/edit hide at import for ambiguous import #56762

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

import 'package:analysis_server/src/services/correction/fix.dart';
import 'package:analysis_server_plugin/edit/dart/correction_producer.dart';
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:analyzer_plugin/utilities/change_builder/change_builder_core.dart';
import 'package:analyzer_plugin/utilities/fixes/fixes.dart';

class ImportAddHide extends MultiCorrectionProducer {
ImportAddHide({required super.context});

@override
Future<List<ResolvedCorrectionProducer>> get producers async {
var node = this.node;
Element? element;
if (node is NamedType) {
element = node.element;
} else if (node is SimpleIdentifier) {
element = node.staticElement;
}
if (element is! MultiplyDefinedElement) {
return const [];
}
var elements = element.conflictingElements;
var pairs = <ImportDirective, Element>{};
for (var element in elements) {
var library = element.enclosingElement3?.library;
// find all ImportDirective that import this library in this unit
for (var directive in unit.directives.whereType<ImportDirective>()) {
var imported = directive.element?.importedLibrary;
if (imported == null) {
continue;
}
if (imported == library) {
pairs[directive] = element;
}
// If the directive exports the library, then the library is also
// imported.
if (imported.exportedLibraries.contains(library)) {
pairs[directive] = element;
}
}
}
return [
for (var MapEntry(key: import, value: element) in pairs.entries)
_ImportAddHide(import, element, context: context),
];
}
}

class _ImportAddHide extends ResolvedCorrectionProducer {
_ImportAddHide(this.importDirective, this.element, {required super.context});

final ImportDirective importDirective;
final Element element;

@override
CorrectionApplicability get applicability =>
// TODO(applicability): comment on why.
CorrectionApplicability.singleLocation;

@override
FixKind get fixKind => DartFixKind.IMPORT_LIBRARY_HIDE;

@override
List<String> get fixArguments {
var aliasStr = importDirective.prefix?.name;
var alias = '';
if (aliasStr != null) {
alias = " as '$aliasStr'";
}
return [elementName, importStr, alias];
}

String get importStr => importDirective.uri.stringValue ?? '';
String get elementName => element.name ?? '';

@override
Future<void> compute(ChangeBuilder builder) async {
if (elementName.isEmpty || importStr.isEmpty) {
return;
}

if (importDirective.combinators.whereType<ShowCombinator>().isNotEmpty) {
return;
}

var hide = importDirective.combinators.whereType<HideCombinator>();
if (hide.isNotEmpty) {
return await builder.addDartFileEdit(file, (builder) {
var hideCombinator = hide.first;
var allNames = <String>[elementName];
for (var name in hideCombinator.hiddenNames) {
allNames.add(name.name);
}
allNames.sort();
var combinator = 'hide ${allNames.join(', ')}';
var range = SourceRange(hideCombinator.offset, hideCombinator.length);
builder.addSimpleReplacement(range, combinator);
});
}

await builder.addDartFileEdit(file, (builder) {
var hideCombinator = ' hide $elementName';
builder.addSimpleInsertion(importDirective.end - 1, hideCombinator);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,11 @@ CompileTimeErrorCode.AMBIGUOUS_EXPORT:
CompileTimeErrorCode.AMBIGUOUS_EXTENSION_MEMBER_ACCESS:
status: hasFix
CompileTimeErrorCode.AMBIGUOUS_IMPORT:
status: needsFix
status: hasFix
notes: |-
1. For each imported name, add a fix to hide the name.
2. For each imported name, add a fix to add a prefix. We wouldn't be able to
add the prefix everywhere, but could add it wherever the name was already
unambiguous.
For each imported name, add a fix to add a prefix. We wouldn't be able to
add the prefix everywhere, but could add it wherever the name was already
unambiguous.
CompileTimeErrorCode.AMBIGUOUS_SET_OR_MAP_LITERAL_BOTH:
status: noFix
notes: |-
Expand Down
5 changes: 5 additions & 0 deletions pkg/analysis_server/lib/src/services/correction/fix.dart
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,11 @@ abstract final class DartFixKind {
DartFixKindPriority.standard + 5,
"Update library '{0}' import",
);
static const IMPORT_LIBRARY_HIDE = FixKind(
'dart.fix.import.libraryHide',
DartFixKindPriority.standard,
"Add 'hide {0}' to library '{1}'{2} import",
);
static const INLINE_INVOCATION = FixKind(
'dart.fix.inlineInvocation',
DartFixKindPriority.standard - 20,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ import 'package:analysis_server/src/services/correction/dart/extend_class_for_mi
import 'package:analysis_server/src/services/correction/dart/extract_local_variable.dart';
import 'package:analysis_server/src/services/correction/dart/flutter_remove_widget.dart';
import 'package:analysis_server/src/services/correction/dart/ignore_diagnostic.dart';
import 'package:analysis_server/src/services/correction/dart/import_add_hide.dart';
import 'package:analysis_server/src/services/correction/dart/import_library.dart';
import 'package:analysis_server/src/services/correction/dart/inline_invocation.dart';
import 'package:analysis_server/src/services/correction/dart/inline_typedef.dart';
Expand Down Expand Up @@ -747,6 +748,9 @@ final _builtInLintProducers = <LintCode, List<ProducerGenerator>>{
};

final _builtInNonLintMultiProducers = {
CompileTimeErrorCode.AMBIGUOUS_IMPORT: [
ImportAddHide.new,
],
CompileTimeErrorCode.AMBIGUOUS_EXTENSION_MEMBER_ACCESS: [
AddExtensionOverride.new,
],
Expand Down
Loading