Skip to content
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
* **BREAKING** removed the deprecated `requireLibraryDirective` parameter in
`PartBuilder`.

* `Revivable` no longer throws a type error when attempting to revive a
reference to a top-level function or static-class method. Now is returns a
reference to that function or method, as expected.

## 0.7.6

* `TypeChecker` now throws an `UnresolvedAnnotationException` with a more
Expand Down
17 changes: 15 additions & 2 deletions lib/src/constants/revive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,21 @@ import '../utils.dart';
/// build tool(s) using this library to surface error messages to the user.
Revivable reviveInstance(DartObject object, [LibraryElement origin]) {
origin ??= object.type.element.library;
var url = Uri.parse(urlOfElement(object.type.element));
final clazz = object?.type?.element as ClassElement;
final element = object.type.element;
var url = Uri.parse(urlOfElement(element));
if (element is FunctionElement) {
return new Revivable._(
source: url.removeFragment(),
accessor: element.name,
);
}
if (element is MethodElement && element.isStatic) {
return new Revivable._(
source: url.removeFragment(),
accessor: '${element.enclosingElement.name}.${element.name}',
);
}
final clazz = element as ClassElement;
// Enums are not included in .definingCompilationUnit.types.
if (clazz.isEnum) {
for (final e in clazz.fields.where(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: source_gen
version: 0.8.0-dev
version: 0.8.0
author: Dart Team <[email protected]>
description: Automated source code generation for Dart.
homepage: https://github.com/dart-lang/source_gen
Expand Down
22 changes: 22 additions & 0 deletions test/constants_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ void main() {
@VisibleClass.secret()
@fieldOnly
@ClassWithStaticField.staticField
@Wrapper(someFunction)
@Wrapper(Wrapper.someFunction)
class Example {}

class Int64Like {
Expand Down Expand Up @@ -238,6 +240,14 @@ void main() {
static const staticField = const ClassWithStaticField._();
const ClassWithStaticField._();
}

class Wrapper {
static void someFunction(int x, String y) {}
final Function f;
const Wrapper(this.f);
}

void someFunction(int x, String y) {}
''', (resolver) => resolver.findLibraryByName('test_lib'));
constants = library
.getType('Example')
Expand Down Expand Up @@ -293,5 +303,17 @@ void main() {
expect(fieldOnly.source.fragment, isEmpty);
expect(fieldOnly.accessor, 'ClassWithStaticField.staticField');
});

test('should decode top-level functions', () {
final fieldOnly = constants[7].read('f').revive();
expect(fieldOnly.source.fragment, isEmpty);
expect(fieldOnly.accessor, 'someFunction');
});

test('should decode static-class functions', () {
final fieldOnly = constants[8].read('f').revive();
expect(fieldOnly.source.fragment, isEmpty);
expect(fieldOnly.accessor, 'Wrapper.someFunction');
});
});
}
2 changes: 1 addition & 1 deletion test/test_files/annotations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ const objectAnnotation = const {
'null': null,
'String': 'a string',
'core type': bool,
'imported sdk type': collection.Maps,
'imported sdk type': collection.Queue,
'non-core type': OtherPublicAnnotationClass
};