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

Add support and tests for class modifiers #3354

Merged
merged 15 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from 13 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
198 changes: 176 additions & 22 deletions lib/src/generator/templates.runtime_renderers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1673,28 +1673,6 @@ class _Renderer_Class extends RendererBase<Class> {
parent: r);
},
),
'fullkind': Property(
getValue: (CT_ c) => c.fullkind,
renderVariable:
(CT_ c, Property<CT_> self, List<String> remainingNames) {
if (remainingNames.isEmpty) {
return self.getValue(c).toString();
}
var name = remainingNames.first;
var nextProperty =
_Renderer_String.propertyMap().getValue(name);
return nextProperty.renderVariable(
self.getValue(c) as String,
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_String(c.fullkind, ast, r.template, sink,
parent: r);
},
),
'inheritanceChain': Property(
getValue: (CT_ c) => c.inheritanceChain,
renderVariable: (CT_ c, Property<CT_> self,
Expand All @@ -1715,13 +1693,48 @@ class _Renderer_Class extends RendererBase<Class> {
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isAbstract == true,
),
'isBase': Property(
getValue: (CT_ c) => c.isBase,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isBase == true,
),
'isErrorOrException': Property(
getValue: (CT_ c) => c.isErrorOrException,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isErrorOrException == true,
),
'isFinal': Property(
getValue: (CT_ c) => c.isFinal,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isFinal == true,
),
'isInterface': Property(
getValue: (CT_ c) => c.isInterface,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isInterface == true,
),
'isMixinClass': Property(
getValue: (CT_ c) => c.isMixinClass,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isMixinClass == true,
),
'isSealed': Property(
getValue: (CT_ c) => c.isSealed,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isSealed == true,
),
'kind': Property(
getValue: (CT_ c) => c.kind,
renderVariable:
Expand Down Expand Up @@ -4358,6 +4371,41 @@ class _Renderer_Enum extends RendererBase<Enum> {
parent: r));
},
),
'isAbstract': Property(
getValue: (CT_ c) => c.isAbstract,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isAbstract == true,
),
'isBase': Property(
getValue: (CT_ c) => c.isBase,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isBase == true,
),
'isInterface': Property(
getValue: (CT_ c) => c.isInterface,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isInterface == true,
),
'isMixinClass': Property(
getValue: (CT_ c) => c.isMixinClass,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isMixinClass == true,
),
'isSealed': Property(
getValue: (CT_ c) => c.isSealed,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isSealed == true,
),
'kind': Property(
getValue: (CT_ c) => c.kind,
renderVariable:
Expand Down Expand Up @@ -6971,13 +7019,55 @@ class _Renderer_InheritingContainer extends RendererBase<InheritingContainer> {
_render_Operator(e, ast, r.template, sink, parent: r));
},
),
'isAbstract': Property(
getValue: (CT_ c) => c.isAbstract,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isAbstract == true,
),
'isBase': Property(
getValue: (CT_ c) => c.isBase,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isBase == true,
),
'isCanonical': Property(
getValue: (CT_ c) => c.isCanonical,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isCanonical == true,
),
'isFinal': Property(
getValue: (CT_ c) => c.isFinal,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isFinal == true,
),
'isInterface': Property(
getValue: (CT_ c) => c.isInterface,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isInterface == true,
),
'isMixinClass': Property(
getValue: (CT_ c) => c.isMixinClass,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isMixinClass == true,
),
'isSealed': Property(
getValue: (CT_ c) => c.isSealed,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isSealed == true,
),
'modelType': Property(
getValue: (CT_ c) => c.modelType,
renderVariable:
Expand All @@ -7002,6 +7092,28 @@ class _Renderer_InheritingContainer extends RendererBase<InheritingContainer> {
parent: r);
},
),
'modifiers': Property(
getValue: (CT_ c) => c.modifiers,
renderVariable:
(CT_ c, Property<CT_> self, List<String> remainingNames) {
if (remainingNames.isEmpty) {
return self.getValue(c).toString();
}
var name = remainingNames.first;
var nextProperty =
_Renderer_String.propertyMap().getValue(name);
return nextProperty.renderVariable(
self.getValue(c) as String,
nextProperty,
[...remainingNames.skip(1)]);
},
isNullValue: (CT_ c) => false,
renderValue: (CT_ c, RendererBase<CT_> r,
List<MustachioNode> ast, StringSink sink) {
_render_String(c.modifiers, ast, r.template, sink,
parent: r);
},
),
'publicInheritedFields': Property(
getValue: (CT_ c) => c.publicInheritedFields,
renderVariable: (CT_ c, Property<CT_> self,
Expand Down Expand Up @@ -9377,6 +9489,48 @@ class _Renderer_Mixin extends RendererBase<Mixin> {
parent: r));
},
),
'isAbstract': Property(
getValue: (CT_ c) => c.isAbstract,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isAbstract == true,
),
'isBase': Property(
getValue: (CT_ c) => c.isBase,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isBase == true,
),
'isFinal': Property(
getValue: (CT_ c) => c.isFinal,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isFinal == true,
),
'isInterface': Property(
getValue: (CT_ c) => c.isInterface,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isInterface == true,
),
'isMixinClass': Property(
getValue: (CT_ c) => c.isMixinClass,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isMixinClass == true,
),
'isSealed': Property(
getValue: (CT_ c) => c.isSealed,
renderVariable: (CT_ c, Property<CT_> self,
List<String> remainingNames) =>
self.renderSimpleVariable(c, remainingNames, 'bool'),
getBool: (CT_ c) => c.isSealed == true,
),
'kind': Property(
getValue: (CT_ c) => c.kind,
renderVariable:
Expand Down
54 changes: 32 additions & 22 deletions lib/src/model/class.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,42 @@ class Class extends InheritingContainer
@override
final ClassElement element;

Class(this.element, Library library, PackageGraph packageGraph)
: super(library, packageGraph) {
packageGraph.specialClasses.addSpecial(this);
}

@override
late final List<ModelElement> allModelElements = [
...super.allModelElements,
...constructors,
];

@override
late final List<InheritingContainer> inheritanceChain = [
this,

// Caching should make this recursion a little less painful.
for (var container in mixedInTypes.reversed.modelElements)
...container.inheritanceChain,

for (var container in superChain.modelElements)
...container.inheritanceChain,

// Interfaces need to come last, because classes in the superChain might
// implement them even when they aren't mentioned.
...interfaces.expandInheritanceChain,
];

Class(this.element, Library library, PackageGraph packageGraph)
: super(library, packageGraph) {
packageGraph.specialClasses.addSpecial(this);
}

@override
String get fileName => '$name-class.$fileType';

@override
bool get isAbstract => element.isAbstract;

@override
bool get isBase => element.isBase;

bool get isErrorOrException {
bool isError(InterfaceElement element) =>
element.library.isDartCore &&
Expand All @@ -44,29 +64,19 @@ class Class extends InheritingContainer
}

@override
String get kind => 'class';
bool get isFinal => element.isFinal;

@override
String get fullkind {
if (isAbstract) return 'abstract $kind';
return super.fullkind;
}
bool get isInterface => element.isInterface;

@override
late final List<InheritingContainer> inheritanceChain = [
this,
bool get isMixinClass => element.isMixinClass;

// Caching should make this recursion a little less painful.
for (var container in mixedInTypes.reversed.modelElements)
...container.inheritanceChain,

for (var container in superChain.modelElements)
...container.inheritanceChain,
@override
bool get isSealed => element.isSealed;

// Interfaces need to come last, because classes in the superChain might
// implement them even when they aren't mentioned.
...interfaces.expandInheritanceChain,
];
@override
String get kind => 'class';

@override
String get relationshipsClass => 'clazz-relationships';
Expand Down
18 changes: 18 additions & 0 deletions lib/src/model/enum.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/model_utils.dart' as model_utils;
import 'package:dartdoc/src/render/enum_field_renderer.dart';

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

@override
bool get hasPublicEnumValues => publicEnumValues.isNotEmpty;

@override
bool get isAbstract => false;

@override
bool get isBase => false;

@override
bool get isInterface => false;

@override
bool get isMixinClass => false;

@override
bool get isSealed => false;
}

/// A field specific to an enum's values.
Expand Down
Loading