Skip to content

Commit d6ab8c2

Browse files
authored
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 d92d3a7 commit d6ab8c2

File tree

5 files changed

+61
-25
lines changed

5 files changed

+61
-25
lines changed

lib/src/model.dart

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,16 +1394,26 @@ abstract class GetterSetterCombo implements ModelElement {
13941394

13951395
if (hasGetter && !getter.element.isSynthetic) {
13961396
assert(getter.documentationFrom.length == 1);
1397-
String docs = getter.documentationFrom.first.computeDocumentationComment;
1398-
if (docs != null) buffer.write(docs);
1397+
// We have to check against dropTextFrom here since documentationFrom
1398+
// doesn't yield the real elements for GetterSetterCombos.
1399+
if (!config.dropTextFrom
1400+
.contains(getter.documentationFrom.first.element.library.name)) {
1401+
String docs =
1402+
getter.documentationFrom.first.computeDocumentationComment;
1403+
if (docs != null) buffer.write(docs);
1404+
}
13991405
}
14001406

14011407
if (hasSetter && !setter.element.isSynthetic) {
14021408
assert(setter.documentationFrom.length == 1);
1403-
String docs = setter.documentationFrom.first.computeDocumentationComment;
1404-
if (docs != null) {
1405-
if (buffer.isNotEmpty) buffer.write('\n\n');
1406-
buffer.write(docs);
1409+
if (!config.dropTextFrom
1410+
.contains(setter.documentationFrom.first.element.library.name)) {
1411+
String docs =
1412+
setter.documentationFrom.first.computeDocumentationComment;
1413+
if (docs != null) {
1414+
if (buffer.isNotEmpty) buffer.write('\n\n');
1415+
buffer.write(docs);
1416+
}
14071417
}
14081418
}
14091419
return buffer.toString();
@@ -2225,6 +2235,10 @@ abstract class ModelElement extends Nameable
22252235
newModelElement = new Parameter(e, library);
22262236
}
22272237
}
2238+
// TODO(jcollins-g): Consider subclass for ModelFunctionTyped.
2239+
if (e is GenericFunctionTypeElement) {
2240+
newModelElement = new ModelFunctionTyped(e, library);
2241+
}
22282242
if (newModelElement == null) throw "Unknown type ${e.runtimeType}";
22292243
if (enclosingClass != null) assert(newModelElement is Inheritable);
22302244
if (library != null) {
@@ -2333,7 +2347,8 @@ abstract class ModelElement extends Nameable
23332347
return md.map((dynamic a) {
23342348
String annotation = (const HtmlEscape()).convert(a.toSource());
23352349
// a.element can be null if the element can't be resolved.
2336-
var me = package.findCanonicalModelElementFor(a.element?.enclosingElement);
2350+
var me =
2351+
package.findCanonicalModelElementFor(a.element?.enclosingElement);
23372352
if (me != null)
23382353
annotation = annotation.replaceFirst(me.name, me.linkedName);
23392354
return annotation;
@@ -2363,7 +2378,7 @@ abstract class ModelElement extends Nameable
23632378
}
23642379

23652380
bool get canHaveParameters =>
2366-
element is ExecutableElement || element is FunctionTypeAliasElement;
2381+
element is ExecutableElement || element is FunctionTypedElement;
23672382

23682383
List<ModelElement> _documentationFrom;
23692384
// TODO(jcollins-g): untangle when mixins can call super
@@ -2762,13 +2777,8 @@ abstract class ModelElement extends Nameable
27622777

27632778
List<ParameterElement> params;
27642779

2765-
if (element is ExecutableElement) {
2766-
// the as check silences the warning
2767-
params = (element as ExecutableElement).parameters;
2768-
}
2769-
2770-
if (element is FunctionTypeAliasElement) {
2771-
params = (element as FunctionTypeAliasElement).parameters;
2780+
if (element is FunctionTypedElement) {
2781+
params = (element as FunctionTypedElement).parameters;
27722782
}
27732783

27742784
_parameters = new UnmodifiableListView<Parameter>(params
@@ -3127,12 +3137,25 @@ abstract class ModelElement extends Nameable
31273137
}
31283138
}
31293139

3130-
class ModelFunction extends ModelElement
3140+
class ModelFunction extends ModelFunctionTyped {
3141+
ModelFunction(FunctionElement element, Library library)
3142+
: super(element, library);
3143+
3144+
@override
3145+
bool get isStatic {
3146+
return _func.isStatic;
3147+
}
3148+
3149+
@override
3150+
FunctionElement get _func => (element as FunctionElement);
3151+
}
3152+
3153+
class ModelFunctionTyped extends ModelElement
31313154
with SourceCodeMixin
31323155
implements EnclosedElement {
31333156
List<TypeParameter> typeParameters = [];
31343157

3135-
ModelFunction(FunctionElement element, Library library)
3158+
ModelFunctionTyped(FunctionTypedElement element, Library library)
31363159
: super(element, library) {
31373160
_modelType = new ElementType(_func.type, this);
31383161
_calcTypeParameters();
@@ -3162,9 +3185,6 @@ class ModelFunction extends ModelElement
31623185
return '${canonicalLibrary.dirName}/$fileName';
31633186
}
31643187

3165-
@override
3166-
bool get isStatic => _func.isStatic;
3167-
31683188
@override
31693189
String get kind => 'function';
31703190

@@ -3182,7 +3202,7 @@ class ModelFunction extends ModelElement
31823202
return '&lt;${typeParameters.map((t) => t.name).join(', ')}&gt;';
31833203
}
31843204

3185-
FunctionElement get _func => (element as FunctionElement);
3205+
FunctionTypedElement get _func => (element as FunctionTypedElement);
31863206
}
31873207

31883208
/// Something that has a name.
@@ -4229,7 +4249,14 @@ class Parameter extends ModelElement implements EnclosedElement {
42294249
}
42304250

42314251
@override
4232-
String get htmlId => '${_parameter.enclosingElement.name}-param-${name}';
4252+
String get htmlId {
4253+
String enclosingName = _parameter.enclosingElement.name;
4254+
if (_parameter.enclosingElement is GenericFunctionTypeElement) {
4255+
// TODO(jcollins-g): Drop when GenericFunctionTypeElement populates name.
4256+
enclosingName = _parameter.enclosingElement.enclosingElement.name;
4257+
}
4258+
return '${enclosingName}-param-${name}';
4259+
}
42334260

42344261
bool get isOptional => _parameter.parameterKind.isOptional;
42354262

pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,4 +350,4 @@ packages:
350350
source: hosted
351351
version: "2.1.12"
352352
sdks:
353-
dart: ">=1.23.0-dev.11.5 <2.0.0"
353+
dart: ">=1.23.0-dev.11.5 <2.0.0-dev.infinity"

test/model_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,7 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
12071207
Field sFromApple, mFromApple, mInB, autoCompress;
12081208
Field isEmpty;
12091209
Field implicitGetterExplicitSetter, explicitGetterImplicitSetter;
1210+
Field ExtraSpecialListLength;
12101211

12111212
setUp(() {
12121213
c = exLibrary.classes.firstWhere((c) => c.name == 'Apple');
@@ -1252,8 +1253,16 @@ String topLevelFunction(int param1, bool param2, Cool coolBeans,
12521253
.firstWhere((c) => c.name == 'B')
12531254
.allInstanceProperties
12541255
.firstWhere((p) => p.name == 'autoCompress');
1256+
ExtraSpecialListLength =
1257+
fakeLibrary.classes.firstWhere((c) => c.name == 'SpecialList').allInstanceProperties.firstWhere((f) => f.name == 'length');
12551258
});
12561259

1260+
test('inheritance of docs from SDK works for getter/setter combos', () {
1261+
expect(ExtraSpecialListLength.getter.documentationFrom.first.element.library.name == 'dart.core', isTrue);
1262+
expect(ExtraSpecialListLength.oneLineDoc == '', isFalse);
1263+
}, skip:
1264+
'Passes on Analyzer 0.31.0+');
1265+
12571266
test('split inheritance with explicit setter works', () {
12581267
expect(implicitGetterExplicitSetter.getter.isInherited, isTrue);
12591268
expect(implicitGetterExplicitSetter.setter.isInherited, isFalse);

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)