Skip to content

Introduce basic generic type alias support for dartdoc #2558

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

Merged
merged 12 commits into from
Mar 8, 2021
2 changes: 1 addition & 1 deletion lib/src/element_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ abstract class DefinedElementType extends ElementType {

@override
List<Parameter> get parameters =>
element.canHaveParameters ? element.parameters : [];
element.isCallable ? element.parameters : [];

ModelElement get returnElement => element;
ElementType _returnType;
Expand Down
8 changes: 8 additions & 0 deletions lib/src/generator/templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ const _partials_html = <String>[
'source_code',
'source_link',
'sidebar_for_library',
'type',
'type_multiline',
'typedef',
'typedef_multiline',
'accessor_getter',
'accessor_setter',
];
Expand All @@ -66,6 +70,10 @@ const _partials_md = <String>[
'property',
'source_code',
'source_link',
'type',
'type_multiline',
'typedef',
'typedef_multiline',
];

abstract class _TemplatesLoader {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/model/getter_setter_combo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ mixin GetterSetterCombo on ModelElement {
}

@override
bool get canHaveParameters => hasSetter;
bool get isCallable => hasSetter;

@override
List<Parameter> get parameters => setter.parameters;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/model/library.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
compilationUnit.enums,
compilationUnit.extensions,
compilationUnit.functions,
compilationUnit.functionTypeAliases,
compilationUnit.mixins,
compilationUnit.topLevelVariables,
compilationUnit.typeAliases,
compilationUnit.types,
]);
}
Expand Down Expand Up @@ -493,7 +493,7 @@ class Library extends ModelElement with Categorization, TopLevelContainer {
@override
List<Typedef> get typedefs {
_typedefs ??= _exportedAndLocalElements
.whereType<FunctionTypeAliasElement>()
.whereType<TypeAliasElement>()
.map((e) => ModelElement.from(e, this, packageGraph) as Typedef)
.toList(growable: false);
return _typedefs;
Expand Down
20 changes: 11 additions & 9 deletions lib/src/model/model_element.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ library dartdoc.models;
import 'dart:convert';

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart' show FunctionType;
import 'package:analyzer/source/line_info.dart';
import 'package:analyzer/src/dart/element/element.dart';
import 'package:analyzer/src/dart/element/member.dart'
Expand Down Expand Up @@ -333,7 +334,10 @@ abstract class ModelElement extends Canonicalization
assert(e.enclosingElement.name != '');
return ModelFunctionTypedef(e, library, packageGraph);
}
if (e is FunctionTypeAliasElement) {
if (e is TypeAliasElement) {
if (e.aliasedType is FunctionType) {
return FunctionTypedef(e, library, packageGraph);
}
return Typedef(e, library, packageGraph);
}
if (e is ConstructorElement) {
Expand Down Expand Up @@ -543,10 +547,8 @@ abstract class ModelElement extends Canonicalization
return allFeatures.join(', ');
}

bool get canHaveParameters =>
element is ExecutableElement ||
element is FunctionTypedElement ||
element is FunctionTypeAliasElement;
bool get isCallable =>
element is FunctionTypedElement || element is FunctionTypeAliasElement;

ModelElement buildCanonicalModelElement() {
Container preferredClass;
Expand Down Expand Up @@ -998,9 +1000,9 @@ abstract class ModelElement extends Canonicalization
}
} else if (element is ClassElement) {
_modelType = ElementType.from(element.thisType, library, packageGraph);
} else if (element is FunctionTypeAliasElement) {
} else if (element is TypeAliasElement) {
_modelType =
ElementType.from(element.function.type, library, packageGraph);
ElementType.from(element.aliasedType, library, packageGraph);
} else if (element is FunctionTypedElement) {
_modelType = ElementType.from(element.type, library, packageGraph);
} else if (element is ParameterElement) {
Expand Down Expand Up @@ -1050,7 +1052,7 @@ abstract class ModelElement extends Canonicalization
(this as GetterSetterCombo).setter != null) {
newParameters.addAll((this as GetterSetterCombo).setter.parameters);
} else {
if (canHaveParameters) newParameters.addAll(parameters);
if (isCallable) newParameters.addAll(parameters);
}
while (newParameters.isNotEmpty) {
recursedParameters.addAll(newParameters);
Expand All @@ -1070,7 +1072,7 @@ abstract class ModelElement extends Canonicalization
path.Context get pathContext => packageGraph.resourceProvider.pathContext;

List<Parameter> get parameters {
if (!canHaveParameters) {
if (!isCallable) {
throw StateError('$element cannot have parameters');
}

Expand Down
50 changes: 32 additions & 18 deletions lib/src/model/typedef.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:analyzer/dart/element/element.dart';
import 'package:dartdoc/src/element_type.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/render/typedef_renderer.dart';

class Typedef extends ModelElement
with TypeParameters, Categorization
implements EnclosedElement {
Typedef(FunctionTypeAliasElement element, Library library,
PackageGraph packageGraph)
Typedef(TypeAliasElement element, Library library, PackageGraph packageGraph)
: super(element, library, packageGraph);

DartType get aliasedType => element.aliasedType;

@override
TypeAliasElement get element => super.element;

@override
ModelElement get enclosingElement => library;

Expand All @@ -23,12 +27,8 @@ class Typedef extends ModelElement
@override
String get genericParameters => _renderer.renderGenericParameters(this);

List<TypeParameterElement> get genericTypeParameters {
if (element is FunctionTypeAliasElement) {
return (element as FunctionTypeAliasElement).function.typeParameters;
}
return Iterable<TypeParameterElement>.empty();
}
List<TypeParameterElement> get genericTypeParameters =>
element.typeParameters;

@override
String get filePath => '${library.dirName}/$fileName';
Expand All @@ -52,17 +52,31 @@ class Typedef extends ModelElement
String get linkedReturnType => modelType.createLinkedReturnTypeName();

@override
// TODO(jcollins-g): change to FunctionTypeElementType after analyzer 0.41
// ignore: unnecessary_overrides
ElementType get modelType => super.modelType;

FunctionTypeAliasElement get _typedef =>
(element as FunctionTypeAliasElement);

@override
List<TypeParameter> get typeParameters => _typedef.typeParameters.map((f) {
List<TypeParameter> get typeParameters => element.typeParameters.map((f) {
return ModelElement.from(f, library, packageGraph) as TypeParameter;
}).toList();

TypedefRenderer get _renderer => packageGraph.rendererFactory.typedefRenderer;
}

/// A typedef referring to a function type.
class FunctionTypedef extends Typedef {
FunctionTypedef(
TypeAliasElement element, Library library, PackageGraph packageGraph)
: super(element, library, packageGraph);

@override
FunctionType get aliasedType => super.aliasedType;

@override
List<TypeParameterElement> get genericTypeParameters {
var aliasedTypeElement = aliasedType.element;
if (aliasedTypeElement is FunctionTypedElement) {
return aliasedTypeElement.typeParameters;
}
if (aliasedType.typeFormals.isNotEmpty == true) {
return aliasedType.typeFormals;
}
return super.genericTypeParameters;
}
}
1 change: 1 addition & 0 deletions lib/templates/html/_callable_multiline.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
</ol>
</div>
{{/hasAnnotations}}

<span class="returntype">{{{ linkedReturnType }}}</span>
{{>name_summary}}{{{genericParameters}}}(<wbr>{{#hasParameters}}{{{linkedParamsLines}}}{{/hasParameters}})
2 changes: 1 addition & 1 deletion lib/templates/html/_name_summary.html
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{{#isConst}}const {{/isConst}}<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{name}}</span>
{{#isConst}}const {{/isConst}}<span class="name {{#isDeprecated}}deprecated{{/isDeprecated}}">{{name}}</span>
11 changes: 11 additions & 0 deletions lib/templates/html/_type.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<dt id="{{htmlId}}" class="{{ #isInherited }} inherited{{ /isInherited}}">
<span class="name{{#isDeprecated}} deprecated{{/isDeprecated}}">{{{linkedName}}}</span>{{{linkedGenericParameters}}}
=
<span class="returntype parameter">{{{ linkedReturnType }}}</span>
</span>
{{>categorization}}
</dt>
<dd{{ #isInherited }} class="inherited"{{ /isInherited}}>
{{{ oneLineDoc }}} {{{ extendedDocLink }}}
{{>features}}
</dd>
10 changes: 10 additions & 0 deletions lib/templates/html/_type_multiline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{#hasAnnotations}}
<div>
<ol class="annotation-list">
{{#annotations}}
<li>{{{.}}}</li>
{{/annotations}}
</ol>
</div>
{{/hasAnnotations}}
{{>name_summary}}{{{genericParameters}}} = <span class="returntype"> {{{linkedReturnType}}}</span>
6 changes: 6 additions & 0 deletions lib/templates/html/_typedef.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{#isCallable}}
{{>callable}}
{{/isCallable}}
{{^isCallable}}
{{>type}}
{{/isCallable}}
6 changes: 6 additions & 0 deletions lib/templates/html/_typedef_multiline.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{#isCallable}}
{{>callable_multiline}}
{{/isCallable}}
{{^isCallable}}
{{>type_multiline}}
{{/isCallable}}
4 changes: 2 additions & 2 deletions lib/templates/html/category.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ <h2>Typedefs</h2>

<dl class="callables">
{{#publicTypedefsSorted}}
{{>callable}}
{{>typedef}}
{{/publicTypedefsSorted}}
</dl>
</section>
Expand All @@ -126,4 +126,4 @@ <h5>{{self.name}} {{self.kind}}</h5>
{{>sidebar_for_category}}
</div>
<!--/sidebar-offcanvas-right-->
{{>footer}}
{{>footer}}
4 changes: 2 additions & 2 deletions lib/templates/html/library.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ <h2>Enums</h2>
<section class="summary offset-anchor" id="typedefs">
<h2>Typedefs</h2>

<dl class="callables">
<dl>
{{#library.publicTypedefsSorted}}
{{>callable}}
{{>typedef}}
{{/library.publicTypedefsSorted}}
</dl>
</section>
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/html/typedef.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ <h5>{{parent.name}} {{parent.kind}}</h5>

<section class="multi-line-signature">
{{#typeDef}}
{{>callable_multiline}}
{{>typedef_multiline}}
{{/typeDef}}
</section>

Expand Down
5 changes: 5 additions & 0 deletions lib/templates/md/_type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
##### {{{linkedName}}}{{{linkedGenericParameters}}}({{{ linkedParamsNoMetadata }}}) {{{ linkedReturnType }}}
{{>categorization}}

{{{ oneLineDoc }}} {{{ extendedDocLink }}} {{!two spaces intentional}}
{{>features}}
7 changes: 7 additions & 0 deletions lib/templates/md/_type_multiline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#hasAnnotations}}
{{#annotations}}
- {{{.}}}
{{/annotations}}
{{/hasAnnotations}}

{{>name_summary}}{{{genericParameters}}} = {{{linkedReturnType}}}
6 changes: 6 additions & 0 deletions lib/templates/md/_typedef.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{#isCallable}}
{{>callable}}
{{/isCallable}}
{{^isCallable}}
{{>type}}
{{/isCallable}}
6 changes: 6 additions & 0 deletions lib/templates/md/_typedef_multiline.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{#isCallable}}
{{>callable_multiline}}
{{/isCallable}}
{{^isCallable}}
{{>type_multiline}}
{{/isCallable}}
2 changes: 1 addition & 1 deletion lib/templates/md/category.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
## Typedefs

{{#publicTypedefsSorted}}
{{>callable}}
{{>typedef}}

{{/publicTypedefsSorted}}
{{/hasPublicTypedefs}}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/md/library.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
## Typedefs

{{#library.publicTypedefsSorted}}
{{>callable}}
{{>typedef}}

{{/library.publicTypedefsSorted}}
{{/library.hasPublicTypedefs}}
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/md/typedef.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
{{/self}}

{{#typeDef}}
{{>callable_multiline}}
{{>typedef_multiline}}

{{>documentation}}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: '>=2.11.99 <3.0.0'

dependencies:
analyzer: ^1.0.0
analyzer: ^1.1.0
args: ^2.0.0
charcode: ^1.2.0
collection: ^1.2.0
Expand Down
Loading