2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
- // @dart=2.9
6
-
7
5
///
8
6
/// Code for managing comment reference lookups in dartdoc.
9
7
///
@@ -33,7 +31,7 @@ class ReferenceChildrenLookup {
33
31
34
32
extension on Scope {
35
33
/// Prefer the getter for a bundled lookup if both exist.
36
- Element lookupPreferGetter (String id) {
34
+ Element ? lookupPreferGetter (String id) {
37
35
var result = lookup (id);
38
36
return result.getter ?? result.setter;
39
37
}
@@ -84,39 +82,40 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
84
82
/// For any [CommentReferable] where an analyzer [Scope] exists (or can
85
83
/// be constructed), implement this. This will take priority over
86
84
/// lookups via [referenceChildren] . Can be cached.
87
- Scope get scope => null ;
85
+ Scope ? get scope => null ;
86
+
87
+ String ? get href => null ;
88
88
89
- String get href => null ;
89
+ static bool _alwaysTrue ( CommentReferable ? _) => true ;
90
90
91
91
/// Look up a comment reference by its component parts. If [tryParents] is
92
92
/// true, try looking up the same reference in any parents of [this] .
93
93
/// Will skip over results that do not pass a given [filter] and keep
94
94
/// searching. Will skip over entire subtrees whose parent node does
95
95
/// not pass [allowTree] .
96
96
@nonVirtual
97
- CommentReferable referenceBy (List <String > reference,
97
+ CommentReferable ? referenceBy (List <String > reference,
98
98
{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}) {
104
102
parentOverrides ?? = referenceParents;
105
103
if (reference.isEmpty) {
106
104
if (tryParents == false ) return this ;
107
105
return null ;
108
106
}
109
- CommentReferable result;
107
+ CommentReferable ? result;
110
108
111
109
/// Search for the reference.
112
110
for (var referenceLookup in childLookups (reference)) {
113
111
if (scope != null ) {
114
- result = lookupViaScope (referenceLookup, filter, allowTree);
112
+ result = lookupViaScope (referenceLookup,
113
+ filter: filter, allowTree: allowTree);
115
114
if (result != null ) break ;
116
115
}
117
116
if (referenceChildren.containsKey (referenceLookup.lookup)) {
118
117
result = recurseChildrenAndFilter (
119
- referenceLookup, referenceChildren[referenceLookup.lookup],
118
+ referenceLookup, referenceChildren[referenceLookup.lookup]! ,
120
119
allowTree: allowTree, filter: filter);
121
120
if (result != null ) break ;
122
121
}
@@ -141,17 +140,16 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
141
140
/// Override if [Scope.lookup] may return elements not corresponding to a
142
141
/// [CommentReferable] , but you still want to have an implementation of
143
142
/// [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);
149
147
if (resultElement == null ) return null ;
150
148
var result = modelBuilder.fromElement (resultElement);
151
149
if (result is Accessor ) {
152
- result = ( result as Accessor ) .enclosingCombo;
150
+ result = result.enclosingCombo;
153
151
}
154
- if (result? .enclosingElement is Container ) {
152
+ if (result.enclosingElement is Container ) {
155
153
assert (false ,
156
154
'[Container] member detected, support not implemented for analyzer scope inside containers' );
157
155
return null ;
@@ -164,26 +162,26 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
164
162
/// Given a [result] found in an implementation of [lookupViaScope] or
165
163
/// [_lookupViaReferenceChildren] , recurse through children, skipping over
166
164
/// results that do not match the filter.
167
- CommentReferable recurseChildrenAndFilter (
165
+ CommentReferable ? recurseChildrenAndFilter (
168
166
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 ;
172
170
if (referenceLookup.remaining.isNotEmpty) {
173
171
if (allowTree (result)) {
174
- result = result.referenceBy (referenceLookup.remaining,
172
+ returnValue = result.referenceBy (referenceLookup.remaining,
175
173
tryParents: false , allowTree: allowTree, filter: filter);
176
174
} else {
177
- result = null ;
175
+ returnValue = null ;
178
176
}
179
177
} else if (! filter (result)) {
180
- result = result.referenceBy ([referenceLookup.lookup],
178
+ returnValue = result.referenceBy ([referenceLookup.lookup],
181
179
tryParents: false , allowTree: allowTree, filter: filter);
182
180
}
183
- if (! filter (result )) {
184
- result = null ;
181
+ if (! filter (returnValue )) {
182
+ returnValue = null ;
185
183
}
186
- return result ;
184
+ return returnValue ;
187
185
}
188
186
189
187
/// A list of lookups that should be attempted on children based on
@@ -218,15 +216,15 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface {
218
216
/// Replace the parents of parents. [referenceBy] ignores whatever might
219
217
/// otherwise be implied by the [referenceParents] of [referenceParents] ,
220
218
/// replacing them with this.
221
- Iterable <CommentReferable > get referenceGrandparentOverrides => null ;
219
+ Iterable <CommentReferable >? get referenceGrandparentOverrides => null ;
222
220
223
221
// TODO(jcollins-g): Enforce that reference name is always the same
224
222
// as [ModelElement.name]. Easier/possible after old lookup code is gone.
225
223
String get referenceName => name;
226
224
227
225
// TODO(jcollins-g): Eliminate need for this in markdown_processor.
228
- Library get library => null ;
226
+ Library ? get library => null ;
229
227
230
228
// TODO(jcollins-g): Eliminate need for this in markdown_processor.
231
- Element get element;
229
+ Element ? get element;
232
230
}
0 commit comments