Skip to content

Migrate comment_referable and model_object_builder #2832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/src/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down
3 changes: 2 additions & 1 deletion lib/src/markdown_processor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,8 @@ MatchingLinkResult _getMatchingLinkElementCommentReferable(
allowTree = _rejectUnnamedAndShadowingConstructors;
}
}

allowTree ??= (_) => true;
filter ??= (_) => true;
var lookupResult = warnable.referenceBy(commentReference.referenceBy,
allowTree: allowTree, filter: filter);

Expand Down
66 changes: 32 additions & 34 deletions lib/src/model/comment_referable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -84,39 +82,40 @@ 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].
/// Will skip over results that do not pass a given [filter] and keep
/// searching. Will skip over entire subtrees whose parent node does
/// not pass [allowTree].
@nonVirtual
CommentReferable referenceBy(List<String> reference,
CommentReferable? referenceBy(List<String> reference,
{bool tryParents = true,
bool Function(CommentReferable) filter,
bool Function(CommentReferable) allowTree,
Iterable<CommentReferable> parentOverrides}) {
filter ??= (r) => true;
allowTree ??= (r) => true;
bool Function(CommentReferable?) filter = _alwaysTrue,
bool Function(CommentReferable?) allowTree = _alwaysTrue,
Iterable<CommentReferable>? 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;
}
Expand All @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<CommentReferable> get referenceGrandparentOverrides => null;
Iterable<CommentReferable>? 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;
}
7 changes: 2 additions & 5 deletions lib/src/model/model_object_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion test/comment_referable/comment_referable_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down