Skip to content

Commit

Permalink
Fix issue with extension members and unfound doc comment references
Browse files Browse the repository at this point in the history
  • Loading branch information
srawlins committed Jul 3, 2024
1 parent 6a02ffb commit 729d78e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
10 changes: 6 additions & 4 deletions lib/src/model/comment_referable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ mixin CommentReferable implements Nameable {
if (resultElement == null) return null;
}

if (this case ModelElement(:var modelNode?) when resultElement == null) {
var references = modelNode.commentData?.references;
if (references != null) {
resultElement = references[referenceLookup.lookup]?.element;
if (resultElement == null) {
if (this case ModelElement(:var modelNode?)) {
var references = modelNode.commentData?.references;
if (references != null) {
resultElement = references[referenceLookup.lookup]?.element;
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions lib/src/model/container_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ mixin ContainerMember on ModelElement {
// Until then, just pretend we're handling this correctly.
[
(documentationFrom.first as ModelElement).definingLibrary,
(packageGraph.findCanonicalModelElementFor(this) ?? this).library,
];
if (this case Field(:var getter, :var setter))
packageGraph.findCanonicalModelElementFor(getter ?? setter)?.library
else
(packageGraph.findCanonicalModelElementFor(this) ?? this).library,
].nonNulls.toList();
}
3 changes: 1 addition & 2 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,10 @@ abstract class ModelElement
if (e.enclosingElement is ExtensionElement ||
e.enclosingElement is InterfaceElement ||
e is MultiplyInheritedExecutableElement) {
if (enclosingContainer == null) {
if (enclosingContainer == null || enclosingContainer is Extension) {
return ContainerAccessor(e, library, packageGraph);
}

assert(e.enclosingElement is! ExtensionElement);
return ContainerAccessor.inherited(
e, library, packageGraph, enclosingContainer,
originalMember: originalMember as ExecutableMember?);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,7 @@ class PackageGraph with CommentReferable, Nameable {
// TODO(keertip): Find a better way to exclude members of extensions
// when libraries are specified using the "--include" flag.
if (lib != null && lib.isDocumented) {
return getModelFor(element, lib);
return getModelFor(element, lib, enclosingContainer: preferredClass);
}
}
// TODO(jcollins-g): The data structures should be changed to eliminate
Expand Down
16 changes: 16 additions & 0 deletions test/extensions_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ var f() {}
);
}

void test_referenceToMissingElement_onExtensionMember() async {
var library = await bootPackageWithLibrary('''
extension E on int {
/// Text [NotFound] text.
int get f => 7;
}
''');

// We are primarily testing that dartdoc does not crash when trying to
// resolve an unknown reference, from the position of an extension member.
expect(
library.extensions.first.instanceFields.first.documentationAsHtml,
contains('<p>Text <code>NotFound</code> text.</p>'),
);
}

void test_referenceToExtensionMethod() async {
var library = await bootPackageWithLibrary('''
extension Ex on int {
Expand Down

0 comments on commit 729d78e

Please sign in to comment.