Skip to content

Fix enum representationsto show correct (implicit) args, const #3669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 39 additions & 9 deletions lib/src/model/getter_setter_combo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ mixin GetterSetterCombo on ModelElement {
return original;
}
var target = modelBuilder.fromElement(staticElement) as Constructor;
if (target.enclosingElement is! Class) return original;
var targetClass = target.enclosingElement as Class;
// TODO(jcollins-g): this logic really should be integrated into Constructor,
// but that's not trivial because of linkedName's usage.
if (targetClass.name == target.name) {
var enclosingElement = target.enclosingElement;
if (enclosingElement is! Class) return original;
// TODO(jcollins-g): this logic really should be integrated into
// `Constructor`, but that's not trivial because of `linkedName`'s usage.
if (enclosingElement.name == target.name) {
return original.replaceAll(constructorName, target.linkedName);
}
return original.replaceAll('${targetClass.name}.${target.name}',
'${targetClass.linkedName}.${target.linkedName}');
return original.replaceAll('${enclosingElement.name}.${target.name}',
'${enclosingElement.linkedName}.${target.linkedName}');
}

@override
Expand Down Expand Up @@ -110,8 +110,38 @@ mixin GetterSetterCombo on ModelElement {
String get constantValueTruncated =>
linkifyConstantValue(truncateString(constantValueBase, 200));

late final String constantValueBase = const HtmlEscape(HtmlEscapeMode.unknown)
.convert(constantInitializer?.toString() ?? '');
late final String constantValueBase = () {
final constantInitializer = this.constantInitializer;
if (constantInitializer == null) {
return '';
}

var initializerString = constantInitializer.toString();

final self = this;
if (self is! EnumField ||
constantInitializer is! InstanceCreationExpression) {
return _htmlEscape.convert(initializerString);
}

initializerString = 'const $initializerString';

var isImplicitConstructorCall = constantInitializer
.constructorName.staticElement?.isDefaultConstructor ??
false;
if (isImplicitConstructorCall) {
// For an enum value with an implicit constructor call (like
// `enum E { one, two; }`), `constantInitializer.toString()` does not
// include the implicit enum index argument (it is something like
// `const E()`). We must manually include it. See
// https://github.com/dart-lang/sdk/issues/54988.
initializerString =
initializerString.replaceFirst('()', '(${self.index})');
}
return _htmlEscape.convert(initializerString);
}();

static const _htmlEscape = HtmlEscape(HtmlEscapeMode.unknown);

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

Expand Down
42 changes: 32 additions & 10 deletions test/enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -394,31 +394,53 @@ class C {}
);
}

void test_valuesHaveAConstantValueImplementation() async {
void test_constantValue_implicitConstructorCall() async {
var library = await bootPackageWithLibrary('''
enum E { one, two }
''');

var oneValue = library.enums.named('E').publicEnumValues.named('one');
expect(oneValue.constantValueTruncated, 'const E(0)');

var twoValue = library.enums.named('E').publicEnumValues.named('two');
expect(twoValue.constantValueTruncated, 'const E(1)');
}

void test_constantValue_explicitConstructorCall() async {
var library = await bootPackageWithLibrary('''
enum E {
one.named(1),
two.named(2);

final int x;

/// A named constructor.
const E.named(this.x);
}

enum F { one, two }
''');
var eOneValue = library.enums.named('E').publicEnumValues.named('one');
expect(eOneValue.constantValueTruncated, 'E.named(1)');
expect(eOneValue.constantValueTruncated, 'const E.named(1)');

var eTwoValue = library.enums.named('E').publicEnumValues.named('two');
expect(eTwoValue.constantValueTruncated, 'E.named(2)');
expect(eTwoValue.constantValueTruncated, 'const E.named(2)');
}

var fOneValue = library.enums.named('F').publicEnumValues.named('one');
expect(fOneValue.constantValueTruncated, 'F()');
void test_constantValue_explicitConstructorCall_zeroConstructorArgs() async {
var library = await bootPackageWithLibrary('''
enum E {
one.named1(),
two.named2();

var fTwoValue = library.enums.named('F').publicEnumValues.named('two');
expect(fTwoValue.constantValueTruncated, 'F()');
final int x;

const E.named1() : x = 1;
const E.named2() : x = 2;
}
''');
var eOneValue = library.enums.named('E').publicEnumValues.named('one');
expect(eOneValue.constantValueTruncated, 'const E.named1()');

var eTwoValue = library.enums.named('E').publicEnumValues.named('two');
expect(eTwoValue.constantValueTruncated, 'const E.named2()');
}
}

Expand Down
9 changes: 2 additions & 7 deletions test/templates/enum_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() async {
late List<String> enumWithDefaultConstructorLines;
late List<String> enumWithDefaultConstructorRightSidebarLines;

group('enhanced enums', skip: !enhancedEnumsAllowed, () {
group('enums', () {
setUpAll(() async {
final packageMetaProvider = testPackageMetaProvider;
final resourceProvider =
Expand All @@ -31,11 +31,6 @@ name: enums
version: 0.0.1
environment:
sdk: '>=2.17.0-0 <3.0.0'
''',
analysisOptions: '''
analyzer:
enable-experiment:
- enhanced-enums
''',
libFiles: [
d.file('lib.dart', '''
Expand Down Expand Up @@ -194,7 +189,7 @@ enum EnumWithDefaultConstructor {
matches('<span class="name ">one</span>'),
matches('<p>Doc comment for <a href="../lib/E.html">one</a>.</p>'),
matches(
r'<span class="signature"><code>E&lt;int&gt;.named\(1\)</code></span>'),
r'<span class="signature"><code>const E&lt;int&gt;.named\(1\)</code></span>'),
]),
);
});
Expand Down