Skip to content

Commit 51a2713

Browse files
authored
Add support and tests for class modifiers (#3354)
1 parent 4c35154 commit 51a2713

9 files changed

+627
-298
lines changed

lib/src/generator/templates.runtime_renderers.dart

Lines changed: 176 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1673,28 +1673,6 @@ class _Renderer_Class extends RendererBase<Class> {
16731673
parent: r);
16741674
},
16751675
),
1676-
'fullkind': Property(
1677-
getValue: (CT_ c) => c.fullkind,
1678-
renderVariable:
1679-
(CT_ c, Property<CT_> self, List<String> remainingNames) {
1680-
if (remainingNames.isEmpty) {
1681-
return self.getValue(c).toString();
1682-
}
1683-
var name = remainingNames.first;
1684-
var nextProperty =
1685-
_Renderer_String.propertyMap().getValue(name);
1686-
return nextProperty.renderVariable(
1687-
self.getValue(c) as String,
1688-
nextProperty,
1689-
[...remainingNames.skip(1)]);
1690-
},
1691-
isNullValue: (CT_ c) => false,
1692-
renderValue: (CT_ c, RendererBase<CT_> r,
1693-
List<MustachioNode> ast, StringSink sink) {
1694-
_render_String(c.fullkind, ast, r.template, sink,
1695-
parent: r);
1696-
},
1697-
),
16981676
'inheritanceChain': Property(
16991677
getValue: (CT_ c) => c.inheritanceChain,
17001678
renderVariable: (CT_ c, Property<CT_> self,
@@ -1715,13 +1693,48 @@ class _Renderer_Class extends RendererBase<Class> {
17151693
self.renderSimpleVariable(c, remainingNames, 'bool'),
17161694
getBool: (CT_ c) => c.isAbstract == true,
17171695
),
1696+
'isBase': Property(
1697+
getValue: (CT_ c) => c.isBase,
1698+
renderVariable: (CT_ c, Property<CT_> self,
1699+
List<String> remainingNames) =>
1700+
self.renderSimpleVariable(c, remainingNames, 'bool'),
1701+
getBool: (CT_ c) => c.isBase == true,
1702+
),
17181703
'isErrorOrException': Property(
17191704
getValue: (CT_ c) => c.isErrorOrException,
17201705
renderVariable: (CT_ c, Property<CT_> self,
17211706
List<String> remainingNames) =>
17221707
self.renderSimpleVariable(c, remainingNames, 'bool'),
17231708
getBool: (CT_ c) => c.isErrorOrException == true,
17241709
),
1710+
'isFinal': Property(
1711+
getValue: (CT_ c) => c.isFinal,
1712+
renderVariable: (CT_ c, Property<CT_> self,
1713+
List<String> remainingNames) =>
1714+
self.renderSimpleVariable(c, remainingNames, 'bool'),
1715+
getBool: (CT_ c) => c.isFinal == true,
1716+
),
1717+
'isInterface': Property(
1718+
getValue: (CT_ c) => c.isInterface,
1719+
renderVariable: (CT_ c, Property<CT_> self,
1720+
List<String> remainingNames) =>
1721+
self.renderSimpleVariable(c, remainingNames, 'bool'),
1722+
getBool: (CT_ c) => c.isInterface == true,
1723+
),
1724+
'isMixinClass': Property(
1725+
getValue: (CT_ c) => c.isMixinClass,
1726+
renderVariable: (CT_ c, Property<CT_> self,
1727+
List<String> remainingNames) =>
1728+
self.renderSimpleVariable(c, remainingNames, 'bool'),
1729+
getBool: (CT_ c) => c.isMixinClass == true,
1730+
),
1731+
'isSealed': Property(
1732+
getValue: (CT_ c) => c.isSealed,
1733+
renderVariable: (CT_ c, Property<CT_> self,
1734+
List<String> remainingNames) =>
1735+
self.renderSimpleVariable(c, remainingNames, 'bool'),
1736+
getBool: (CT_ c) => c.isSealed == true,
1737+
),
17251738
'kind': Property(
17261739
getValue: (CT_ c) => c.kind,
17271740
renderVariable:
@@ -4358,6 +4371,41 @@ class _Renderer_Enum extends RendererBase<Enum> {
43584371
parent: r));
43594372
},
43604373
),
4374+
'isAbstract': Property(
4375+
getValue: (CT_ c) => c.isAbstract,
4376+
renderVariable: (CT_ c, Property<CT_> self,
4377+
List<String> remainingNames) =>
4378+
self.renderSimpleVariable(c, remainingNames, 'bool'),
4379+
getBool: (CT_ c) => c.isAbstract == true,
4380+
),
4381+
'isBase': Property(
4382+
getValue: (CT_ c) => c.isBase,
4383+
renderVariable: (CT_ c, Property<CT_> self,
4384+
List<String> remainingNames) =>
4385+
self.renderSimpleVariable(c, remainingNames, 'bool'),
4386+
getBool: (CT_ c) => c.isBase == true,
4387+
),
4388+
'isInterface': Property(
4389+
getValue: (CT_ c) => c.isInterface,
4390+
renderVariable: (CT_ c, Property<CT_> self,
4391+
List<String> remainingNames) =>
4392+
self.renderSimpleVariable(c, remainingNames, 'bool'),
4393+
getBool: (CT_ c) => c.isInterface == true,
4394+
),
4395+
'isMixinClass': Property(
4396+
getValue: (CT_ c) => c.isMixinClass,
4397+
renderVariable: (CT_ c, Property<CT_> self,
4398+
List<String> remainingNames) =>
4399+
self.renderSimpleVariable(c, remainingNames, 'bool'),
4400+
getBool: (CT_ c) => c.isMixinClass == true,
4401+
),
4402+
'isSealed': Property(
4403+
getValue: (CT_ c) => c.isSealed,
4404+
renderVariable: (CT_ c, Property<CT_> self,
4405+
List<String> remainingNames) =>
4406+
self.renderSimpleVariable(c, remainingNames, 'bool'),
4407+
getBool: (CT_ c) => c.isSealed == true,
4408+
),
43614409
'kind': Property(
43624410
getValue: (CT_ c) => c.kind,
43634411
renderVariable:
@@ -6971,13 +7019,55 @@ class _Renderer_InheritingContainer extends RendererBase<InheritingContainer> {
69717019
_render_Operator(e, ast, r.template, sink, parent: r));
69727020
},
69737021
),
7022+
'isAbstract': Property(
7023+
getValue: (CT_ c) => c.isAbstract,
7024+
renderVariable: (CT_ c, Property<CT_> self,
7025+
List<String> remainingNames) =>
7026+
self.renderSimpleVariable(c, remainingNames, 'bool'),
7027+
getBool: (CT_ c) => c.isAbstract == true,
7028+
),
7029+
'isBase': Property(
7030+
getValue: (CT_ c) => c.isBase,
7031+
renderVariable: (CT_ c, Property<CT_> self,
7032+
List<String> remainingNames) =>
7033+
self.renderSimpleVariable(c, remainingNames, 'bool'),
7034+
getBool: (CT_ c) => c.isBase == true,
7035+
),
69747036
'isCanonical': Property(
69757037
getValue: (CT_ c) => c.isCanonical,
69767038
renderVariable: (CT_ c, Property<CT_> self,
69777039
List<String> remainingNames) =>
69787040
self.renderSimpleVariable(c, remainingNames, 'bool'),
69797041
getBool: (CT_ c) => c.isCanonical == true,
69807042
),
7043+
'isFinal': Property(
7044+
getValue: (CT_ c) => c.isFinal,
7045+
renderVariable: (CT_ c, Property<CT_> self,
7046+
List<String> remainingNames) =>
7047+
self.renderSimpleVariable(c, remainingNames, 'bool'),
7048+
getBool: (CT_ c) => c.isFinal == true,
7049+
),
7050+
'isInterface': Property(
7051+
getValue: (CT_ c) => c.isInterface,
7052+
renderVariable: (CT_ c, Property<CT_> self,
7053+
List<String> remainingNames) =>
7054+
self.renderSimpleVariable(c, remainingNames, 'bool'),
7055+
getBool: (CT_ c) => c.isInterface == true,
7056+
),
7057+
'isMixinClass': Property(
7058+
getValue: (CT_ c) => c.isMixinClass,
7059+
renderVariable: (CT_ c, Property<CT_> self,
7060+
List<String> remainingNames) =>
7061+
self.renderSimpleVariable(c, remainingNames, 'bool'),
7062+
getBool: (CT_ c) => c.isMixinClass == true,
7063+
),
7064+
'isSealed': Property(
7065+
getValue: (CT_ c) => c.isSealed,
7066+
renderVariable: (CT_ c, Property<CT_> self,
7067+
List<String> remainingNames) =>
7068+
self.renderSimpleVariable(c, remainingNames, 'bool'),
7069+
getBool: (CT_ c) => c.isSealed == true,
7070+
),
69817071
'modelType': Property(
69827072
getValue: (CT_ c) => c.modelType,
69837073
renderVariable:
@@ -7002,6 +7092,28 @@ class _Renderer_InheritingContainer extends RendererBase<InheritingContainer> {
70027092
parent: r);
70037093
},
70047094
),
7095+
'modifiers': Property(
7096+
getValue: (CT_ c) => c.modifiers,
7097+
renderVariable:
7098+
(CT_ c, Property<CT_> self, List<String> remainingNames) {
7099+
if (remainingNames.isEmpty) {
7100+
return self.getValue(c).toString();
7101+
}
7102+
var name = remainingNames.first;
7103+
var nextProperty =
7104+
_Renderer_String.propertyMap().getValue(name);
7105+
return nextProperty.renderVariable(
7106+
self.getValue(c) as String,
7107+
nextProperty,
7108+
[...remainingNames.skip(1)]);
7109+
},
7110+
isNullValue: (CT_ c) => false,
7111+
renderValue: (CT_ c, RendererBase<CT_> r,
7112+
List<MustachioNode> ast, StringSink sink) {
7113+
_render_String(c.modifiers, ast, r.template, sink,
7114+
parent: r);
7115+
},
7116+
),
70057117
'publicInheritedFields': Property(
70067118
getValue: (CT_ c) => c.publicInheritedFields,
70077119
renderVariable: (CT_ c, Property<CT_> self,
@@ -9377,6 +9489,48 @@ class _Renderer_Mixin extends RendererBase<Mixin> {
93779489
parent: r));
93789490
},
93799491
),
9492+
'isAbstract': Property(
9493+
getValue: (CT_ c) => c.isAbstract,
9494+
renderVariable: (CT_ c, Property<CT_> self,
9495+
List<String> remainingNames) =>
9496+
self.renderSimpleVariable(c, remainingNames, 'bool'),
9497+
getBool: (CT_ c) => c.isAbstract == true,
9498+
),
9499+
'isBase': Property(
9500+
getValue: (CT_ c) => c.isBase,
9501+
renderVariable: (CT_ c, Property<CT_> self,
9502+
List<String> remainingNames) =>
9503+
self.renderSimpleVariable(c, remainingNames, 'bool'),
9504+
getBool: (CT_ c) => c.isBase == true,
9505+
),
9506+
'isFinal': Property(
9507+
getValue: (CT_ c) => c.isFinal,
9508+
renderVariable: (CT_ c, Property<CT_> self,
9509+
List<String> remainingNames) =>
9510+
self.renderSimpleVariable(c, remainingNames, 'bool'),
9511+
getBool: (CT_ c) => c.isFinal == true,
9512+
),
9513+
'isInterface': Property(
9514+
getValue: (CT_ c) => c.isInterface,
9515+
renderVariable: (CT_ c, Property<CT_> self,
9516+
List<String> remainingNames) =>
9517+
self.renderSimpleVariable(c, remainingNames, 'bool'),
9518+
getBool: (CT_ c) => c.isInterface == true,
9519+
),
9520+
'isMixinClass': Property(
9521+
getValue: (CT_ c) => c.isMixinClass,
9522+
renderVariable: (CT_ c, Property<CT_> self,
9523+
List<String> remainingNames) =>
9524+
self.renderSimpleVariable(c, remainingNames, 'bool'),
9525+
getBool: (CT_ c) => c.isMixinClass == true,
9526+
),
9527+
'isSealed': Property(
9528+
getValue: (CT_ c) => c.isSealed,
9529+
renderVariable: (CT_ c, Property<CT_> self,
9530+
List<String> remainingNames) =>
9531+
self.renderSimpleVariable(c, remainingNames, 'bool'),
9532+
getBool: (CT_ c) => c.isSealed == true,
9533+
),
93809534
'kind': Property(
93819535
getValue: (CT_ c) => c.kind,
93829536
renderVariable:

lib/src/model/class.dart

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,42 @@ class Class extends InheritingContainer
1717
@override
1818
final ClassElement element;
1919

20-
Class(this.element, Library library, PackageGraph packageGraph)
21-
: super(library, packageGraph) {
22-
packageGraph.specialClasses.addSpecial(this);
23-
}
24-
2520
@override
2621
late final List<ModelElement> allModelElements = [
2722
...super.allModelElements,
2823
...constructors,
2924
];
3025

26+
@override
27+
late final List<InheritingContainer> inheritanceChain = [
28+
this,
29+
30+
// Caching should make this recursion a little less painful.
31+
for (var container in mixedInTypes.reversed.modelElements)
32+
...container.inheritanceChain,
33+
34+
for (var container in superChain.modelElements)
35+
...container.inheritanceChain,
36+
37+
// Interfaces need to come last, because classes in the superChain might
38+
// implement them even when they aren't mentioned.
39+
...interfaces.expandInheritanceChain,
40+
];
41+
42+
Class(this.element, Library library, PackageGraph packageGraph)
43+
: super(library, packageGraph) {
44+
packageGraph.specialClasses.addSpecial(this);
45+
}
46+
3147
@override
3248
String get fileName => '$name-class.$fileType';
3349

50+
@override
3451
bool get isAbstract => element.isAbstract;
3552

53+
@override
54+
bool get isBase => element.isBase;
55+
3656
bool get isErrorOrException {
3757
bool isError(InterfaceElement element) =>
3858
element.library.isDartCore &&
@@ -44,29 +64,19 @@ class Class extends InheritingContainer
4464
}
4565

4666
@override
47-
String get kind => 'class';
67+
bool get isFinal => element.isFinal;
4868

4969
@override
50-
String get fullkind {
51-
if (isAbstract) return 'abstract $kind';
52-
return super.fullkind;
53-
}
70+
bool get isInterface => element.isInterface;
5471

5572
@override
56-
late final List<InheritingContainer> inheritanceChain = [
57-
this,
73+
bool get isMixinClass => element.isMixinClass;
5874

59-
// Caching should make this recursion a little less painful.
60-
for (var container in mixedInTypes.reversed.modelElements)
61-
...container.inheritanceChain,
62-
63-
for (var container in superChain.modelElements)
64-
...container.inheritanceChain,
75+
@override
76+
bool get isSealed => element.isSealed;
6577

66-
// Interfaces need to come last, because classes in the superChain might
67-
// implement them even when they aren't mentioned.
68-
...interfaces.expandInheritanceChain,
69-
];
78+
@override
79+
String get kind => 'class';
7080

7181
@override
7282
String get relationshipsClass => 'clazz-relationships';

lib/src/model/enum.dart

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import 'package:dartdoc/src/model/model.dart';
88
import 'package:dartdoc/src/model_utils.dart' as model_utils;
99
import 'package:dartdoc/src/render/enum_field_renderer.dart';
1010

11+
/// The [Enum] class only inherits from [InheritingContainer] because declared
12+
/// `enum`s inherit methods from [Object]. It can't actually participate
13+
/// meaningfully in other inheritance or have class modifiers.
1114
class Enum extends InheritingContainer
1215
with Constructable, TypeImplementing, MixedInTypes {
1316
@override
@@ -47,6 +50,21 @@ class Enum extends InheritingContainer
4750

4851
@override
4952
bool get hasPublicEnumValues => publicEnumValues.isNotEmpty;
53+
54+
@override
55+
bool get isAbstract => false;
56+
57+
@override
58+
bool get isBase => false;
59+
60+
@override
61+
bool get isInterface => false;
62+
63+
@override
64+
bool get isMixinClass => false;
65+
66+
@override
67+
bool get isSealed => false;
5068
}
5169

5270
/// A field specific to an enum's values.

0 commit comments

Comments
 (0)