From 729d78ebf9eadce152ef3e5e3433b1d36b8a776e Mon Sep 17 00:00:00 2001 From: Sam Rawlins Date: Wed, 3 Jul 2024 09:33:21 -0700 Subject: [PATCH] Fix issue with extension members and unfound doc comment references --- lib/src/model/comment_referable.dart | 10 ++++++---- lib/src/model/container_member.dart | 7 +++++-- lib/src/model/model_element.dart | 3 +-- lib/src/model/package_graph.dart | 2 +- test/extensions_test.dart | 16 ++++++++++++++++ 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/src/model/comment_referable.dart b/lib/src/model/comment_referable.dart index 19d30605ba..1b86636b3d 100644 --- a/lib/src/model/comment_referable.dart +++ b/lib/src/model/comment_referable.dart @@ -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; + } } } diff --git a/lib/src/model/container_member.dart b/lib/src/model/container_member.dart index 7ffbb9f1a7..80e54eae6a 100644 --- a/lib/src/model/container_member.dart +++ b/lib/src/model/container_member.dart @@ -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(); } diff --git a/lib/src/model/model_element.dart b/lib/src/model/model_element.dart index cc7e979f8a..ede6fbc985 100644 --- a/lib/src/model/model_element.dart +++ b/lib/src/model/model_element.dart @@ -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?); diff --git a/lib/src/model/package_graph.dart b/lib/src/model/package_graph.dart index cd39dfbce5..1f9137bd6a 100644 --- a/lib/src/model/package_graph.dart +++ b/lib/src/model/package_graph.dart @@ -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 diff --git a/test/extensions_test.dart b/test/extensions_test.dart index c61ff49f20..391b984895 100644 --- a/test/extensions_test.dart +++ b/test/extensions_test.dart @@ -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('

Text NotFound text.

'), + ); + } + void test_referenceToExtensionMethod() async { var library = await bootPackageWithLibrary(''' extension Ex on int {