Skip to content

Commit e32b7e2

Browse files
DRY up
1 parent c23460d commit e32b7e2

1 file changed

Lines changed: 25 additions & 27 deletions

File tree

lib/src/builder/parsing/meta.dart

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ import '../vendor/transformer_utils/src/analyzer_helpers.dart';
2323

2424
/// Returns the first annotation AST node on [node] of type [T],
2525
/// or null if no matching annotations are found.
26-
Annotation? _getMatchingAnnotation<T extends Object>(AnnotatedNode node) {
27-
final annotationClass = getAnnotationClassFromGeneric<T>();
28-
if (annotationClass == null) return null;
26+
Annotation? _getMatchingAnnotationFromGeneric<T extends Object>(AnnotatedNode node) =>
27+
_getMatchingAnnotation(_AnnotationClass.fromGeneric<T>(), node);
2928

30-
return node.metadata.firstWhereOrNull((m) => m.name.name == annotationClass.className);
31-
}
29+
/// Returns the first annotation AST node on [node] of type [annotationClass],
30+
/// or null if no matching annotations are found.
31+
Annotation? _getMatchingAnnotation(_AnnotationClass annotationClass, AnnotatedNode node) =>
32+
node.metadata.firstWhereOrNull((m) => m.name.name == annotationClass.className);
3233

3334
/// Utility class that allows partial instantiation of annotations, to support reading
3435
/// annotation data in a context without a resolved AST. See [isIncomplete] for more info.
@@ -52,7 +53,7 @@ class InstantiatedMeta<TMeta extends Object> {
5253
///
5354
/// The instantiated annotation will be available via [value].
5455
static InstantiatedMeta<T>? fromNode<T extends Object>(AnnotatedNode node) {
55-
final metaNode = _getMatchingAnnotation<T>(node);
56+
final metaNode = _getMatchingAnnotationFromGeneric<T>(node);
5657
if (metaNode == null) return null;
5758

5859
final unsupportedArguments = <Expression>[];
@@ -134,12 +135,9 @@ T? instantiateAnnotationTyped<T extends Object>(
134135
AnnotatedNode node, {
135136
dynamic Function(Expression argument)? onUnsupportedArgument,
136137
}) {
137-
// TODO DRY up
138-
final annotationClass = getAnnotationClassFromGeneric<T>();
139-
if (annotationClass == null) return null;
138+
final annotationClass = _AnnotationClass.fromGeneric<T>();
140139

141-
final annotation =
142-
node.metadata.firstWhereOrNull((m) => m.name.name == annotationClass.className);
140+
final annotation = _getMatchingAnnotation(annotationClass, node);
143141
if (annotation == null) return null;
144142

145143
final args = parseAnnotationArgs(annotation, onUnsupportedArgument: onUnsupportedArgument);
@@ -213,23 +211,23 @@ enum _AnnotationClass {
213211
final String className;
214212

215213
const _AnnotationClass(this.className);
216-
}
217214

218-
@visibleForTesting
219-
_AnnotationClass? getAnnotationClassFromGeneric<T>() {
220-
if (_isSubtypeOf<T, a.Props>()) return _AnnotationClass.props;
221-
if (_isSubtypeOf<T, a.AbstractProps>()) return _AnnotationClass.abstractProps;
222-
// ignore: deprecated_member_use_from_same_package
223-
if (_isSubtypeOf<T, a.PropsMixin>()) return _AnnotationClass.propsMixin;
224-
if (_isSubtypeOf<T, a.State>()) return _AnnotationClass.state;
225-
if (_isSubtypeOf<T, a.AbstractState>()) return _AnnotationClass.abstractState;
226-
// ignore: deprecated_member_use_from_same_package
227-
if (_isSubtypeOf<T, a.StateMixin>()) return _AnnotationClass.stateMixin;
228-
if (_isSubtypeOf<T, a.Component2>()) return _AnnotationClass.component2;
229-
// ignore: deprecated_member_use_from_same_package
230-
if (_isSubtypeOf<T, a.Component>()) return _AnnotationClass.component;
231-
if (_isSubtypeOf<T, a.Accessor>()) return _AnnotationClass.accessor;
232-
return null;
215+
static _AnnotationClass fromGeneric<T>() {
216+
if (_isSubtypeOf<T, a.Props>()) return _AnnotationClass.props;
217+
if (_isSubtypeOf<T, a.AbstractProps>()) return _AnnotationClass.abstractProps;
218+
// ignore: deprecated_member_use_from_same_package
219+
if (_isSubtypeOf<T, a.PropsMixin>()) return _AnnotationClass.propsMixin;
220+
if (_isSubtypeOf<T, a.State>()) return _AnnotationClass.state;
221+
if (_isSubtypeOf<T, a.AbstractState>()) return _AnnotationClass.abstractState;
222+
// ignore: deprecated_member_use_from_same_package
223+
if (_isSubtypeOf<T, a.StateMixin>()) return _AnnotationClass.stateMixin;
224+
if (_isSubtypeOf<T, a.Component2>()) return _AnnotationClass.component2;
225+
// ignore: deprecated_member_use_from_same_package
226+
if (_isSubtypeOf<T, a.Component>()) return _AnnotationClass.component;
227+
if (_isSubtypeOf<T, a.Accessor>()) return _AnnotationClass.accessor;
228+
229+
throw ArgumentError('Unsupported generic: $T. Must correspond to a type in this enum.');
230+
}
233231
}
234232

235233
bool _isSubtypeOf<T, S>() => _SubtypeOfHelper<T>() is _SubtypeOfHelper<S>;

0 commit comments

Comments
 (0)