Skip to content

Commit

Permalink
fix: Support reviving a reference to a function. (#321)
Browse files Browse the repository at this point in the history
* fix: Support reviving a reference to a function.

* Fix test breakage.
  • Loading branch information
matanlurey authored Mar 29, 2018
1 parent 2ea59ef commit 8bf5d08
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
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
};

0 comments on commit 8bf5d08

Please sign in to comment.