Skip to content

Commit 3e9bb48

Browse files
authored
Simplify Annotation and Attribute classes (#3718)
* Privatize many fields on Annotation: `annotation`, `packageGraph`, `modelType`. * Convert `linkedNameWithParameters` to a getter. * Inline the `parameterText` field into its only call site, `linkedNameWithParameters`. * Make Annotation class and _BuiltInAttribute class final.
1 parent b001a15 commit 3e9bb48

File tree

3 files changed

+32
-157
lines changed

3 files changed

+32
-157
lines changed

lib/src/generator/templates.runtime_renderers.dart

-126
Original file line numberDiff line numberDiff line change
@@ -425,20 +425,6 @@ class _Renderer_Annotation extends RendererBase<Annotation> {
425425
CT_,
426426
() => {
427427
..._Renderer_Attribute.propertyMap<CT_>(),
428-
'annotation': Property(
429-
getValue: (CT_ c) => c.annotation,
430-
renderVariable: (CT_ c, Property<CT_> self,
431-
List<String> remainingNames) =>
432-
self.renderSimpleVariable(
433-
c, remainingNames, 'ElementAnnotation'),
434-
isNullValue: (CT_ c) => false,
435-
renderValue: (CT_ c, RendererBase<CT_> r,
436-
List<MustachioNode> ast, StringSink sink) {
437-
renderSimple(c.annotation, ast, r.template, sink,
438-
parent: r,
439-
getters: _invisibleGetters['ElementAnnotation']!);
440-
},
441-
),
442428
'cssClassName': Property(
443429
getValue: (CT_ c) => c.cssClassName,
444430
renderVariable:
@@ -480,28 +466,6 @@ class _Renderer_Annotation extends RendererBase<Annotation> {
480466
self.renderSimpleVariable(c, remainingNames, 'bool'),
481467
getBool: (CT_ c) => c.isPublic,
482468
),
483-
'library': Property(
484-
getValue: (CT_ c) => c.library,
485-
renderVariable:
486-
(CT_ c, Property<CT_> self, List<String> remainingNames) {
487-
if (remainingNames.isEmpty) {
488-
return self.getValue(c).toString();
489-
}
490-
var name = remainingNames.first;
491-
var nextProperty =
492-
_Renderer_Library.propertyMap().getValue(name);
493-
return nextProperty.renderVariable(
494-
self.getValue(c) as Library,
495-
nextProperty,
496-
[...remainingNames.skip(1)]);
497-
},
498-
isNullValue: (CT_ c) => false,
499-
renderValue: (CT_ c, RendererBase<CT_> r,
500-
List<MustachioNode> ast, StringSink sink) {
501-
_render_Library(c.library, ast, r.template, sink,
502-
parent: r);
503-
},
504-
),
505469
'linkedName': Property(
506470
getValue: (CT_ c) => c.linkedName,
507471
renderVariable:
@@ -547,63 +511,6 @@ class _Renderer_Annotation extends RendererBase<Annotation> {
547511
parent: r);
548512
},
549513
),
550-
'modelType': Property(
551-
getValue: (CT_ c) => c.modelType,
552-
renderVariable:
553-
(CT_ c, Property<CT_> self, List<String> remainingNames) {
554-
if (remainingNames.isEmpty) {
555-
return self.getValue(c).toString();
556-
}
557-
var name = remainingNames.first;
558-
var nextProperty =
559-
_Renderer_ElementType.propertyMap().getValue(name);
560-
return nextProperty.renderVariable(
561-
self.getValue(c) as ElementType,
562-
nextProperty,
563-
[...remainingNames.skip(1)]);
564-
},
565-
isNullValue: (CT_ c) => false,
566-
renderValue: (CT_ c, RendererBase<CT_> r,
567-
List<MustachioNode> ast, StringSink sink) {
568-
_render_ElementType(c.modelType, ast, r.template, sink,
569-
parent: r);
570-
},
571-
),
572-
'packageGraph': Property(
573-
getValue: (CT_ c) => c.packageGraph,
574-
renderVariable: (CT_ c, Property<CT_> self,
575-
List<String> remainingNames) =>
576-
self.renderSimpleVariable(
577-
c, remainingNames, 'PackageGraph'),
578-
isNullValue: (CT_ c) => false,
579-
renderValue: (CT_ c, RendererBase<CT_> r,
580-
List<MustachioNode> ast, StringSink sink) {
581-
renderSimple(c.packageGraph, ast, r.template, sink,
582-
parent: r, getters: _invisibleGetters['PackageGraph']!);
583-
},
584-
),
585-
'parameterText': Property(
586-
getValue: (CT_ c) => c.parameterText,
587-
renderVariable:
588-
(CT_ c, Property<CT_> self, List<String> remainingNames) {
589-
if (remainingNames.isEmpty) {
590-
return self.getValue(c).toString();
591-
}
592-
var name = remainingNames.first;
593-
var nextProperty =
594-
_Renderer_String.propertyMap().getValue(name);
595-
return nextProperty.renderVariable(
596-
self.getValue(c) as String,
597-
nextProperty,
598-
[...remainingNames.skip(1)]);
599-
},
600-
isNullValue: (CT_ c) => false,
601-
renderValue: (CT_ c, RendererBase<CT_> r,
602-
List<MustachioNode> ast, StringSink sink) {
603-
_render_String(c.parameterText, ast, r.template, sink,
604-
parent: r);
605-
},
606-
),
607514
}) as Map<String, Property<CT_>>;
608515

609516
_Renderer_Annotation(Annotation context, RendererBase<Object>? parent,
@@ -16180,39 +16087,6 @@ const _invisibleGetters = {
1618016087
'sinceSdkVersion',
1618116088
'source'
1618216089
},
16183-
'ElementAnnotation': {
16184-
'constantEvaluationErrors',
16185-
'element',
16186-
'hashCode',
16187-
'isAlwaysThrows',
16188-
'isDeprecated',
16189-
'isDoNotStore',
16190-
'isFactory',
16191-
'isImmutable',
16192-
'isInternal',
16193-
'isIsTest',
16194-
'isIsTestGroup',
16195-
'isJS',
16196-
'isLiteral',
16197-
'isMustBeOverridden',
16198-
'isMustCallSuper',
16199-
'isNonVirtual',
16200-
'isOptionalTypeArgs',
16201-
'isOverride',
16202-
'isProtected',
16203-
'isProxy',
16204-
'isRedeclare',
16205-
'isReopen',
16206-
'isRequired',
16207-
'isSealed',
16208-
'isTarget',
16209-
'isUseResult',
16210-
'isVisibleForOverriding',
16211-
'isVisibleForTemplate',
16212-
'isVisibleForTesting',
16213-
'isVisibleOutsideTemplate',
16214-
'runtimeType'
16215-
},
1621616090
'EnumElement': {
1621716091
'augmentation',
1621816092
'augmentationTarget',

lib/src/model/annotation.dart

+31-30
Original file line numberDiff line numberDiff line change
@@ -14,49 +14,50 @@ import 'package:dartdoc/src/model/package_graph.dart';
1414

1515
/// Represents a Dart annotation, attached to an element in the source code with
1616
/// `@`.
17-
class Annotation extends Attribute {
18-
final ElementAnnotation annotation;
19-
final Library library;
17+
final class Annotation extends Attribute {
18+
final ElementAnnotation _annotation;
2019

21-
final PackageGraph packageGraph;
20+
final Library _library;
2221

23-
Annotation(this.annotation, this.library, this.packageGraph)
24-
: super(annotation.element!.name!);
22+
final PackageGraph _packageGraph;
23+
24+
Annotation(this._annotation, this._library, this._packageGraph)
25+
: super(_annotation.element!.name!);
2526

2627
@override
27-
late final String linkedNameWithParameters =
28-
'@$linkedName${HtmlEscape().convert(parameterText)}';
28+
String get linkedNameWithParameters {
29+
var source = _annotation.toSource();
30+
var startIndex = source.indexOf('(');
31+
32+
// TODO(srawlins): Attempt to revive constructor arguments in an annotation,
33+
// akin to source_gen's Reviver, in order to link to inner components. For
34+
// example, in `@Foo(const Bar(), baz: <Baz>[Baz.one, Baz.two])`, link to
35+
// `Foo`, `Bar`, `Baz`, `Baz.one`, and `Baz.two`.
36+
var parameterText =
37+
source.substring(startIndex == -1 ? source.length : startIndex);
38+
39+
return '@$linkedName${const HtmlEscape().convert(parameterText)}';
40+
}
2941

3042
@override
31-
String get linkedName => annotation.element is PropertyAccessorElement
32-
? packageGraph.getModelForElement(annotation.element!).linkedName
43+
String get linkedName => _annotation.element is PropertyAccessorElement
44+
? _packageGraph.getModelForElement(_annotation.element!).linkedName
3345
// TODO(jcollins-g): consider linking to constructor instead of type?
34-
: modelType.linkedName;
46+
: _modelType.linkedName;
3547

36-
late final ElementType modelType = switch (annotation.element) {
48+
late final ElementType _modelType = switch (_annotation.element) {
3749
ConstructorElement(:var returnType) =>
38-
packageGraph.getTypeFor(returnType, library),
50+
_packageGraph.getTypeFor(returnType, _library),
3951
PropertyAccessorElement(:var variable) =>
40-
(packageGraph.getModelForElement(variable) as GetterSetterCombo)
52+
(_packageGraph.getModelForElement(variable) as GetterSetterCombo)
4153
.modelType,
4254
_ => throw StateError(
43-
'non-callable element used as annotation?: ${annotation.element}')
55+
'non-callable element used as annotation?: ${_annotation.element}')
4456
};
4557

46-
// TODO(srawlins): Attempt to revive constructor arguments in an annotation,
47-
// akin to source_gen's Reviver, in order to link to inner components. For
48-
// example, in `@Foo(const Bar(), baz: <Baz>[Baz.one, Baz.two])`, link to
49-
// `Foo`, `Bar`, `Baz`, `Baz.one`, and `Baz.two`.
50-
/// The textual representation of the argument(s) supplied to the annotation.
51-
String get parameterText {
52-
var source = annotation.toSource();
53-
var startIndex = source.indexOf('(');
54-
return source.substring(startIndex == -1 ? source.length : startIndex);
55-
}
56-
5758
@override
5859
bool get isPublic {
59-
final modelType = this.modelType;
60+
final modelType = _modelType;
6061
if (!modelType.isPublic) {
6162
return false;
6263
}
@@ -66,15 +67,15 @@ class Annotation extends Attribute {
6667

6768
var modelElement = modelType.modelElement;
6869
return modelElement is Class &&
69-
packageGraph.isAnnotationVisible(modelElement);
70+
_packageGraph.isAnnotationVisible(modelElement);
7071
}
7172

7273
@override
7374
bool operator ==(Object other) =>
74-
other is Annotation && other.annotation == annotation;
75+
other is Annotation && other._annotation == _annotation;
7576

7677
@override
77-
int get hashCode => annotation.hashCode;
78+
int get hashCode => _annotation.hashCode;
7879

7980
@override
8081
String get cssClassName => '';

lib/src/model/attribute.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract class Attribute implements Privacy {
4949
static const overrideSetter = Attribute._builtIn('override-setter', 3);
5050
}
5151

52-
class _BuiltInAttribute extends Attribute {
52+
final class _BuiltInAttribute extends Attribute {
5353
const _BuiltInAttribute(super.name, super.sortGroup);
5454

5555
@override

0 commit comments

Comments
 (0)