Skip to content

Commit 0593ca3

Browse files
authored
Add annotationsOfExact. (#200)
1 parent 4c50faf commit 0593ca3

File tree

4 files changed

+29
-8
lines changed

4 files changed

+29
-8
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## 0.6.0
2+
3+
* **Breaking change**: `TypeChecker#annotationsOf|firstAnnotationOf` now
4+
returns annotations that are _assignable_ to the `TypeChecker`'s type. As a
5+
result we've added `#annotationsOfExact|firstAnnotationOfExact` which has the
6+
old behavior for precise checks.
7+
18
## 0.5.10+1
29

310
* Update minimum `analyzer` package to `0.29.10`.

lib/src/type_checker.dart

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ abstract class TypeChecker {
3838
/// package like in the `dart:` SDK.
3939
const factory TypeChecker.fromUrl(dynamic url) = _UriTypeChecker;
4040

41-
/// Returns the first constant annotating [element] that is this type.
41+
/// Returns the first constant annotating [element] assignable to this type.
4242
///
4343
/// Otherwise returns `null`.
4444
DartObject firstAnnotationOf(Element element) {
@@ -49,18 +49,30 @@ abstract class TypeChecker {
4949
return results.isEmpty ? null : results.first;
5050
}
5151

52-
/// Returns every constant annotating [element] that is this type.
52+
/// Returns the first constant annotating [element] that is exactly this type.
53+
DartObject firstAnnotationOfExact(Element element) {
54+
if (element.metadata.isEmpty) {
55+
return null;
56+
}
57+
final results = annotationsOfExact(element);
58+
return results.isEmpty ? null : results.first;
59+
}
60+
61+
/// Returns annotating constants on [element] assignable to this type.
5362
Iterable<DartObject> annotationsOf(Element element) => element.metadata
63+
.map((a) => a.computeConstantValue())
64+
.where((a) => isAssignableFromType(a.type));
65+
66+
/// Returns annotating constants on [element] of exactly this type.
67+
Iterable<DartObject> annotationsOfExact(Element element) => element.metadata
5468
.map((a) => a.computeConstantValue())
5569
.where((a) => isExactlyType(a.type));
5670

57-
/// Returns `true` if the type of [element] can be assigned to the type
58-
/// represented by `this`.
71+
/// Returns `true` if the type of [element] can be assigned to this type.
5972
bool isAssignableFrom(Element element) =>
6073
isExactly(element) || _getAllSupertypes(element).any(isExactlyType);
6174

62-
/// Returns `true` if [staticType] can be assigned to the type represented
63-
/// by `this`.
75+
/// Returns `true` if [staticType] can be assigned to this type.
6476
bool isAssignableFromType(DartType staticType) =>
6577
isAssignableFrom(staticType.element);
6678

@@ -97,7 +109,7 @@ abstract class TypeChecker {
97109
bool isSuperTypeOf(DartType staticType) => isSuperOf(staticType.element);
98110
}
99111

100-
//TODO(kevmoo) Remove when bug with `ClassElement.allSupertypes` is fixed
112+
// TODO(kevmoo) Remove when bug with `ClassElement.allSupertypes` is fixed
101113
// https://github.com/dart-lang/sdk/issues/29767
102114
Iterable<InterfaceType> _getAllSupertypes(Element element) sync* {
103115
if (element is ClassElement) {

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: source_gen
2-
version: 0.5.10+1
2+
version: 0.6.0-dev
33
author: Dart Team <[email protected]>
44
description: Automated source code generation for Dart.
55
homepage: https://github.com/dart-lang/source_gen

test/type_checker_test.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5+
// Increase timeouts on this test which resolves source code and can be slow.
6+
@Timeout.factor(2.0)
57
import 'dart:collection';
68

79
import 'package:analyzer/dart/element/type.dart';

0 commit comments

Comments
 (0)