Skip to content

Commit 2a48c40

Browse files
authored
Migrate comment_referable and model_object_builder (#2832)
* squash * fix error in filter setup * fix test
1 parent 0d0d46f commit 2a48c40

File tree

5 files changed

+38
-42
lines changed

5 files changed

+38
-42
lines changed

lib/src/generator/generator.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ library dartdoc.generator;
77

88
import 'package:analyzer/file_system/file_system.dart';
99
import 'package:dartdoc/src/dartdoc_options.dart';
10-
import 'package:dartdoc/src/model/model.dart' show PackageGraph;
10+
import 'package:dartdoc/src/model/package_graph.dart';
1111
import 'package:dartdoc/src/package_meta.dart';
1212
import 'package:dartdoc/src/warnings.dart';
1313

lib/src/markdown_processor.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ MatchingLinkResult _getMatchingLinkElementCommentReferable(
220220
allowTree = _rejectUnnamedAndShadowingConstructors;
221221
}
222222
}
223-
223+
allowTree ??= (_) => true;
224+
filter ??= (_) => true;
224225
var lookupResult = warnable.referenceBy(commentReference.referenceBy,
225226
allowTree: allowTree, filter: filter);
226227

lib/src/model/comment_referable.dart

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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

3432
extension 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
}

lib/src/model/model_object_builder.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
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
import 'package:analyzer/dart/element/element.dart';
86
import 'package:analyzer/dart/element/type.dart';
97
import 'package:dartdoc/src/element_type.dart';
@@ -51,8 +49,7 @@ class ModelObjectBuilderImpl extends ModelObjectBuilder
5149
mixin ModelBuilder implements ModelBuilderInterface {
5250
PackageGraph get packageGraph;
5351

54-
ModelObjectBuilder _modelBuilder;
5552
@override
56-
ModelObjectBuilder get modelBuilder =>
57-
_modelBuilder ??= ModelObjectBuilderImpl(packageGraph);
53+
late final ModelObjectBuilder modelBuilder =
54+
ModelObjectBuilderImpl(packageGraph);
5855
}

test/comment_referable/comment_referable_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ abstract class Base extends Nameable with CommentReferable {
3232
{bool Function(CommentReferable) allowTree,
3333
bool Function(CommentReferable) filter}) =>
3434
referenceBy(value.split(_separator),
35-
allowTree: allowTree, filter: filter);
35+
allowTree: allowTree ?? (_) => true, filter: filter ?? (_) => true);
3636

3737
@override
3838
Element get element => throw UnimplementedError();

0 commit comments

Comments
 (0)