Skip to content

Commit 2803425

Browse files
authoredSep 2, 2020
Use first element in a MultiplyDefinedElement (#2326)
1 parent a416b03 commit 2803425

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed
 

‎lib/src/markdown_processor.dart

+44-10
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,9 @@ class _MarkdownCommentReference {
438438
// Squelch ambiguous doc reference warnings for parameters, because we
439439
// don't link those anyway.
440440
if (!results.every((r) => r is Parameter)) {
441+
var elementNames = results.map((r) => "'${r.fullyQualifiedName}'");
441442
element.warn(PackageWarning.ambiguousDocReference,
442-
message:
443-
"[$codeRef] => ${results.map((r) => "'${r.fullyQualifiedName}'").join(", ")}");
443+
message: '[$codeRef] => ${elementNames.join(', ')}');
444444
}
445445
result = results.first;
446446
}
@@ -683,17 +683,51 @@ class _MarkdownCommentReference {
683683

684684
void _findAnalyzerReferences() {
685685
var refElement = _getRefElementFromCommentRefs(commentRefs, codeRef);
686-
if (refElement != null) {
687-
var refModelElement = ModelElement.fromElement(
688-
_getRefElementFromCommentRefs(commentRefs, codeRef),
686+
if (refElement == null) return;
687+
688+
ModelElement refModelElement;
689+
if (refElement is MultiplyDefinedElement) {
690+
var elementNames = refElement.conflictingElements
691+
.map((e) => "'${_fullyQualifiedElementName(e)}'");
692+
element.warn(PackageWarning.ambiguousDocReference,
693+
message: '[$codeRef] => [${elementNames.join(', ')}]');
694+
refModelElement = ModelElement.fromElement(
695+
// Continue; just use the first conflicting element.
696+
refElement.conflictingElements.first,
689697
element.packageGraph);
690-
if (refModelElement is Accessor) {
691-
refModelElement = (refModelElement as Accessor).enclosingCombo;
692-
}
698+
} else {
693699
refModelElement =
694-
refModelElement.canonicalModelElement ?? refModelElement;
695-
results.add(refModelElement);
700+
ModelElement.fromElement(refElement, element.packageGraph);
701+
}
702+
if (refModelElement is Accessor) {
703+
refModelElement = (refModelElement as Accessor).enclosingCombo;
704+
}
705+
refModelElement = refModelElement.canonicalModelElement ?? refModelElement;
706+
results.add(refModelElement);
707+
}
708+
709+
/// Generates a fully-qualified name, similar to that of
710+
/// [ModelElement.fullyQualifiedName], for an Element.
711+
static String _fullyQualifiedElementName(Element element) {
712+
var enclosingElement = element.enclosingElement;
713+
714+
var enclosingName = enclosingElement == null
715+
? null
716+
: _fullyQualifiedElementName(enclosingElement);
717+
var name = element.name;
718+
if (name == null) {
719+
if (element is ExtensionElement) {
720+
name = '<unnamed extension>';
721+
} else if (element is LibraryElement) {
722+
name = '<unnamed library>';
723+
} else if (element is CompilationUnitElement) {
724+
return enclosingName;
725+
} else {
726+
name = '<unnamed ${element.runtimeType}>';
727+
}
696728
}
729+
730+
return enclosingName == null ? name : '$enclosingName.$name';
697731
}
698732

699733
// Add a result, but make it canonical.

‎lib/src/model/model_element.dart

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ abstract class ModelElement extends Canonicalization
313313
return resolveMultiplyInheritedElement(
314314
e, library, packageGraph, enclosingContainer);
315315
}
316+
assert(e is! MultiplyDefinedElement);
316317
if (e is LibraryElement) {
317318
return Library(e, packageGraph);
318319
}

0 commit comments

Comments
 (0)
Please sign in to comment.