forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotation.dart
82 lines (66 loc) · 2.73 KB
/
annotation.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
// Copyright (c) 2021, 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 'dart:convert';
import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/attribute.dart';
import 'package:dartdoc/src/model/class.dart';
import 'package:dartdoc/src/model/getter_setter_combo.dart';
import 'package:dartdoc/src/model/library.dart';
import 'package:dartdoc/src/model/package_graph.dart';
/// Represents a Dart annotation, attached to an element in the source code with
/// `@`.
final class Annotation extends Attribute {
final ElementAnnotation _annotation;
final Library _library;
final PackageGraph _packageGraph;
Annotation(this._annotation, this._library, this._packageGraph)
: super(_annotation.element!.name!);
@override
String get linkedNameWithParameters {
var source = _annotation.toSource();
var startIndex = source.indexOf('(');
// TODO(srawlins): Attempt to revive constructor arguments in an annotation,
// akin to source_gen's Reviver, in order to link to inner components. For
// example, in `@Foo(const Bar(), baz: <Baz>[Baz.one, Baz.two])`, link to
// `Foo`, `Bar`, `Baz`, `Baz.one`, and `Baz.two`.
var parameterText =
source.substring(startIndex == -1 ? source.length : startIndex);
return '@$linkedName${const HtmlEscape().convert(parameterText)}';
}
@override
String get linkedName => _annotation.element is PropertyAccessorElement
? _packageGraph.getModelForElement(_annotation.element!).linkedName
// TODO(jcollins-g): consider linking to constructor instead of type?
: _modelType.linkedName;
late final ElementType _modelType = switch (_annotation.element) {
ConstructorElement(:var returnType) =>
_packageGraph.getTypeFor(returnType, _library),
PropertyAccessorElement(:var variable2?) =>
(_packageGraph.getModelForElement(variable2) as GetterSetterCombo)
.modelType,
_ => throw StateError(
'non-callable element used as annotation?: ${_annotation.element}')
};
@override
bool get isPublic {
final modelType = _modelType;
if (!modelType.isPublic) {
return false;
}
if (modelType is! DefinedElementType) {
return false;
}
var modelElement = modelType.modelElement;
return modelElement is Class &&
_packageGraph.isAnnotationVisible(modelElement);
}
@override
bool operator ==(Object other) =>
other is Annotation && other._annotation == _annotation;
@override
int get hashCode => _annotation.hashCode;
@override
String get cssClassName => '';
}