Skip to content

Fix issue with extension members and unfound doc comment references #3811

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

Merged
merged 1 commit into from
Jul 8, 2024
Merged
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
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();
}
4 changes: 1 addition & 3 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,6 @@ abstract class ModelElement
originalMember: originalMember,
);

if (enclosingContainer != null) assert(newModelElement is Inheritable);
_cacheNewModelElement(e, newModelElement, library,
enclosingContainer: enclosingContainer);

Expand Down Expand Up @@ -324,11 +323,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