Skip to content
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

Display inherited elements for extension types #3556

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fixes
  • Loading branch information
srawlins committed Oct 27, 2023
commit 36b7551ca7e8b559202e9be0c9a2c2f4a94a9068
14 changes: 7 additions & 7 deletions lib/src/model/container_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ mixin ContainerMember on ModelElement implements EnclosedElement {

Container? computeCanonicalEnclosingContainer() {
final enclosingElement = this.enclosingElement;
if (enclosingElement is! Extension) {
return packageGraph.findCanonicalModelElementFor(element.enclosingElement)
as Container?;
}
// TODO(jcollins-g): move Extension specific code to [Extendable]
return packageGraph.findCanonicalModelElementFor(enclosingElement.element)
as Container?;
return switch (enclosingElement) {
Extension() =>
packageGraph.findCanonicalModelElementFor(enclosingElement.element),
ExtensionType() =>
packageGraph.findCanonicalModelElementFor(enclosingElement.element),
_ => packageGraph.findCanonicalModelElementFor(element.enclosingElement),
} as Container?;
}

@override
Expand Down
6 changes: 4 additions & 2 deletions lib/src/model/extension_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ class ExtensionType extends InheritingContainer
];

@override
// TODO(srawlins): Implement.
List<InheritingContainer> get inheritanceChain => [];
late final List<InheritingContainer> inheritanceChain = [
this,
...interfaces.expandInheritanceChain,
];

@override
String get filePath => '${library.dirName}/${fileStructure.fileName}';
Expand Down
30 changes: 16 additions & 14 deletions lib/src/model/inheritable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@ import 'package:dartdoc/src/model/attribute.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/special_elements.dart';

/// Mixin for subclasses of ModelElement representing Elements that can be
/// Mixin for subclasses of [ModelElement] representing elements that can be
/// inherited from one class to another.
///
/// We can search the inheritance chain between this instance and
/// [definingEnclosingContainer] in [Inheritable.canonicalEnclosingContainer],
/// for the canonical [Class] closest to where this member was defined. We
/// can then know that when we find [Inheritable.modelElement] inside that [Class]'s
/// namespace, that's the one we should treat as canonical and implementors
/// of this class can use that knowledge to determine canonicalization.
/// can then know that when we find [Inheritable.modelElement] inside that
/// [Class]'s namespace, that's the one we should treat as canonical and
/// implementors of this class can use that knowledge to determine
/// canonicalization.
///
/// We pick the class closest to the [definingEnclosingContainer] so that all
/// children of that class inheriting the same member will point to the same
Expand Down Expand Up @@ -89,7 +90,10 @@ mixin Inheritable on ContainerMember {
// classes.
if (definingEnclosingContainer.isCanonical &&
definingEnclosingContainer.isPublic) {
assert(definingEnclosingContainer == found);
assert(
definingEnclosingContainer == found,
'For $element, expected $definingEnclosingContainer to be Object '
'or $found, but was neither.');
}
if (found != null) {
return found;
Expand All @@ -108,16 +112,14 @@ mixin Inheritable on ContainerMember {
];
var object = packageGraph.specialClasses[SpecialClass.object]!;

if (enclosingElement is! ExtensionType) {
assert(definingEnclosingContainer == object ||
inheritance.contains(definingEnclosingContainer));
// Unless the code explicitly extends dart:core's Object, we won't get
// an entry here. So add it.
if (inheritance.last != object) {
inheritance.add(object);
}
assert(inheritance.where((e) => e == object).length == 1);
assert(definingEnclosingContainer == object ||
inheritance.contains(definingEnclosingContainer));
// Unless the code explicitly extends dart:core's Object, we won't get
// an entry here. So add it.
if (inheritance.last != object) {
inheritance.add(object);
}
assert(inheritance.where((e) => e == object).length == 1);
return inheritance;
}

Expand Down
10 changes: 3 additions & 7 deletions lib/src/model/inheriting_container.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/element/inheritance_manager3.dart'
show InheritanceManager3;
import 'package:collection/collection.dart' show IterableExtension;
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
Expand Down Expand Up @@ -154,8 +151,9 @@ abstract class InheritingContainer extends Container
}

final concreteInheritenceMap =
_inheritanceManager.getInheritedConcreteMap2(element);
final inheritenceMap = _inheritanceManager.getInheritedMap2(element);
packageGraph.inheritanceManager.getInheritedConcreteMap2(element);
final inheritenceMap =
packageGraph.inheritanceManager.getInheritedMap2(element);

List<InterfaceElement>? inheritanceChainElements;

Expand Down Expand Up @@ -195,8 +193,6 @@ abstract class InheritingContainer extends Container
return combinedMap.values.toList(growable: false);
}

static final InheritanceManager3 _inheritanceManager = InheritanceManager3();

/// All fields defined on this container, _including inherited fields_.
List<Field> get allFields {
var inheritedAccessorElements = {
Expand Down
5 changes: 5 additions & 0 deletions lib/src/model/package_graph.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/file_system/file_system.dart';
// ignore: implementation_imports
import 'package:analyzer/src/dart/element/inheritance_manager3.dart'
show InheritanceManager3;
// ignore: implementation_imports
import 'package:analyzer/src/generated/sdk.dart' show DartSdk, SdkLibrary;
// ignore: implementation_imports
import 'package:analyzer/src/generated/source.dart' show Source;
Expand Down Expand Up @@ -43,6 +46,8 @@ class PackageGraph with CommentReferable, Nameable, ModelBuilder {
Package.fromPackageMeta(packageMeta, this);
}

final InheritanceManager3 inheritanceManager = InheritanceManager3();

void dispose() {
// Clear out any cached tool snapshots and temporary directories.
// TODO(jcollins-g): Consider ownership change for these objects
Expand Down
Loading