Skip to content

Commit a16973b

Browse files
authored
Fix enum representationsto show correct (implicit) args, const (#3669)
1 parent 47fe3b6 commit a16973b

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

lib/src/model/getter_setter_combo.dart

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ mixin GetterSetterCombo on ModelElement {
7373
return original;
7474
}
7575
var target = modelBuilder.fromElement(staticElement) as Constructor;
76-
if (target.enclosingElement is! Class) return original;
77-
var targetClass = target.enclosingElement as Class;
78-
// TODO(jcollins-g): this logic really should be integrated into Constructor,
79-
// but that's not trivial because of linkedName's usage.
80-
if (targetClass.name == target.name) {
76+
var enclosingElement = target.enclosingElement;
77+
if (enclosingElement is! Class) return original;
78+
// TODO(jcollins-g): this logic really should be integrated into
79+
// `Constructor`, but that's not trivial because of `linkedName`'s usage.
80+
if (enclosingElement.name == target.name) {
8181
return original.replaceAll(constructorName, target.linkedName);
8282
}
83-
return original.replaceAll('${targetClass.name}.${target.name}',
84-
'${targetClass.linkedName}.${target.linkedName}');
83+
return original.replaceAll('${enclosingElement.name}.${target.name}',
84+
'${enclosingElement.linkedName}.${target.linkedName}');
8585
}
8686

8787
@override
@@ -110,8 +110,38 @@ mixin GetterSetterCombo on ModelElement {
110110
String get constantValueTruncated =>
111111
linkifyConstantValue(truncateString(constantValueBase, 200));
112112

113-
late final String constantValueBase = const HtmlEscape(HtmlEscapeMode.unknown)
114-
.convert(constantInitializer?.toString() ?? '');
113+
late final String constantValueBase = () {
114+
final constantInitializer = this.constantInitializer;
115+
if (constantInitializer == null) {
116+
return '';
117+
}
118+
119+
var initializerString = constantInitializer.toString();
120+
121+
final self = this;
122+
if (self is! EnumField ||
123+
constantInitializer is! InstanceCreationExpression) {
124+
return _htmlEscape.convert(initializerString);
125+
}
126+
127+
initializerString = 'const $initializerString';
128+
129+
var isImplicitConstructorCall = constantInitializer
130+
.constructorName.staticElement?.isDefaultConstructor ??
131+
false;
132+
if (isImplicitConstructorCall) {
133+
// For an enum value with an implicit constructor call (like
134+
// `enum E { one, two; }`), `constantInitializer.toString()` does not
135+
// include the implicit enum index argument (it is something like
136+
// `const E()`). We must manually include it. See
137+
// https://github.com/dart-lang/sdk/issues/54988.
138+
initializerString =
139+
initializerString.replaceFirst('()', '(${self.index})');
140+
}
141+
return _htmlEscape.convert(initializerString);
142+
}();
143+
144+
static const _htmlEscape = HtmlEscape(HtmlEscapeMode.unknown);
115145

116146
late final bool hasPublicGetter = hasGetter && getter!.isPublic;
117147

test/enum_test.dart

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -394,31 +394,53 @@ class C {}
394394
);
395395
}
396396

397-
void test_valuesHaveAConstantValueImplementation() async {
397+
void test_constantValue_implicitConstructorCall() async {
398+
var library = await bootPackageWithLibrary('''
399+
enum E { one, two }
400+
''');
401+
402+
var oneValue = library.enums.named('E').publicEnumValues.named('one');
403+
expect(oneValue.constantValueTruncated, 'const E(0)');
404+
405+
var twoValue = library.enums.named('E').publicEnumValues.named('two');
406+
expect(twoValue.constantValueTruncated, 'const E(1)');
407+
}
408+
409+
void test_constantValue_explicitConstructorCall() async {
398410
var library = await bootPackageWithLibrary('''
399411
enum E {
400412
one.named(1),
401413
two.named(2);
402414
403415
final int x;
404416
405-
/// A named constructor.
406417
const E.named(this.x);
407418
}
408-
409-
enum F { one, two }
410419
''');
411420
var eOneValue = library.enums.named('E').publicEnumValues.named('one');
412-
expect(eOneValue.constantValueTruncated, 'E.named(1)');
421+
expect(eOneValue.constantValueTruncated, 'const E.named(1)');
413422

414423
var eTwoValue = library.enums.named('E').publicEnumValues.named('two');
415-
expect(eTwoValue.constantValueTruncated, 'E.named(2)');
424+
expect(eTwoValue.constantValueTruncated, 'const E.named(2)');
425+
}
416426

417-
var fOneValue = library.enums.named('F').publicEnumValues.named('one');
418-
expect(fOneValue.constantValueTruncated, 'F()');
427+
void test_constantValue_explicitConstructorCall_zeroConstructorArgs() async {
428+
var library = await bootPackageWithLibrary('''
429+
enum E {
430+
one.named1(),
431+
two.named2();
419432
420-
var fTwoValue = library.enums.named('F').publicEnumValues.named('two');
421-
expect(fTwoValue.constantValueTruncated, 'F()');
433+
final int x;
434+
435+
const E.named1() : x = 1;
436+
const E.named2() : x = 2;
437+
}
438+
''');
439+
var eOneValue = library.enums.named('E').publicEnumValues.named('one');
440+
expect(eOneValue.constantValueTruncated, 'const E.named1()');
441+
442+
var eTwoValue = library.enums.named('E').publicEnumValues.named('two');
443+
expect(eTwoValue.constantValueTruncated, 'const E.named2()');
422444
}
423445
}
424446

test/templates/enum_test.dart

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ void main() async {
1919
late List<String> enumWithDefaultConstructorLines;
2020
late List<String> enumWithDefaultConstructorRightSidebarLines;
2121

22-
group('enhanced enums', skip: !enhancedEnumsAllowed, () {
22+
group('enums', () {
2323
setUpAll(() async {
2424
final packageMetaProvider = testPackageMetaProvider;
2525
final resourceProvider =
@@ -31,11 +31,6 @@ name: enums
3131
version: 0.0.1
3232
environment:
3333
sdk: '>=2.17.0-0 <3.0.0'
34-
''',
35-
analysisOptions: '''
36-
analyzer:
37-
enable-experiment:
38-
- enhanced-enums
3934
''',
4035
libFiles: [
4136
d.file('lib.dart', '''
@@ -194,7 +189,7 @@ enum EnumWithDefaultConstructor {
194189
matches('<span class="name ">one</span>'),
195190
matches('<p>Doc comment for <a href="../lib/E.html">one</a>.</p>'),
196191
matches(
197-
r'<span class="signature"><code>E&lt;int&gt;.named\(1\)</code></span>'),
192+
r'<span class="signature"><code>const E&lt;int&gt;.named\(1\)</code></span>'),
198193
]),
199194
);
200195
});

0 commit comments

Comments
 (0)