Skip to content

Commit

Permalink
Add support for GenericFunctionTypeElementImpl (#1497)
Browse files Browse the repository at this point in the history
* Initial implementation

* Test updates

* Rearrange test

* Tune up tests to work without new analyzer

* Change to alpha analyzer

* dartfmt

* Use original analyzer version for mainline branch

* Review comments
  • Loading branch information
jcollins-g committed Sep 8, 2017
1 parent 831502f commit 8305be5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 18 deletions.
49 changes: 33 additions & 16 deletions lib/src/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1988,6 +1988,10 @@ abstract class ModelElement extends Nameable
newModelElement = new Parameter(e, library);
}
}
// TODO(jcollins-g): Consider subclass for ModelFunctionTyped.
if (e is GenericFunctionTypeElement) {
newModelElement = new ModelFunctionTyped(e, library);
}
if (newModelElement == null) throw "Unknown type ${e.runtimeType}";
if (enclosingClass != null) assert(newModelElement is Inheritable);
if (library != null) {
Expand Down Expand Up @@ -2097,7 +2101,8 @@ abstract class ModelElement extends Nameable
return md.map((dynamic a) {
String annotation = (const HtmlEscape()).convert(a.toSource());
// a.element can be null if the element can't be resolved.
var me = package.findCanonicalModelElementFor(a.element?.enclosingElement);
var me =
package.findCanonicalModelElementFor(a.element?.enclosingElement);
if (me != null)
annotation = annotation.replaceFirst(me.name, me.linkedName);
return annotation;
Expand Down Expand Up @@ -2127,7 +2132,7 @@ abstract class ModelElement extends Nameable
}

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

/// Returns the docs, stripped of their leading comments syntax.
ModelElement _documentationFrom;
Expand Down Expand Up @@ -2503,13 +2508,8 @@ abstract class ModelElement extends Nameable

List<ParameterElement> params;

if (element is ExecutableElement) {
// the as check silences the warning
params = (element as ExecutableElement).parameters;
}

if (element is FunctionTypeAliasElement) {
params = (element as FunctionTypeAliasElement).parameters;
if (element is FunctionTypedElement) {
params = (element as FunctionTypedElement).parameters;
}

_parameters = new UnmodifiableListView<Parameter>(params
Expand Down Expand Up @@ -2866,12 +2866,25 @@ abstract class ModelElement extends Nameable
}
}

class ModelFunction extends ModelElement
class ModelFunction extends ModelFunctionTyped {
ModelFunction(FunctionElement element, Library library)
: super(element, library);

@override
bool get isStatic {
return _func.isStatic;
}

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

class ModelFunctionTyped extends ModelElement
with SourceCodeMixin
implements EnclosedElement {
List<TypeParameter> typeParameters = [];

ModelFunction(FunctionElement element, Library library)
ModelFunctionTyped(FunctionTypedElement element, Library library)
: super(element, library) {
_modelType = new ElementType(_func.type, this);
_calcTypeParameters();
Expand Down Expand Up @@ -2901,9 +2914,6 @@ class ModelFunction extends ModelElement
return '${canonicalLibrary.dirName}/$fileName';
}

@override
bool get isStatic => _func.isStatic;

@override
String get kind => 'function';

Expand All @@ -2921,7 +2931,7 @@ class ModelFunction extends ModelElement
return '&lt;${typeParameters.map((t) => t.name).join(', ')}&gt;';
}

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

/// Something that has a name.
Expand Down Expand Up @@ -3938,7 +3948,14 @@ class Parameter extends ModelElement implements EnclosedElement {
}

@override
String get htmlId => '${_parameter.enclosingElement.name}-param-${name}';
String get htmlId {
String enclosingName = _parameter.enclosingElement.name;
if (_parameter.enclosingElement is GenericFunctionTypeElement) {
// TODO(jcollins-g): Drop when GenericFunctionTypeElement populates name.
enclosingName = _parameter.enclosingElement.enclosingElement.name;
}
return '${enclosingName}-param-${name}';
}

bool get isOptional => _parameter.parameterKind.isOptional;

Expand Down
9 changes: 9 additions & 0 deletions test/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
Field lengthX;
Field sFromApple, mFromApple, mInB, autoCompress;
Field isEmpty;
Field ExtraSpecialListLength;

setUp(() {
c = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
Expand Down Expand Up @@ -1243,8 +1244,16 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
.firstWhere((c) => c.name == 'B')
.allInstanceProperties
.firstWhere((p) => p.name == 'autoCompress');
ExtraSpecialListLength =
fakeLibrary.classes.firstWhere((c) => c.name == 'SpecialList').allInstanceProperties.firstWhere((f) => f.name == 'length');
});

test('inheritance of docs from SDK works for getter/setter combos', () {
expect(ExtraSpecialListLength.getter.documentationFrom.element.library.name == 'dart.core', isTrue);
expect(ExtraSpecialListLength.oneLineDoc == '', isFalse);
}, skip:
'Passes on Analyzer 0.31.0+');

test('has a fully qualified name', () {
expect(lengthX.fullyQualifiedName, 'fake.WithGetterAndSetter.lengthX');
});
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/fake/NewGenericTypedef.html
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ <h5>library fake</h5>

<section class="multi-line-signature">
<span class="returntype">List&lt;S&gt;</span>
<span class="name ">NewGenericTypedef</span>&lt;S&gt;(<wbr><span class="parameter" id="-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="-param-"><span class="type-annotation">T</span></span>)
<span class="name ">NewGenericTypedef</span>&lt;S&gt;(<wbr><span class="parameter" id="NewGenericTypedef-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="NewGenericTypedef-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="NewGenericTypedef-param-"><span class="type-annotation">T</span></span>)
</section>

<section class="desc markdown">
Expand Down
2 changes: 1 addition & 1 deletion testing/test_package_docs/fake/fake-library.html
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ <h2>Typedefs</h2>

</dd>
<dt id="NewGenericTypedef" class="callable">
<span class="name"><a href="fake/NewGenericTypedef.html">NewGenericTypedef</a></span><span class="signature">&lt;S&gt;(<wbr><span class="parameter" id="-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="-param-"><span class="type-annotation">T</span></span>)
<span class="name"><a href="fake/NewGenericTypedef.html">NewGenericTypedef</a></span><span class="signature">&lt;S&gt;(<wbr><span class="parameter" id="NewGenericTypedef-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="NewGenericTypedef-param-"><span class="type-annotation">T</span></span> <span class="parameter" id="NewGenericTypedef-param-"><span class="type-annotation">T</span></span>)
<span class="returntype parameter">&#8594; List&lt;S&gt;</span>
</span>
</dt>
Expand Down

0 comments on commit 8305be5

Please sign in to comment.