Skip to content

Commit fffde65

Browse files
authored
Bug fix for Revivable, and prepare to release. (#303)
* Bug fix for Revivable. * Feedback.
1 parent 19a3360 commit fffde65

File tree

3 files changed

+41
-9
lines changed

3 files changed

+41
-9
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
## 0.7.4+2
22

3+
* **BUG FIX**: `ConstantReader.revive()` now properly returns no URL fragment
4+
when the constant expression is resolved from a top-level or static-field.
5+
The documentation had said otherwise, and it was impossible to tell the
6+
difference between a constructor and a field. _Now_, fields are always in
7+
the form of accessor = `{clazz}.{field}` or `{topLevelField}`.
8+
39
* Fix file URIs on windows.
410

511
## 0.7.4+1

lib/src/constants/revive.dart

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,24 @@ Revivable reviveInstance(DartObject object, [LibraryElement origin]) {
2222
origin ??= object.type.element.library;
2323
var url = Uri.parse(urlOfElement(object.type.element));
2424
final clazz = object?.type?.element as ClassElement;
25-
for (final e in clazz.fields.where(
26-
(f) => f.isPublic && f.isConst && f.computeConstantValue() == object,
27-
)) {
28-
return new Revivable._(source: url, accessor: e.name);
25+
// Enums are not included in .definingCompilationUnit.types.
26+
if (clazz.isEnum) {
27+
for (final e in clazz.fields.where(
28+
(f) => f.isPublic && f.isConst && f.computeConstantValue() == object)) {
29+
return new Revivable._(
30+
source: url.removeFragment(),
31+
accessor: '${clazz.name}.${e.name}',
32+
);
33+
}
34+
}
35+
for (final e in origin.definingCompilationUnit.types
36+
.expand((t) => t.fields)
37+
.where((f) =>
38+
f.isPublic && f.isConst && f.computeConstantValue() == object)) {
39+
return new Revivable._(
40+
source: url.removeFragment(),
41+
accessor: '${clazz.name}.${e.name}',
42+
);
2943
}
3044
final i = (object as DartObjectImpl).getInvocation();
3145
if (i != null &&

test/constants_test.dart

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ void main() {
194194
@MapLike()
195195
@VisibleClass.secret()
196196
@fieldOnly
197+
@ClassWithStaticField.staticField
197198
class Example {}
198199
199200
class Int64Like {
@@ -230,8 +231,13 @@ void main() {
230231
class _FieldOnlyVisible {
231232
const _FieldOnlyVisible();
232233
}
233-
234+
234235
const fieldOnly = const _FieldOnlyVisible();
236+
237+
class ClassWithStaticField {
238+
static const staticField = const ClassWithStaticField._();
239+
const ClassWithStaticField._();
240+
}
235241
''', (resolver) => resolver.findLibraryByName('test_lib'));
236242
constants = library
237243
.getType('Example')
@@ -242,8 +248,8 @@ void main() {
242248

243249
test('should decode Int64Like.ZERO', () {
244250
final int64Like0 = constants[0].revive();
245-
expect(int64Like0.source.toString(), endsWith('#Int64Like'));
246-
expect(int64Like0.accessor, 'ZERO');
251+
expect(int64Like0.source.fragment, isEmpty);
252+
expect(int64Like0.accessor, 'Int64Like.ZERO');
247253
});
248254

249255
test('should decode Duration', () {
@@ -260,8 +266,8 @@ void main() {
260266

261267
test('should decode enums', () {
262268
final enumField1 = constants[2].revive();
263-
expect(enumField1.source.toString(), endsWith('#Enum'));
264-
expect(enumField1.accessor, 'field1');
269+
expect(enumField1.source.fragment, isEmpty);
270+
expect(enumField1.accessor, 'Enum.field1');
265271
});
266272

267273
test('should decode forwarding factories', () {
@@ -281,5 +287,11 @@ void main() {
281287
expect(fieldOnly.source.fragment, isEmpty);
282288
expect(fieldOnly.accessor, 'fieldOnly');
283289
});
290+
291+
test('should decode static fields', () {
292+
final fieldOnly = constants[6].revive();
293+
expect(fieldOnly.source.fragment, isEmpty);
294+
expect(fieldOnly.accessor, 'ClassWithStaticField.staticField');
295+
});
284296
});
285297
}

0 commit comments

Comments
 (0)