Skip to content

Commit fb2c2d0

Browse files
authored
Fix issue with generation of 'null' value enum fields (#1470)
* Fix the null enums problem by dropping the index hack, using the analyzer to compute the values instead * Add tests, cleanup * Make use of isEnumConstant to simplify constructor logic
1 parent 6eafa36 commit fb2c2d0

File tree

5 files changed

+23
-44
lines changed

5 files changed

+23
-44
lines changed

lib/src/model.dart

Lines changed: 12 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ class Class extends ModelElement implements EnclosedElement {
459459
..addAll(constructors)
460460
..addAll(staticMethods)
461461
..addAll(staticProperties)
462-
..addAll(allInstanceMethods)
463462
..addAll(_typeParameters);
464463
}
465464
return _allModelElements.toList();
@@ -694,7 +693,7 @@ class Class extends ModelElement implements EnclosedElement {
694693
List<Field> get instanceProperties {
695694
if (_instanceFields != null) return _instanceFields;
696695
_instanceFields = _allFields
697-
.where((f) => !f.isStatic && !f.isInherited)
696+
.where((f) => !f.isStatic && !f.isInherited && !f.isConst)
698697
.toList(growable: false)
699698
..sort(byName);
700699

@@ -1073,36 +1072,14 @@ abstract class EnclosedElement {
10731072
}
10741073

10751074
class Enum extends Class {
1076-
List<EnumField> _enumFields;
1077-
10781075
Enum(ClassElement element, Library library) : super(element, library);
10791076

1080-
@override
1081-
List<EnumField> get constants {
1082-
if (_enumFields != null) return _enumFields;
1083-
1084-
// This is a hack to give 'values' an index of -1 and all other fields
1085-
// their expected indices. https://github.com/dart-lang/dartdoc/issues/1176
1086-
var index = -1;
1087-
1088-
_enumFields = [];
1089-
for (FieldElement f in _cls.fields.where((f) => f.isConst)) {
1090-
// Enums do not have inheritance.
1091-
Accessor accessor = new ModelElement.from(f.getter, library);
1092-
EnumField enumField =
1093-
new ModelElement.from(f, library, index: index++, getter: accessor);
1094-
if (enumField.isPublic) _enumFields.add(enumField);
1095-
}
1096-
_enumFields.sort(byName);
1097-
1098-
return _enumFields;
1099-
}
1100-
11011077
@override
11021078
List<EnumField> get instanceProperties {
11031079
return super
11041080
.instanceProperties
1105-
.map((Field p) => new ModelElement.from(p.element, p.library))
1081+
.map((Field p) => new ModelElement.from(p.element, p.library,
1082+
getter: p.getter, setter: p.setter))
11061083
.toList(growable: false);
11071084
}
11081085

@@ -2150,13 +2127,8 @@ abstract class ModelElement extends Nameable
21502127
// parameter when given a null.
21512128
/// Do not construct any ModelElements unless they are from this constructor.
21522129
/// Specify enclosingClass only if this is to be an inherited object.
2153-
/// Specify index only if this is to be an EnumField.forConstant.
21542130
factory ModelElement.from(Element e, Library library,
2155-
{Class enclosingClass, int index, Accessor getter, Accessor setter}) {
2156-
// We don't need index in this key because it isn't a disambiguator.
2157-
// It isn't a disambiguator because EnumFields are not inherited, ever.
2158-
// TODO(jcollins-g): cleanup class hierarchy so that EnumFields aren't
2159-
// Inheritable, somehow?
2131+
{Class enclosingClass, Accessor getter, Accessor setter}) {
21602132
if (e is Member) {
21612133
e = Package.getBasestElement(e);
21622134
}
@@ -2193,21 +2165,18 @@ abstract class ModelElement extends Nameable
21932165
newModelElement = new Typedef(e, library);
21942166
}
21952167
if (e is FieldElement) {
2168+
assert(getter != null || setter != null);
21962169
if (enclosingClass == null) {
2197-
if (index != null) {
2198-
assert(getter != null);
2199-
newModelElement =
2200-
new EnumField.forConstant(index, e, library, getter);
2170+
if (e.isEnumConstant) {
2171+
int index = e.computeConstantValue().getField('index').toIntValue();
2172+
newModelElement = new EnumField.forConstant(index, e, library, getter);
2173+
} else if (e.enclosingElement.isEnum) {
2174+
newModelElement = new EnumField(e, library, getter, setter);
22012175
} else {
2202-
if (e.enclosingElement.isEnum) {
2203-
newModelElement = new EnumField(e, library, getter, setter);
2204-
} else {
2205-
assert(getter != null || setter != null);
2206-
newModelElement = new Field(e, library, getter, setter);
2207-
}
2176+
newModelElement = new Field(e, library, getter, setter);
22082177
}
22092178
} else {
2210-
assert(getter != null || setter != null);
2179+
// EnumFields can't be inherited, so this case is simpler.
22112180
newModelElement =
22122181
new Field.inherited(e, enclosingClass, library, getter, setter);
22132182
}

test/model_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,9 @@ void main() {
842842

843843
setUp(() {
844844
animal = exLibrary.enums.firstWhere((e) => e.name == 'Animal');
845+
846+
/// Trigger code reference resolution
847+
animal.documentationAsHtml;
845848
});
846849

847850
test('has a fully qualified name', () {

testing/test_package/lib/example.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ typedef String processMessage<T>(String msg);
6767

6868
typedef String ParameterizedTypedef<T>(T msg, int foo);
6969

70+
/// Referencing [processMessage] (or other things) here should not break
71+
/// enum constants ala #1445
7072
enum Animal {
7173
/// Single line docs.
7274
CAT,

testing/test_package_docs/ex/Animal-class.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ <h5>library ex</h5>
100100

101101
<div class="col-xs-12 col-sm-9 col-md-8 main-content">
102102

103+
<section class="desc markdown">
104+
<p>Referencing <a href="ex/processMessage.html">processMessage</a> (or other things) here should not break
105+
enum constants ala #1445</p>
106+
</section>
103107

104108

105109
<section class="summary offset-anchor" id="constants">

testing/test_package_docs/ex/ex-library.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,8 @@ <h2>Enums</h2>
384384
<a href="ex/Animal-class.html">Animal</a>
385385
</dt>
386386
<dd>
387-
387+
Referencing <a href="ex/processMessage.html">processMessage</a> (or other things) here should not break
388+
enum constants ala #1445
388389
</dd>
389390
</dl>
390391
</section>

0 commit comments

Comments
 (0)