Skip to content

Commit 15d3fdd

Browse files
authored
Privatize some of the interface of Class (#2292)
* Deprecate the setters Class.mixins and Class.supertype and method Class.isInheritedFrom. * Small clean-ups in Class.
1 parent 6c3242a commit 15d3fdd

File tree

2 files changed

+53
-28
lines changed

2 files changed

+53
-28
lines changed

lib/src/model/category.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class Category extends Nameable
2424
Indexable
2525
implements Documentable {
2626
/// All libraries in [libraries] must come from [package].
27+
// TODO(srawlins): To make final, remove public getter, setter, rename to be
28+
// public, and add `final` modifier.
2729
Package _package;
2830

2931
@override
@@ -35,6 +37,8 @@ class Category extends Nameable
3537

3638
final String _name;
3739

40+
// TODO(srawlins): To make final, remove public getter, setter, rename to be
41+
// public, and add `final` modifier.
3842
DartdocOptionContext _config;
3943

4044
@override

lib/src/model/class.dart

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:dartdoc/src/element_type.dart';
88
import 'package:dartdoc/src/model/extension_target.dart';
99
import 'package:dartdoc/src/model/model.dart';
1010
import 'package:dartdoc/src/model_utils.dart' as model_utils;
11+
import 'package:meta/meta.dart';
1112
import 'package:quiver/iterables.dart' as quiver;
1213

1314
/// A [Container] defined with a `class` declaration in Dart.
@@ -20,32 +21,43 @@ import 'package:quiver/iterables.dart' as quiver;
2021
class Class extends Container
2122
with TypeParameters, Categorization, ExtensionTarget
2223
implements EnclosedElement {
23-
List<DefinedElementType> mixins;
24-
DefinedElementType supertype;
25-
List<DefinedElementType> _interfaces;
26-
List<Operator> _inheritedOperators;
27-
List<Method> _inheritedMethods;
24+
// TODO(srawlins): To make final, remove public getter, setter, rename to be
25+
// public, and add `final` modifier.
26+
List<DefinedElementType> _mixins;
27+
28+
List<DefinedElementType> get mixins => _mixins;
29+
30+
@Deprecated('Field intended to be final; setter will be removed as early as '
31+
'Dartdoc 1.0.0')
32+
set mixins(List<DefinedElementType> value) => _mixins = value;
33+
34+
// TODO(srawlins): To make final, remove public getter, setter, rename to be
35+
// public, and add `final` modifier.
36+
DefinedElementType _supertype;
37+
38+
DefinedElementType get supertype => _supertype;
39+
40+
@Deprecated('Field intended to be final; setter will be removed as early as '
41+
'Dartdoc 1.0.0')
42+
set supertype(DefinedElementType value) => _supertype = value;
43+
44+
final List<DefinedElementType> _interfaces;
2845

2946
Class(ClassElement element, Library library, PackageGraph packageGraph)
30-
: super(element, library, packageGraph) {
47+
: _mixins = element.mixins
48+
.map<DefinedElementType>(
49+
(f) => ElementType.from(f, library, packageGraph))
50+
.where((mixin) => mixin != null)
51+
.toList(growable: false),
52+
_supertype = element.supertype?.element?.supertype == null
53+
? null
54+
: ElementType.from(element.supertype, library, packageGraph),
55+
_interfaces = element.interfaces
56+
.map<DefinedElementType>(
57+
(f) => ElementType.from(f, library, packageGraph))
58+
.toList(growable: false),
59+
super(element, library, packageGraph) {
3160
packageGraph.specialClasses.addSpecial(this);
32-
mixins = element.mixins
33-
.map((f) {
34-
DefinedElementType t = ElementType.from(f, library, packageGraph);
35-
return t;
36-
})
37-
.where((mixin) => mixin != null)
38-
.toList(growable: false);
39-
40-
if (element.supertype != null &&
41-
element.supertype.element.supertype != null) {
42-
supertype = ElementType.from(element.supertype, library, packageGraph);
43-
}
44-
45-
_interfaces = element.interfaces
46-
.map((f) =>
47-
ElementType.from(f, library, packageGraph) as DefinedElementType)
48-
.toList(growable: false);
4961
}
5062

5163
Constructor _defaultConstructor;
@@ -60,6 +72,7 @@ class Class extends Container
6072
Iterable<Method> get instanceMethods =>
6173
quiver.concat([super.instanceMethods, inheritedMethods]);
6274

75+
// Whether all instance methods are inherited, used in mustache templates.
6376
bool get publicInheritedInstanceMethods =>
6477
instanceMethods.every((f) => f.isInherited);
6578

@@ -87,10 +100,10 @@ class Class extends Container
87100
allModelElements.where((e) => e.isCanonical).toList());
88101
}
89102

90-
Iterable<Constructor> get constructors => element.constructors.map((e) {
91-
return ModelElement.from(e, library, packageGraph) as Constructor;
92-
});
103+
Iterable<Constructor> get constructors => element.constructors
104+
.map((e) => ModelElement.from(e, library, packageGraph) as Constructor);
93105

106+
@visibleForTesting
94107
Iterable<Constructor> get publicConstructors =>
95108
model_utils.filterNonPublic(constructors);
96109

@@ -112,6 +125,7 @@ class Class extends Container
112125
return kind;
113126
}
114127

128+
// Whether any constructors are public, used in mustache templates.
115129
bool get hasPublicConstructors => publicConstructorsSorted.isNotEmpty;
116130

117131
List<Constructor> _publicConstructorsSorted;
@@ -151,6 +165,7 @@ class Class extends Container
151165
model_utils.findCanonicalFor(packageGraph.implementors[href] ?? []));
152166
}
153167

168+
/*lazy final*/ List<Method> _inheritedMethods;
154169
Iterable<Method> get inheritedMethods {
155170
if (_inheritedMethods == null) {
156171
_inheritedMethods = <Method>[];
@@ -177,6 +192,7 @@ class Class extends Container
177192

178193
bool get hasPublicInheritedMethods => publicInheritedMethods.isNotEmpty;
179194

195+
/*lazy final*/ List<Operator> _inheritedOperators;
180196
Iterable<Operator> get inheritedOperators {
181197
if (_inheritedOperators == null) {
182198
_inheritedOperators = [];
@@ -227,9 +243,14 @@ class Class extends Container
227243
}
228244

229245
/// Returns true if [other] is a parent class for this class.
230-
bool isInheritingFrom(covariant Class other) =>
246+
bool _isInheritingFrom(Class other) =>
231247
superChain.map((et) => (et.element as Class)).contains(other);
232248

249+
@Deprecated(
250+
'Public method intended to be private; will be removed as early as '
251+
'Dartdoc 1.0.0')
252+
bool isInheritingFrom(Class other) => _isInheritingFrom(other);
253+
233254
@override
234255
String get kind => 'class';
235256

@@ -417,7 +438,7 @@ class Class extends Container
417438
// to this class on the inheritance chain.
418439
if (setter.enclosingElement is Class &&
419440
(setter.enclosingElement as Class)
420-
.isInheritingFrom(getter.enclosingElement)) {
441+
._isInheritingFrom(getter.enclosingElement)) {
421442
f = setterElement.variable;
422443
} else {
423444
f = getterElement.variable;

0 commit comments

Comments
 (0)