Skip to content
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

reviving: Prefer static constant fields on constant object's class #578

Merged
merged 2 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 4 additions & 5 deletions source_gen/lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ Revivable reviveInstance(DartObject object, [LibraryElement? origin]) {
accessor: '${element.enclosingElement.name}.${element.name}',
);
}
// Enums are not included in .definingCompilationUnit.types.
final clazz = element as ClassElement;
if (clazz.isEnum) {
for (final e in clazz.fields.where(

if (element is ClassElement) {
for (final e in element.fields.where(
(f) => f.isPublic && f.isConst && f.computeConstantValue() == object)) {
return Revivable._(
source: url.removeFragment(),
accessor: '${clazz.name}.${e.name}',
accessor: '${element.name}.${e.name}',
);
}
}
Expand Down
12 changes: 11 additions & 1 deletion source_gen/test/constants_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,14 @@ void main() {
});
});

group('Reviable', () {
group('Revivable', () {
late List<ConstantReader> constants;

setUpAll(() async {
final library = await resolveSource(
r'''
library test_lib;
import 'dart:io';

@Int64Like.ZERO
@Duration(seconds: 30)
Expand All @@ -216,6 +217,7 @@ void main() {
@PublicWithPrivateConstructor._()
@_privateField
@Wrapper(_privateFunction)
@ProcessStartMode.normal
class Example {}

class Int64Like implements Int64LikeBase{
Expand Down Expand Up @@ -374,5 +376,13 @@ void main() {
expect(function.source.fragment, isEmpty);
expect(function.accessor, '_privateFunction');
});

test('should decode public static fields backed by private constructors',
() {
final staticFieldWithPrivateImpl = constants[13].revive();
expect(staticFieldWithPrivateImpl.accessor, 'ProcessStartMode.normal');
expect(staticFieldWithPrivateImpl.isPrivate, isFalse);
expect(staticFieldWithPrivateImpl.source.fragment, isEmpty);
});
});
}