From 0052d4dd10991f90f44a7702b124b9b729115598 Mon Sep 17 00:00:00 2001 From: Matan Lurey Date: Wed, 5 Jul 2017 21:22:26 -0700 Subject: [PATCH] Add annotationsOfExact. (dart-lang/source_gen#200) --- source_gen/source_gen/CHANGELOG.md | 7 +++++ .../source_gen/lib/src/type_checker.dart | 26 ++++++++++++++----- source_gen/source_gen/pubspec.yaml | 2 +- .../source_gen/test/type_checker_test.dart | 2 ++ 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/source_gen/source_gen/CHANGELOG.md b/source_gen/source_gen/CHANGELOG.md index 127e42f2d..bd7803aaf 100644 --- a/source_gen/source_gen/CHANGELOG.md +++ b/source_gen/source_gen/CHANGELOG.md @@ -1,3 +1,10 @@ +## 0.6.0 + +* **Breaking change**: `TypeChecker#annotationsOf|firstAnnotationOf` now + returns annotations that are _assignable_ to the `TypeChecker`'s type. As a + result we've added `#annotationsOfExact|firstAnnotationOfExact` which has the + old behavior for precise checks. + ## 0.5.10+1 * Update minimum `analyzer` package to `0.29.10`. diff --git a/source_gen/source_gen/lib/src/type_checker.dart b/source_gen/source_gen/lib/src/type_checker.dart index 3c24864c5..5e91470e5 100644 --- a/source_gen/source_gen/lib/src/type_checker.dart +++ b/source_gen/source_gen/lib/src/type_checker.dart @@ -38,7 +38,7 @@ abstract class TypeChecker { /// package like in the `dart:` SDK. const factory TypeChecker.fromUrl(dynamic url) = _UriTypeChecker; - /// Returns the first constant annotating [element] that is this type. + /// Returns the first constant annotating [element] assignable to this type. /// /// Otherwise returns `null`. DartObject firstAnnotationOf(Element element) { @@ -49,18 +49,30 @@ abstract class TypeChecker { return results.isEmpty ? null : results.first; } - /// Returns every constant annotating [element] that is this type. + /// Returns the first constant annotating [element] that is exactly this type. + DartObject firstAnnotationOfExact(Element element) { + if (element.metadata.isEmpty) { + return null; + } + final results = annotationsOfExact(element); + return results.isEmpty ? null : results.first; + } + + /// Returns annotating constants on [element] assignable to this type. Iterable annotationsOf(Element element) => element.metadata + .map((a) => a.computeConstantValue()) + .where((a) => isAssignableFromType(a.type)); + + /// Returns annotating constants on [element] of exactly this type. + Iterable annotationsOfExact(Element element) => element.metadata .map((a) => a.computeConstantValue()) .where((a) => isExactlyType(a.type)); - /// Returns `true` if the type of [element] can be assigned to the type - /// represented by `this`. + /// Returns `true` if the type of [element] can be assigned to this type. bool isAssignableFrom(Element element) => isExactly(element) || _getAllSupertypes(element).any(isExactlyType); - /// Returns `true` if [staticType] can be assigned to the type represented - /// by `this`. + /// Returns `true` if [staticType] can be assigned to this type. bool isAssignableFromType(DartType staticType) => isAssignableFrom(staticType.element); @@ -97,7 +109,7 @@ abstract class TypeChecker { bool isSuperTypeOf(DartType staticType) => isSuperOf(staticType.element); } -//TODO(kevmoo) Remove when bug with `ClassElement.allSupertypes` is fixed +// TODO(kevmoo) Remove when bug with `ClassElement.allSupertypes` is fixed // https://github.com/dart-lang/sdk/issues/29767 Iterable _getAllSupertypes(Element element) sync* { if (element is ClassElement) { diff --git a/source_gen/source_gen/pubspec.yaml b/source_gen/source_gen/pubspec.yaml index 725a6e844..4e73c1219 100644 --- a/source_gen/source_gen/pubspec.yaml +++ b/source_gen/source_gen/pubspec.yaml @@ -1,5 +1,5 @@ name: source_gen -version: 0.5.10+1 +version: 0.6.0-dev author: Dart Team description: Automated source code generation for Dart. homepage: https://github.com/dart-lang/source_gen diff --git a/source_gen/source_gen/test/type_checker_test.dart b/source_gen/source_gen/test/type_checker_test.dart index b13cd6c6c..d5e6d8bc8 100644 --- a/source_gen/source_gen/test/type_checker_test.dart +++ b/source_gen/source_gen/test/type_checker_test.dart @@ -2,6 +2,8 @@ // for details. All rights reserved. Use of this source code is governed by a // BSD-style license that can be found in the LICENSE file. +// Increase timeouts on this test which resolves source code and can be slow. +@Timeout.factor(2.0) import 'dart:collection'; import 'package:analyzer/dart/element/type.dart';