diff --git a/lib/src/generator/generator.dart b/lib/src/generator/generator.dart index 7246e61184..221574877e 100644 --- a/lib/src/generator/generator.dart +++ b/lib/src/generator/generator.dart @@ -7,7 +7,7 @@ library dartdoc.generator; import 'package:analyzer/file_system/file_system.dart'; import 'package:dartdoc/src/dartdoc_options.dart'; -import 'package:dartdoc/src/model/model.dart' show PackageGraph; +import 'package:dartdoc/src/model/package_graph.dart'; import 'package:dartdoc/src/package_meta.dart'; import 'package:dartdoc/src/warnings.dart'; diff --git a/lib/src/markdown_processor.dart b/lib/src/markdown_processor.dart index 6eabab0b7a..8e0f842aea 100644 --- a/lib/src/markdown_processor.dart +++ b/lib/src/markdown_processor.dart @@ -220,7 +220,8 @@ MatchingLinkResult _getMatchingLinkElementCommentReferable( allowTree = _rejectUnnamedAndShadowingConstructors; } } - + allowTree ??= (_) => true; + filter ??= (_) => true; var lookupResult = warnable.referenceBy(commentReference.referenceBy, allowTree: allowTree, filter: filter); diff --git a/lib/src/model/comment_referable.dart b/lib/src/model/comment_referable.dart index be4b5bb890..2f0f15be4c 100644 --- a/lib/src/model/comment_referable.dart +++ b/lib/src/model/comment_referable.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart=2.9 - /// /// Code for managing comment reference lookups in dartdoc. /// @@ -33,7 +31,7 @@ class ReferenceChildrenLookup { extension on Scope { /// Prefer the getter for a bundled lookup if both exist. - Element lookupPreferGetter(String id) { + Element? lookupPreferGetter(String id) { var result = lookup(id); return result.getter ?? result.setter; } @@ -84,9 +82,11 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface { /// For any [CommentReferable] where an analyzer [Scope] exists (or can /// be constructed), implement this. This will take priority over /// lookups via [referenceChildren]. Can be cached. - Scope get scope => null; + Scope? get scope => null; + + String? get href => null; - String get href => null; + static bool _alwaysTrue(CommentReferable? _) => true; /// Look up a comment reference by its component parts. If [tryParents] is /// true, try looking up the same reference in any parents of [this]. @@ -94,29 +94,28 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface { /// searching. Will skip over entire subtrees whose parent node does /// not pass [allowTree]. @nonVirtual - CommentReferable referenceBy(List reference, + CommentReferable? referenceBy(List reference, {bool tryParents = true, - bool Function(CommentReferable) filter, - bool Function(CommentReferable) allowTree, - Iterable parentOverrides}) { - filter ??= (r) => true; - allowTree ??= (r) => true; + bool Function(CommentReferable?) filter = _alwaysTrue, + bool Function(CommentReferable?) allowTree = _alwaysTrue, + Iterable? parentOverrides}) { parentOverrides ??= referenceParents; if (reference.isEmpty) { if (tryParents == false) return this; return null; } - CommentReferable result; + CommentReferable? result; /// Search for the reference. for (var referenceLookup in childLookups(reference)) { if (scope != null) { - result = lookupViaScope(referenceLookup, filter, allowTree); + result = lookupViaScope(referenceLookup, + filter: filter, allowTree: allowTree); if (result != null) break; } if (referenceChildren.containsKey(referenceLookup.lookup)) { result = recurseChildrenAndFilter( - referenceLookup, referenceChildren[referenceLookup.lookup], + referenceLookup, referenceChildren[referenceLookup.lookup]!, allowTree: allowTree, filter: filter); if (result != null) break; } @@ -141,17 +140,16 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface { /// Override if [Scope.lookup] may return elements not corresponding to a /// [CommentReferable], but you still want to have an implementation of /// [scope]. - CommentReferable lookupViaScope( - ReferenceChildrenLookup referenceLookup, - bool Function(CommentReferable) allowTree, - bool Function(CommentReferable) filter) { - var resultElement = scope.lookupPreferGetter(referenceLookup.lookup); + CommentReferable? lookupViaScope(ReferenceChildrenLookup referenceLookup, + {required bool Function(CommentReferable?) allowTree, + required bool Function(CommentReferable?) filter}) { + var resultElement = scope!.lookupPreferGetter(referenceLookup.lookup); if (resultElement == null) return null; var result = modelBuilder.fromElement(resultElement); if (result is Accessor) { - result = (result as Accessor).enclosingCombo; + result = result.enclosingCombo; } - if (result?.enclosingElement is Container) { + if (result.enclosingElement is Container) { assert(false, '[Container] member detected, support not implemented for analyzer scope inside containers'); return null; @@ -164,26 +162,26 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface { /// Given a [result] found in an implementation of [lookupViaScope] or /// [_lookupViaReferenceChildren], recurse through children, skipping over /// results that do not match the filter. - CommentReferable recurseChildrenAndFilter( + CommentReferable? recurseChildrenAndFilter( ReferenceChildrenLookup referenceLookup, CommentReferable result, - {bool Function(CommentReferable) allowTree, - bool Function(CommentReferable) filter}) { - assert(result != null); + {required bool Function(CommentReferable?) allowTree, + required bool Function(CommentReferable?) filter}) { + CommentReferable? returnValue = result; if (referenceLookup.remaining.isNotEmpty) { if (allowTree(result)) { - result = result.referenceBy(referenceLookup.remaining, + returnValue = result.referenceBy(referenceLookup.remaining, tryParents: false, allowTree: allowTree, filter: filter); } else { - result = null; + returnValue = null; } } else if (!filter(result)) { - result = result.referenceBy([referenceLookup.lookup], + returnValue = result.referenceBy([referenceLookup.lookup], tryParents: false, allowTree: allowTree, filter: filter); } - if (!filter(result)) { - result = null; + if (!filter(returnValue)) { + returnValue = null; } - return result; + return returnValue; } /// A list of lookups that should be attempted on children based on @@ -218,15 +216,15 @@ mixin CommentReferable implements Nameable, ModelBuilderInterface { /// Replace the parents of parents. [referenceBy] ignores whatever might /// otherwise be implied by the [referenceParents] of [referenceParents], /// replacing them with this. - Iterable get referenceGrandparentOverrides => null; + Iterable? get referenceGrandparentOverrides => null; // TODO(jcollins-g): Enforce that reference name is always the same // as [ModelElement.name]. Easier/possible after old lookup code is gone. String get referenceName => name; // TODO(jcollins-g): Eliminate need for this in markdown_processor. - Library get library => null; + Library? get library => null; // TODO(jcollins-g): Eliminate need for this in markdown_processor. - Element get element; + Element? get element; } diff --git a/lib/src/model/model_object_builder.dart b/lib/src/model/model_object_builder.dart index fe96da758a..1a7a53a014 100644 --- a/lib/src/model/model_object_builder.dart +++ b/lib/src/model/model_object_builder.dart @@ -2,8 +2,6 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. -// @dart=2.9 - import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:dartdoc/src/element_type.dart'; @@ -51,8 +49,7 @@ class ModelObjectBuilderImpl extends ModelObjectBuilder mixin ModelBuilder implements ModelBuilderInterface { PackageGraph get packageGraph; - ModelObjectBuilder _modelBuilder; @override - ModelObjectBuilder get modelBuilder => - _modelBuilder ??= ModelObjectBuilderImpl(packageGraph); + late final ModelObjectBuilder modelBuilder = + ModelObjectBuilderImpl(packageGraph); } diff --git a/test/comment_referable/comment_referable_test.dart b/test/comment_referable/comment_referable_test.dart index 425a32cee0..84c2465a12 100644 --- a/test/comment_referable/comment_referable_test.dart +++ b/test/comment_referable/comment_referable_test.dart @@ -32,7 +32,7 @@ abstract class Base extends Nameable with CommentReferable { {bool Function(CommentReferable) allowTree, bool Function(CommentReferable) filter}) => referenceBy(value.split(_separator), - allowTree: allowTree, filter: filter); + allowTree: allowTree ?? (_) => true, filter: filter ?? (_) => true); @override Element get element => throw UnimplementedError();