22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- // @dart=2.9
6-
75///
86/// Code for managing comment reference lookups in dartdoc.
97///
@@ -33,7 +31,7 @@ class ReferenceChildrenLookup {
3331
3432extension on Scope {
3533 /// Prefer the getter for a bundled lookup if both exist.
36- Element lookupPreferGetter (String id) {
34+ Element ? lookupPreferGetter (String id) {
3735 var result = lookup (id);
3836 return result.getter ?? result.setter;
3937 }
@@ -84,39 +82,40 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
8482 /// For any [CommentReferable] where an analyzer [Scope] exists (or can
8583 /// be constructed), implement this. This will take priority over
8684 /// lookups via [referenceChildren] . Can be cached.
87- Scope get scope => null ;
85+ Scope ? get scope => null ;
86+
87+ String ? get href => null ;
8888
89- String get href => null ;
89+ static bool _alwaysTrue ( CommentReferable ? _) => true ;
9090
9191 /// Look up a comment reference by its component parts. If [tryParents] is
9292 /// true, try looking up the same reference in any parents of [this] .
9393 /// Will skip over results that do not pass a given [filter] and keep
9494 /// searching. Will skip over entire subtrees whose parent node does
9595 /// not pass [allowTree] .
9696 @nonVirtual
97- CommentReferable referenceBy (List <String > reference,
97+ CommentReferable ? referenceBy (List <String > reference,
9898 {bool tryParents = true ,
99- bool Function (CommentReferable ) filter,
100- bool Function (CommentReferable ) allowTree,
101- Iterable <CommentReferable > parentOverrides}) {
102- filter ?? = (r) => true ;
103- allowTree ?? = (r) => true ;
99+ bool Function (CommentReferable ? ) filter = _alwaysTrue,
100+ bool Function (CommentReferable ? ) allowTree = _alwaysTrue,
101+ Iterable <CommentReferable >? parentOverrides}) {
104102 parentOverrides ?? = referenceParents;
105103 if (reference.isEmpty) {
106104 if (tryParents == false ) return this ;
107105 return null ;
108106 }
109- CommentReferable result;
107+ CommentReferable ? result;
110108
111109 /// Search for the reference.
112110 for (var referenceLookup in childLookups (reference)) {
113111 if (scope != null ) {
114- result = lookupViaScope (referenceLookup, filter, allowTree);
112+ result = lookupViaScope (referenceLookup,
113+ filter: filter, allowTree: allowTree);
115114 if (result != null ) break ;
116115 }
117116 if (referenceChildren.containsKey (referenceLookup.lookup)) {
118117 result = recurseChildrenAndFilter (
119- referenceLookup, referenceChildren[referenceLookup.lookup],
118+ referenceLookup, referenceChildren[referenceLookup.lookup]! ,
120119 allowTree: allowTree, filter: filter);
121120 if (result != null ) break ;
122121 }
@@ -141,17 +140,16 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
141140 /// Override if [Scope.lookup] may return elements not corresponding to a
142141 /// [CommentReferable] , but you still want to have an implementation of
143142 /// [scope] .
144- CommentReferable lookupViaScope (
145- ReferenceChildrenLookup referenceLookup,
146- bool Function (CommentReferable ) allowTree,
147- bool Function (CommentReferable ) filter) {
148- var resultElement = scope.lookupPreferGetter (referenceLookup.lookup);
143+ CommentReferable ? lookupViaScope (ReferenceChildrenLookup referenceLookup,
144+ {required bool Function (CommentReferable ? ) allowTree,
145+ required bool Function (CommentReferable ? ) filter}) {
146+ var resultElement = scope! .lookupPreferGetter (referenceLookup.lookup);
149147 if (resultElement == null ) return null ;
150148 var result = modelBuilder.fromElement (resultElement);
151149 if (result is Accessor ) {
152- result = ( result as Accessor ) .enclosingCombo;
150+ result = result.enclosingCombo;
153151 }
154- if (result? .enclosingElement is Container ) {
152+ if (result.enclosingElement is Container ) {
155153 assert (false ,
156154 '[Container] member detected, support not implemented for analyzer scope inside containers' );
157155 return null ;
@@ -164,26 +162,26 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
164162 /// Given a [result] found in an implementation of [lookupViaScope] or
165163 /// [_lookupViaReferenceChildren] , recurse through children, skipping over
166164 /// results that do not match the filter.
167- CommentReferable recurseChildrenAndFilter (
165+ CommentReferable ? recurseChildrenAndFilter (
168166 ReferenceChildrenLookup referenceLookup, CommentReferable result,
169- {bool Function (CommentReferable ) allowTree,
170- bool Function (CommentReferable ) filter}) {
171- assert (result != null ) ;
167+ {required bool Function (CommentReferable ? ) allowTree,
168+ required bool Function (CommentReferable ? ) filter}) {
169+ CommentReferable ? returnValue = result ;
172170 if (referenceLookup.remaining.isNotEmpty) {
173171 if (allowTree (result)) {
174- result = result.referenceBy (referenceLookup.remaining,
172+ returnValue = result.referenceBy (referenceLookup.remaining,
175173 tryParents: false , allowTree: allowTree, filter: filter);
176174 } else {
177- result = null ;
175+ returnValue = null ;
178176 }
179177 } else if (! filter (result)) {
180- result = result.referenceBy ([referenceLookup.lookup],
178+ returnValue = result.referenceBy ([referenceLookup.lookup],
181179 tryParents: false , allowTree: allowTree, filter: filter);
182180 }
183- if (! filter (result )) {
184- result = null ;
181+ if (! filter (returnValue )) {
182+ returnValue = null ;
185183 }
186- return result ;
184+ return returnValue ;
187185 }
188186
189187 /// A list of lookups that should be attempted on children based on
@@ -218,15 +216,15 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
218216 /// Replace the parents of parents. [referenceBy] ignores whatever might
219217 /// otherwise be implied by the [referenceParents] of [referenceParents] ,
220218 /// replacing them with this.
221- Iterable <CommentReferable > get referenceGrandparentOverrides => null ;
219+ Iterable <CommentReferable >? get referenceGrandparentOverrides => null ;
222220
223221 // TODO(jcollins-g): Enforce that reference name is always the same
224222 // as [ModelElement.name]. Easier/possible after old lookup code is gone.
225223 String get referenceName => name;
226224
227225 // TODO(jcollins-g): Eliminate need for this in markdown_processor.
228- Library get library => null ;
226+ Library ? get library => null ;
229227
230228 // TODO(jcollins-g): Eliminate need for this in markdown_processor.
231- Element get element;
229+ Element ? get element;
232230}
0 commit comments