forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinheritable.dart
188 lines (172 loc) · 7.24 KB
/
inheritable.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
// 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.
import 'package:analyzer/dart/element/element.dart';
import 'package:collection/collection.dart' show IterableExtension;
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
/// 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.
///
/// We pick the class closest to the [definingEnclosingContainer] so that all
/// children of that class inheriting the same member will point to the same
/// place in the documentation, and we pick a canonical class because that's
/// the one in the public namespace that will be documented.
mixin Inheritable on ContainerMember {
/// Whether this is inherited from a different class or mixin.
bool get isInherited;
/// Whether this has a parameter whose type is overridden by a subtype.
bool get isCovariant;
@override
Set<Attribute> get attributes => {
...super.attributes,
if (isOverride) Attribute.override_,
if (isInherited) Attribute.inherited,
if (isCovariant) Attribute.covariant,
};
@override
Library? get canonicalLibrary =>
canonicalEnclosingContainer?.canonicalLibrary;
@override
late final ModelElement? canonicalModelElement = canonicalEnclosingContainer
?.allCanonicalModelElements
.firstWhereOrNull((m) =>
m.name == name &&
m is PropertyAccessorElement == this is PropertyAccessorElement);
@override
Container? computeCanonicalEnclosingContainer() {
if (isInherited) {
var searchElement = element.declaration;
// TODO(jcollins-g): generate warning if an inherited element's definition
// is in an intermediate non-canonical class in the inheritance chain?
Container? previous;
Container? previousNonSkippable;
Container? found;
for (var c in inheritance.reversed) {
// Filter out mixins.
if (c.containsElement(searchElement)) {
if ((packageGraph.inheritThrough.contains(previous) &&
c != definingEnclosingContainer) ||
(packageGraph.inheritThrough.contains(c) &&
c == definingEnclosingContainer)) {
return previousNonSkippable!
.memberByExample(this)
.canonicalEnclosingContainer;
}
var canonicalContainer =
packageGraph.findCanonicalModelElementFor(c) as Container?;
// TODO(jcollins-g): invert this lookup so traversal is recursive
// starting from the ModelElement.
if (canonicalContainer != null) {
assert(canonicalContainer.isCanonical);
assert(canonicalContainer.containsElement(searchElement));
found = canonicalContainer;
break;
}
}
previous = c;
if (!packageGraph.inheritThrough.contains(c)) {
previousNonSkippable = c;
}
}
if (found != null) {
return found;
}
} else if (definingEnclosingContainer is! Extension) {
// TODO(jcollins-g): factor out extension logic into [Extendable].
return packageGraph.findCanonicalModelElementFor(enclosingElement)
as Container?;
}
return super.computeCanonicalEnclosingContainer();
}
List<InheritingContainer> get inheritance {
var inheritance = [
...(enclosingElement as InheritingContainer).inheritanceChain,
];
var object = packageGraph.specialClasses[SpecialClass.object]!;
assert(
definingEnclosingContainer == object ||
inheritance.contains(definingEnclosingContainer), () {
var inheritanceDescriptions = inheritance
.map((e) =>
"'$e' (hashCode: ${e.hashCode}, in library '${e.library}')")
.toList();
return "Given '$this', on '$enclosingElement' in library '$library', "
"the defining enclosing container, '$definingEnclosingContainer' "
'(hashCode: ${definingEnclosingContainer.hashCode}, '
"in library '${definingEnclosingContainer.library}'), should have "
'been Object or contained in: $inheritanceDescriptions';
}());
// 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;
}
Inheritable? get overriddenElement;
/// Whether this [Inheritable] is overriding a member from a superclass.
///
/// This is distinct from [isInherited]. An inheritable member which is an
/// override is explicitly written in its container. An inheritable member
/// which is implicitly included in a container is "inherited", and not an
/// override.
late final bool isOverride = () {
// The canonical version of the enclosing element -- not
// [canonicalEnclosingElement], as that is the element enclosing the
// canonical version of this element; two different things. Defaults to the
// enclosing element.
//
// We use canonical elements here where possible to deal with reexports
// as seen in Flutter.
if (enclosingElement is Extension) {
return false;
}
final overriddenElement = this.overriddenElement;
if (overriddenElement == null) {
// We have to have an overridden element for it to be possible for this
// element to be an override.
return false;
}
final enclosingCanonical =
enclosingElement.canonicalModelElement as InheritingContainer?;
// The container in which this element was defined, canonical if available.
final definingCanonical =
definingEnclosingContainer.canonicalModelElement as Container? ??
definingEnclosingContainer;
if (enclosingCanonical != definingCanonical) {
// The defining class and the enclosing class for this element must be the
// same (element is defined here).
assert(isInherited);
return false;
}
// The canonical version of the element we're overriding, if available.
final overriddenCanonical =
overriddenElement.canonicalModelElement ?? overriddenElement;
// If the overridden element isn't public, we shouldn't be an override in
// most cases. Approximation until #1623 is fixed.
final isOverride = overriddenCanonical.isPublic;
assert(!isOverride || !isInherited);
return isOverride;
}();
@override
int get overriddenDepth {
var depth = 0;
var e = this;
while (e.overriddenElement != null) {
depth += 1;
e = e.overriddenElement!;
}
return depth;
}
}