Skip to content

Commit 8305be5

Browse files
committed
Add support for GenericFunctionTypeElementImpl (#1497)
* 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
1 parent 831502f commit 8305be5

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

lib/src/model.dart

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1988,6 +1988,10 @@ abstract class ModelElement extends Nameable
19881988
newModelElement = new Parameter(e, library);
19891989
}
19901990
}
1991+
// TODO(jcollins-g): Consider subclass for ModelFunctionTyped.
1992+
if (e is GenericFunctionTypeElement) {
1993+
newModelElement = new ModelFunctionTyped(e, library);
1994+
}
19911995
if (newModelElement == null) throw "Unknown type ${e.runtimeType}";
19921996
if (enclosingClass != null) assert(newModelElement is Inheritable);
19931997
if (library != null) {
@@ -2097,7 +2101,8 @@ abstract class ModelElement extends Nameable
20972101
return md.map((dynamic a) {
20982102
String annotation = (const HtmlEscape()).convert(a.toSource());
20992103
// a.element can be null if the element can't be resolved.
2100-
var me = package.findCanonicalModelElementFor(a.element?.enclosingElement);
2104+
var me =
2105+
package.findCanonicalModelElementFor(a.element?.enclosingElement);
21012106
if (me != null)
21022107
annotation = annotation.replaceFirst(me.name, me.linkedName);
21032108
return annotation;
@@ -2127,7 +2132,7 @@ abstract class ModelElement extends Nameable
21272132
}
21282133

21292134
bool get canHaveParameters =>
2130-
element is ExecutableElement || element is FunctionTypeAliasElement;
2135+
element is ExecutableElement || element is FunctionTypedElement;
21312136

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

25042509
List<ParameterElement> params;
25052510

2506-
if (element is ExecutableElement) {
2507-
// the as check silences the warning
2508-
params = (element as ExecutableElement).parameters;
2509-
}
2510-
2511-
if (element is FunctionTypeAliasElement) {
2512-
params = (element as FunctionTypeAliasElement).parameters;
2511+
if (element is FunctionTypedElement) {
2512+
params = (element as FunctionTypedElement).parameters;
25132513
}
25142514

25152515
_parameters = new UnmodifiableListView<Parameter>(params
@@ -2866,12 +2866,25 @@ abstract class ModelElement extends Nameable
28662866
}
28672867
}
28682868

2869-
class ModelFunction extends ModelElement
2869+
class ModelFunction extends ModelFunctionTyped {
2870+
ModelFunction(FunctionElement element, Library library)
2871+
: super(element, library);
2872+
2873+
@override
2874+
bool get isStatic {
2875+
return _func.isStatic;
2876+
}
2877+
2878+
@override
2879+
FunctionElement get _func => (element as FunctionElement);
2880+
}
2881+
2882+
class ModelFunctionTyped extends ModelElement
28702883
with SourceCodeMixin
28712884
implements EnclosedElement {
28722885
List<TypeParameter> typeParameters = [];
28732886

2874-
ModelFunction(FunctionElement element, Library library)
2887+
ModelFunctionTyped(FunctionTypedElement element, Library library)
28752888
: super(element, library) {
28762889
_modelType = new ElementType(_func.type, this);
28772890
_calcTypeParameters();
@@ -2901,9 +2914,6 @@ class ModelFunction extends ModelElement
29012914
return '${canonicalLibrary.dirName}/$fileName';
29022915
}
29032916

2904-
@override
2905-
bool get isStatic => _func.isStatic;
2906-
29072917
@override
29082918
String get kind => 'function';
29092919

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

2924-
FunctionElement get _func => (element as FunctionElement);
2934+
FunctionTypedElement get _func => (element as FunctionTypedElement);
29252935
}
29262936

29272937
/// Something that has a name.
@@ -3938,7 +3948,14 @@ class Parameter extends ModelElement implements EnclosedElement {
39383948
}
39393949

39403950
@override
3941-
String get htmlId => '${_parameter.enclosingElement.name}-param-${name}';
3951+
String get htmlId {
3952+
String enclosingName = _parameter.enclosingElement.name;
3953+
if (_parameter.enclosingElement is GenericFunctionTypeElement) {
3954+
// TODO(jcollins-g): Drop when GenericFunctionTypeElement populates name.
3955+
enclosingName = _parameter.enclosingElement.enclosingElement.name;
3956+
}
3957+
return '${enclosingName}-param-${name}';
3958+
}
39423959

39433960
bool get isOptional => _parameter.parameterKind.isOptional;
39443961

test/model_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,6 +1206,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
12061206
Field lengthX;
12071207
Field sFromApple, mFromApple, mInB, autoCompress;
12081208
Field isEmpty;
1209+
Field ExtraSpecialListLength;
12091210

12101211
setUp(() {
12111212
c = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
@@ -1243,8 +1244,16 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
12431244
.firstWhere((c) => c.name == 'B')
12441245
.allInstanceProperties
12451246
.firstWhere((p) => p.name == 'autoCompress');
1247+
ExtraSpecialListLength =
1248+
fakeLibrary.classes.firstWhere((c) => c.name == 'SpecialList').allInstanceProperties.firstWhere((f) => f.name == 'length');
12461249
});
12471250

1251+
test('inheritance of docs from SDK works for getter/setter combos', () {
1252+
expect(ExtraSpecialListLength.getter.documentationFrom.element.library.name == 'dart.core', isTrue);
1253+
expect(ExtraSpecialListLength.oneLineDoc == '', isFalse);
1254+
}, skip:
1255+
'Passes on Analyzer 0.31.0+');
1256+
12481257
test('has a fully qualified name', () {
12491258
expect(lengthX.fullyQualifiedName, 'fake.WithGetterAndSetter.lengthX');
12501259
});

testing/test_package_docs/fake/NewGenericTypedef.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ <h5>library fake</h5>
120120

121121
<section class="multi-line-signature">
122122
<span class="returntype">List&lt;S&gt;</span>
123-
<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>)
123+
<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>)
124124
</section>
125125

126126
<section class="desc markdown">

testing/test_package_docs/fake/fake-library.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ <h2>Typedefs</h2>
602602

603603
</dd>
604604
<dt id="NewGenericTypedef" class="callable">
605-
<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>)
605+
<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>)
606606
<span class="returntype parameter">&#8594; List&lt;S&gt;</span>
607607
</span>
608608
</dt>

0 commit comments

Comments
 (0)