Skip to content

support generic methods #1282

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 5 commits into from
Nov 28, 2016
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
2 changes: 2 additions & 0 deletions .analysis_options
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
analyzer:
strong-mode: true
language:
enableGenericMethods: true
exclude:
- 'doc/api/**'
- 'lib/templates/*.html'
Expand Down
1 change: 1 addition & 0 deletions lib/dartdoc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ class DartDoc {
SourceFactory sourceFactory = new SourceFactory(resolvers);

var options = new AnalysisOptionsImpl();
options.enableGenericMethods = true;

AnalysisEngine.instance.processRequiredPlugins();

Expand Down
4 changes: 2 additions & 2 deletions lib/src/html/template_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ class FunctionTemplateData extends TemplateData<ModelFunction> {
'${function.name} function - ${library.name} library - Dart API';
@override
String get layoutTitle =>
_layoutTitle(function.name, 'function', function.isDeprecated);
_layoutTitle(function.nameWithGenerics, 'function', function.isDeprecated);
@override
String get metaDescription =>
'API docs for the ${function.name} function from the '
Expand Down Expand Up @@ -306,7 +306,7 @@ class MethodTemplateData extends TemplateData<Method> {
'${library.name} library - Dart API';
@override
String get layoutTitle =>
_layoutTitle(method.name, method.fullkind, method.isDeprecated);
_layoutTitle(method.nameWithGenerics, method.fullkind, method.isDeprecated);
@override
String get metaDescription =>
'API docs for the ${method.name} method from the ${clazz.name} class, '
Expand Down
54 changes: 49 additions & 5 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ class Class extends ModelElement implements EnclosedElement {
// problem if we want to show that info later.
List<ElementType> get mixins => _mixins;

@override
String get nameWithGenerics {
if (!modelType.isParameterizedType) return name;
return '$name&lt;${_typeParameters.map((t) => t.name).join(', ')}&gt;';
Expand Down Expand Up @@ -1221,15 +1222,24 @@ class Method extends ModelElement
implements EnclosedElement {
bool _isInherited = false;
Class _enclosingClass;
List<TypeParameter> typeParameters = [];

Method(MethodElement element, Library library) : super(element, library) {
_modelType = new ElementType(_method.type, this);
_calcTypeParameters();
}

Method.inherited(MethodElement element, this._enclosingClass, Library library)
: super(element, library) {
_modelType = new ElementType(_method.type, this);
_isInherited = true;
_calcTypeParameters();
}

void _calcTypeParameters() {
typeParameters = _method.typeParameters.map((f) {
return new TypeParameter(f, library);
}).toList();
}

@override
Expand Down Expand Up @@ -1263,6 +1273,18 @@ class Method extends ModelElement

String get linkedReturnType => modelType.createLinkedReturnTypeName();

@override
String get nameWithGenerics {
if (typeParameters.isEmpty) return name;
return '$name&lt;${typeParameters.map((t) => t.name).join(', ')}&gt;';
}

@override
String get genericParameters {
if (typeParameters.isEmpty) return '';
return '&lt;${typeParameters.map((t) => t.name).join(', ')}&gt;';
}

@override
Method get overriddenElement {
ClassElement parent = element.enclosingElement;
Expand Down Expand Up @@ -1482,6 +1504,10 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
@override
String get name => element.name;

String get nameWithGenerics => name;

String get genericParameters => '';

@override
String get oneLineDoc => _documentation.asOneLiner;

Expand Down Expand Up @@ -1673,11 +1699,7 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
}
}

var classContent = '';
if (isDeprecated) {
classContent = 'class="deprecated" ';
}

var classContent = isDeprecated ? 'class="deprecated" ' : '';
return '<a ${classContent}href="${href}">$name</a>';
}

Expand Down Expand Up @@ -1726,9 +1748,18 @@ abstract class ModelElement implements Comparable, Nameable, Documentable {
class ModelFunction extends ModelElement
with SourceCodeMixin
implements EnclosedElement {
List<TypeParameter> typeParameters = [];

ModelFunction(FunctionElement element, Library library)
: super(element, library) {
_modelType = new ElementType(_func.type, this);
_calcTypeParameters();
}

void _calcTypeParameters() {
typeParameters = _func.typeParameters.map((f) {
return new TypeParameter(f, library);
}).toList();
}

@override
Expand All @@ -1747,6 +1778,18 @@ class ModelFunction extends ModelElement

String get linkedReturnType => modelType.createLinkedReturnTypeName();

@override
String get nameWithGenerics {
if (typeParameters.isEmpty) return name;
return '$name&lt;${typeParameters.map((t) => t.name).join(', ')}&gt;';
}

@override
String get genericParameters {
if (typeParameters.isEmpty) return '';
return '&lt;${typeParameters.map((t) => t.name).join(', ')}&gt;';
}

FunctionElement get _func => (element as FunctionElement);
}

Expand Down Expand Up @@ -2240,6 +2283,7 @@ class Typedef extends ModelElement
? modelType.createLinkedReturnTypeName()
: _typedef.returnType.name;

@override
String get nameWithGenerics {
if (!modelType.isParameterizedType) return name;
return '$name&lt;${_typeParameters.map((t) => t.name).join(', ')}&gt;';
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/_callable.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<dt id="{{htmlId}}" class="callable{{ #isInherited }} inherited{{ /isInherited}}">
<span class="name{{#isDeprecated}} deprecated{{/isDeprecated}}">{{{linkedName}}}</span><span class="signature">(<wbr>{{{ linkedParamsNoMetadata }}})
<span class="name{{#isDeprecated}} deprecated{{/isDeprecated}}">{{{linkedName}}}</span><span class="signature">{{{genericParameters}}}(<wbr>{{{ linkedParamsNoMetadata }}})
<span class="returntype parameter">&#8594; {{{ linkedReturnType }}}</span>
</span>
</dt>
Expand Down
2 changes: 1 addition & 1 deletion lib/templates/_callable_multiline.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
</div>
{{/hasAnnotations}}
<span class="returntype">{{{ linkedReturnType }}}</span>
{{>name_summary}}(<wbr>{{#hasParameters}}{{{linkedParamsLines}}}{{/hasParameters}})
{{>name_summary}}{{{genericParameters}}}(<wbr>{{#hasParameters}}{{{linkedParamsLines}}}{{/hasParameters}})
25 changes: 21 additions & 4 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ void main() {
});

test('get methods', () {
expect(Dog.instanceMethods, hasLength(5));
expect(Dog.instanceMethods, hasLength(6));
});

test('get operators', () {
Expand Down Expand Up @@ -637,7 +637,7 @@ void main() {
});

test('F has many inherited methods', () {
expect(F.inheritedMethods, hasLength(8));
expect(F.inheritedMethods, hasLength(9));
expect(
F.inheritedMethods.map((im) => im.name),
equals([
Expand All @@ -647,6 +647,7 @@ void main() {
'noSuchMethod',
'test',
'testGeneric',
'testGenericMethod',
'testMethod',
'toString'
]));
Expand Down Expand Up @@ -741,6 +742,7 @@ void main() {
String valueByName(var name) {
return animal.constants.firstWhere((f) => f.name == name).constantValue;
}

expect(valueByName('CAT'), equals('const Animal(0)'));
expect(valueByName('DOG'), equals('const Animal(1)'));
expect(valueByName('HORSE'), equals('const Animal(2)'));
Expand All @@ -759,11 +761,14 @@ void main() {

group('Function', () {
ModelFunction f1;
ModelFunction genericFunction;
ModelFunction thisIsAsync;
ModelFunction topLevelFunction;

setUp(() {
f1 = exLibrary.functions.single;
f1 = exLibrary.functions.first;
genericFunction =
exLibrary.functions.firstWhere((f) => f.name == 'genericFunction');
thisIsAsync =
fakeLibrary.functions.firstWhere((f) => f.name == 'thisIsAsync');
topLevelFunction =
Expand Down Expand Up @@ -848,12 +853,16 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
'<span class="type-annotation"><a href="fake/Callback2.html">Callback2</a></span> '
'<span class="parameter-name">callback</span></span>');
});

test('supports generic methods', () {
expect(genericFunction.nameWithGenerics, 'genericFunction&lt;T&gt;');
});
});

group('Method', () {
Class classB, klass, HasGenerics, Cat, CatString;
Method m1, isGreaterThan, m4, m5, m6, m7, convertToMap, abstractMethod;
Method inheritedClear, testGeneric;
Method inheritedClear, testGeneric, testGenericMethod;

setUp(() {
klass = exLibrary.classes.singleWhere((c) => c.name == 'Klass');
Expand All @@ -879,6 +888,10 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
.singleWhere((c) => c.name == 'Dog')
.instanceMethods
.singleWhere((m) => m.name == 'testGeneric');
testGenericMethod = exLibrary.classes
.singleWhere((c) => c.name == 'Dog')
.instanceMethods
.singleWhere((m) => m.name == 'testGenericMethod');
convertToMap = HasGenerics.instanceMethods
.singleWhere((m) => m.name == 'convertToMap');
});
Expand Down Expand Up @@ -963,6 +976,10 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
expect(functionArgParam.modelType.createLinkedReturnTypeName(), 'String');
});

test('generic method type args are rendered', () {
expect(testGenericMethod.nameWithGenerics, 'testGenericMethod&lt;T&gt;');
});

test('doc for method with no return type', () {
var comment = m5.documentation;
var comment2 = m6.documentation;
Expand Down
2 changes: 2 additions & 0 deletions test/src/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class AnalyzerHelper {
SourceFactory sourceFactory = new SourceFactory(resolvers);
AnalysisEngine.instance.processRequiredPlugins();
context = AnalysisEngine.instance.createAnalysisContext();
context.analysisOptions = new AnalysisOptionsImpl()
..enableGenericMethods = true;
context.sourceFactory = sourceFactory;
}

Expand Down
8 changes: 8 additions & 0 deletions testing/test_package/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ get y => 2;

int function1(String s, bool b, lastParam) => 5;

T genericFunction<T>(T arg) {
return arg;
}

typedef String processMessage<T>(String msg);

enum Animal {
Expand Down Expand Up @@ -227,6 +231,10 @@ class Dog implements Cat, E {

void testMethod(Iterable it) {}

T testGenericMethod<T>(T arg) {
return arg;
}

@Deprecated("Internal use")
static Dog createDog(String s) {
return new Dog.deprecatedCreate(s);
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/Animal-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/Apple-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/B-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/COLOR-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/COLOR_GREEN-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/COLOR_ORANGE-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/COMPLEX_COLOR-constant.html
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/Cat-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/CatString-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/ConstantCat-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/Cool-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
1 change: 1 addition & 0 deletions testing/test_package_docs/ex/Deprecated-class.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ <h5><a href="ex/ex-library.html">ex</a></h5>

<li class="section-title"><a href="ex/ex-library.html#functions">Functions</a></li>
<li><a href="ex/function1.html">function1</a></li>
<li><a href="ex/genericFunction.html">genericFunction</a></li>

<li class="section-title"><a href="ex/ex-library.html#enums">Enums</a></li>
<li><a href="ex/Animal-class.html">Animal</a></li>
Expand Down
Loading