Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
44 changes: 39 additions & 5 deletions lib/src/model/annotation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:convert';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:dartdoc/src/model/attribute.dart';
import 'package:dartdoc/src/model/class.dart';
Expand Down Expand Up @@ -36,14 +37,47 @@ final class Annotation extends Attribute {
var parameterText =
source.substring(startIndex == -1 ? source.length : startIndex);

return '@$linkedName${const HtmlEscape().convert(parameterText)}';
var escapedParameterText = const HtmlEscape().convert(parameterText);
return '@$linkedName$_linkedTypeArguments$escapedParameterText';
}

@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;
String get linkedName => switch (_annotation.element) {
PropertyAccessorElement element =>
_packageGraph.getModelForElement(element).linkedName,
ConstructorElement element =>
_packageGraph.getModelForElement(element).linkedName,
_ => _modelType.linkedName
};

/// The linked type argument text, with `<` and `>`, if there are any type
/// arguments.
String get _linkedTypeArguments {
if (_annotation.element is PropertyAccessorElement) {
return '';
}

var type = _modelType.type;
if (type is! InterfaceType) {
return '';
}

var typeArguments = type.typeArguments;
if (typeArguments.isEmpty) {
return '';
}

var buffer = StringBuffer();
buffer.write('&lt;');
for (var t in typeArguments) {
buffer.write(_packageGraph.getTypeFor(t, _library).linkedName);
if (t != typeArguments.last) {
buffer.write(', ');
}
}
buffer.write('&gt;');
return buffer.toString();
}

late final ElementType _modelType = switch (_annotation.element) {
ConstructorElement(:var returnType) =>
Expand Down
8 changes: 6 additions & 2 deletions lib/src/model/constructor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ class Constructor extends ModelElement with ContainerMember, TypeParameters {
@override
Kind get kind => Kind.constructor;

late final Callable modelType =
getTypeFor(element.type, library) as Callable;
late final Callable modelType = getTypeFor(element.type, library) as Callable;

@override
String get name =>
Expand All @@ -102,6 +101,11 @@ class Constructor extends ModelElement with ContainerMember, TypeParameters {
// code there and elsewhere with simple references to the name.
'${enclosingElement.name}.${element.name}';

@override
String get displayName => isUnnamedConstructor
? enclosingElement.name
: '${enclosingElement.name}.${element.name}';

@override
String get nameWithGenerics {
var constructorName = element.name!;
Expand Down
42 changes: 32 additions & 10 deletions test/annotations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ int value = 0;
var annotation = valueVariable.annotations.single;
expect(
annotation.linkedName,
'<a href="$dartCoreUrlPrefix/Deprecated-class.html">Deprecated</a>',
'<a href="$dartCoreUrlPrefix/Deprecated/Deprecated.html">Deprecated</a>',
);
expect(
annotation.linkedNameWithParameters,
'@<a href="$dartCoreUrlPrefix/Deprecated-class.html">Deprecated</a>'
'@<a href="$dartCoreUrlPrefix/Deprecated/Deprecated.html">Deprecated</a>'
'(&#39;text&#39;)',
);
}
Expand Down Expand Up @@ -97,16 +97,40 @@ int value = 0;
var annotation = valueVariable.annotations.single;
expect(
annotation.linkedName,
'<a href="${htmlBasePlaceholder}annotations/MyAnnotation-class.html">'
'<a href="${htmlBasePlaceholder}annotations/MyAnnotation/MyAnnotation.html">'
'MyAnnotation</a>',
);
expect(
annotation.linkedNameWithParameters,
'@<a href="${htmlBasePlaceholder}annotations/MyAnnotation-class.html">'
'@<a href="${htmlBasePlaceholder}annotations/MyAnnotation/MyAnnotation.html">'
'MyAnnotation</a>(true)',
);
}

void test_locallyDeclaredConstructorCall_named() async {
var library = await bootPackageWithLibrary('''
class MyAnnotation {
const MyAnnotation.named(bool b);
}

@MyAnnotation.named(true)
int value = 0;
''');
var valueVariable = library.properties.named('value');
expect(valueVariable.hasAnnotations, true);
var annotation = valueVariable.annotations.single;
expect(
annotation.linkedName,
'<a href="${htmlBasePlaceholder}annotations/MyAnnotation/MyAnnotation.named.html">'
'MyAnnotation.named</a>',
);
expect(
annotation.linkedNameWithParameters,
'@<a href="${htmlBasePlaceholder}annotations/MyAnnotation/MyAnnotation.named.html">'
'MyAnnotation.named</a>(true)',
);
}

void test_genericConstructorCall() async {
var library = await bootPackageWithLibrary('''
class Ann<T> {
Expand All @@ -122,15 +146,13 @@ int value = 0;
var annotation = valueVariable.annotations.single;
expect(
annotation.linkedName,
'<a href="${htmlBasePlaceholder}annotations/Ann-class.html">Ann</a>'
'<span class="signature">&lt;<wbr><span class="type-parameter">'
'<a href="$dartCoreUrlPrefix/bool-class.html">bool</a></span>&gt;</span>',
'<a href="${htmlBasePlaceholder}annotations/Ann/Ann.html">Ann</a>',
);
expect(
annotation.linkedNameWithParameters,
'@<a href="${htmlBasePlaceholder}annotations/Ann-class.html">Ann</a>'
'<span class="signature">&lt;<wbr><span class="type-parameter">'
'<a href="$dartCoreUrlPrefix/bool-class.html">bool</a></span>&gt;</span>(true)',
'@<a href="${htmlBasePlaceholder}annotations/Ann/Ann.html">Ann</a>'
'&lt;<a href="$dartCoreUrlPrefix/bool-class.html">bool</a>&gt;'
'(true)',
);
}
}
4 changes: 3 additions & 1 deletion test/end2end/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ void main() async {
test('Verify type arguments on annotations renders, including parameters',
() {
var ab0 =
'@<a href="%%__HTMLBASE_dartdoc_internal__%%generic_metadata/A-class.html">A</a><span class="signature">&lt;<wbr><span class="type-parameter"><a href="%%__HTMLBASE_dartdoc_internal__%%generic_metadata/B.html">B</a></span>&gt;</span>(0)';
'@<a href="%%__HTMLBASE_dartdoc_internal__%%generic_metadata/A/A.html">A</a>'
'&lt;<a href="%%__HTMLBASE_dartdoc_internal__%%generic_metadata/B.html">B</a>&gt;'
'(0)';

expect(genericMetadata.annotations.first.linkedNameWithParameters,
equals(ab0));
Expand Down
2 changes: 1 addition & 1 deletion test/enums_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ enum E { one, two, three }
expect(eEnum.hasAnnotations, true);
expect(eEnum.annotations, hasLength(1));
expect(eEnum.annotations.single.linkedName,
'<a href="$linkPrefix/C-class.html">C</a>');
'<a href="$linkPrefix/C/C.html">C</a>');
}

void test_hasDocComment() async {
Expand Down
2 changes: 1 addition & 1 deletion test/templates/class_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ class C {

htmlLines.expectMainContentContainsAllInOrder([
matches('<h2>Constructors</h2>'),
matches('<a href="../lib/C/C.html">C.new</a>'),
matches('<a href="../lib/C/C.html">C</a>'),
matches('An unnamed constructor.'),
]);
}
Expand Down
2 changes: 1 addition & 1 deletion test/templates/enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ extension Ext<T> on E<T> {}
matches('<dt>Annotations</dt>'),
matches('<ul class="annotation-list eNum-relationships">'),
matches(
r'<li>@<a href="../lib/C-class.html">C</a>\(&#39;message&#39;\)</li>'),
r'<li>@<a href="../lib/C/C.html">C</a>&lt;dynamic&gt;\(&#39;message&#39;\)</li>'),
matches('</ul>'),
]);
});
Expand Down
2 changes: 1 addition & 1 deletion test/templates/extension_type_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ extension type One(int it) {

htmlLines.expectMainContentContainsAllInOrder([
matches('<h2>Constructors</h2>'),
matches('<a href="../lib/One/One.html">One.new</a>'),
matches('<a href="../lib/One/One.html">One</a>'),
matches('<a href="../lib/One/One.named.html">One.named</a>'),
matches('A named constructor.'),
]);
Expand Down
2 changes: 1 addition & 1 deletion test/templates/field_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class A {
matches('<ol class="annotation-list">'),
matches('<li>@deprecated</li>'),
matches(
r'<li>@<a href="../../lib/A-class.html">A</a>\(&#39;message&#39;\)</li>'),
r'<li>@<a href="../../lib/A/A.html">A</a>\(&#39;message&#39;\)</li>'),
matches('</ol>'),
],
);
Expand Down
3 changes: 1 addition & 2 deletions test/templates/method_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';

import '../src/utils.dart';

import 'template_test_base.dart';

void main() async {
Expand Down Expand Up @@ -53,7 +52,7 @@ class C {
matches('<ol class="annotation-list">'),
matches('<li>@deprecated</li>'),
matches(
r'<li>@<a href="../../lib/A-class.html">A</a>\(&#39;message&#39;\)</li>'),
r'<li>@<a href="../../lib/A/A.html">A</a>\(&#39;message&#39;\)</li>'),
matches('</ol>'),
],
);
Expand Down