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

Add TypeChecker.any #202

Merged
merged 7 commits into from
Jul 6, 2017
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
* `TypeChecker#annotations...`-methods now throw a `StateError` if one or more
annotations on an element are not resolvable. This is usually a sign of a
mispelling, missing import, or missing dependency.

* Added `TypeChecker.any`, which delegates to multiple other `TypeChecker`
implementations when making a type check.

## 0.5.10+1

Expand Down
28 changes: 25 additions & 3 deletions lib/src/type_checker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ import 'utils.dart';
abstract class TypeChecker {
const TypeChecker._();

/// Creates a new [TypeChecker] that delegates to other [checkers].
///
/// This implementation will return `true` for type checks if _any_ of the
/// provided type checkers return true, which is useful for deprecating an
/// API:
/// ```dart
/// const $Foo = const TypeChecker.fromRuntime(Foo);
/// const $Bar = const TypeChecker.fromRuntime(Bar);
///
/// // Used until $Foo is deleted.
/// const $FooOrBar = const TypeChecker.forAny(const [$Foo, $Bar]);
/// ```
const factory TypeChecker.any(Iterable<TypeChecker> checkers) = _AnyChecker;

/// Create a new [TypeChecker] backed by a runtime [type].
///
/// This implementation uses `dart:mirrors` (runtime reflection).
Expand Down Expand Up @@ -71,14 +85,13 @@ abstract class TypeChecker {
/// Returns annotating constants on [element] assignable to this type.
Iterable<DartObject> annotationsOf(Element element) => element.metadata
.map(_checkedConstantValue)
.where((a) => isAssignableFromType(a.type));
.where((a) => a?.type != null && isAssignableFromType(a.type));

/// Returns annotating constants on [element] of exactly this type.
Iterable<DartObject> annotationsOfExact(Element element) => element.metadata
.map(_checkedConstantValue)
.where((a) => a?.type != null && isAssignableFromType(a.type));
.where((a) => a?.type != null && isExactlyType(a.type));

/// Returns `true` if the type of [element] can be assigned to this type.
/// Returns `true` if the type of [element] can be assigned to this type.
bool isAssignableFrom(Element element) =>
isExactly(element) || _getAllSupertypes(element).any(isExactlyType);
Expand Down Expand Up @@ -210,3 +223,12 @@ class _UriTypeChecker extends TypeChecker {
@override
String toString() => '${uri}';
}

class _AnyChecker extends TypeChecker {
final Iterable<TypeChecker> _checkers;

const _AnyChecker(this._checkers) : super._();

@override
bool isExactly(Element element) => _checkers.any((c) => c.isExactly(element));
}
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.6.0-dev
version: 0.6.0
author: Dart Team <[email protected]>
description: Automated source code generation for Dart.
homepage: https://github.com/dart-lang/source_gen
Expand Down
10 changes: 9 additions & 1 deletion test/type_checker_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ void main() {
'package:source_gen/src/generator_for_annotation.dart#GeneratorForAnnotation'));
});

test('should gracefully when something is not resolvable', () async {
test('should fail gracefully when something is not resolvable', () async {
final resolver = await resolveSource(r'''
library _test;

Expand All @@ -194,4 +194,12 @@ void main() {
expect(() => $deprecated.annotationsOf(classX), throwsStateError,
reason: 'deprecated was spelled wrong; no annotation can be resolved');
});

test('should check multiple checkers', () {
final listOrMap = const TypeChecker.any(const [
const TypeChecker.fromRuntime(List),
const TypeChecker.fromRuntime(Map),
]);
expect(listOrMap.isExactlyType(staticMap), isTrue);
});
}