Skip to content

Commit 122b61b

Browse files
committed
Added type methods.
1 parent 0bc3504 commit 122b61b

File tree

3 files changed

+142
-0
lines changed

3 files changed

+142
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extension NullableTypeCheck on Object? {
2+
/// Returns `true` if the object is null, and `false` otherwise.
3+
bool get isNull => this == null ? true : false;
4+
5+
/// Returns `true` if the object is not null, and `false` otherwise.
6+
bool get isNotNull => this == null ? false : true;
7+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
import 'package:analyzer/dart/constant/value.dart' show DartObject;
2+
import 'package:analyzer/dart/element/type.dart'
3+
show DynamicType, DartType, ParameterizedType;
4+
5+
/// Extension adding the type methods to `ConstantReader`.
6+
extension TypeMethods on DartObject {
7+
// Returns `true` if `this` represents a constant expression
8+
/// with type [double].
9+
bool get isBool => type?.isDartCoreBool ?? false;
10+
11+
// Returns `true` if `this` represents a constant expression
12+
/// with type
13+
bool get isDouble => type?.isDartCoreDouble ?? false;
14+
15+
/// Returns `true` if `this` represents a constant expression
16+
/// with type [dynamic].
17+
bool get isDynamic => type is DynamicType;
18+
19+
// Returns `true` if `this` represents a constant expression
20+
/// with type [Enum].
21+
bool get isEnum => type?.isDartCoreEnum ?? false;
22+
23+
// Returns `true` if `this` represents a constant expression
24+
/// with type [int].
25+
bool get isInt => type?.isDartCoreInt ?? false;
26+
27+
/// Returns `true` is `this` represents a constant expression with
28+
/// type exactly [Iterable]`.
29+
///
30+
/// Note: Returns `false` if the static type represents `List` or `Set`.
31+
bool get isIterable => type?.isDartCoreIterable ?? false;
32+
33+
// Returns `true` if `this` represents a constant expression
34+
/// with type [List].
35+
bool get isList => type?.isDartCoreList ?? false;
36+
37+
// Returns `true` if `this` represents a constant expression
38+
/// with type that is not [List].
39+
bool get isNotList => !isList;
40+
41+
// Returns `true` if `this` represents a constant expression
42+
/// with type [Map].
43+
bool get isMap => type?.isDartCoreMap ?? false;
44+
45+
// Returns `true` if `this` represents a constant expression
46+
/// with type that is not [Map].
47+
bool get isNotMap => !isMap;
48+
49+
// Returns `true` if `this` represents a constant expression
50+
/// with type [Set].
51+
bool get isSet => type?.isDartCoreSet ?? false;
52+
53+
// Returns `true` if `this` represents a constant expression
54+
/// with a type that is not [Set].
55+
bool get isNotSet => !isSet;
56+
57+
// Returns `true` if `this` represents a constant expression
58+
/// with type [Null].
59+
bool get isNull => type?.isDartCoreNull ?? false;
60+
61+
// Returns `true` if `this` represents a constant expression
62+
/// with type [num].
63+
bool get isNum => type?.isDartCoreNum ?? false;
64+
65+
// Returns `true` if `this` represents a constant expression
66+
/// with type [Object].
67+
bool get isObject => type?.isDartCoreObject ?? false;
68+
69+
// Returns `true` if `this` represents a constant expression
70+
/// with type [Record].
71+
bool get isRecord => type?.isDartCoreRecord ?? false;
72+
73+
// Returns `true` if `this` represents a constant expression
74+
/// with type [String].
75+
bool get isString => type?.isDartCoreString ?? false;
76+
77+
// Returns `true` if `this` represents a constant expression
78+
/// with type [Symbol].
79+
bool get isSymbol => type?.isDartCoreSymbol ?? false;
80+
81+
// Returns `true` if `this` represents a constant expression
82+
/// with type [Type].
83+
bool get isType => type?.isDartCoreType ?? false;
84+
85+
/// Returns `true` if the static type represents a
86+
/// `List`, `Set`, `Map`, or `Iterable`.
87+
bool get isCollection => isList || isSet || isMap || isIterable;
88+
89+
/// Returns `true` if the static type *and* the static type argument
90+
/// represent a `List`, `Set`, `Map`, or `Iterable`
91+
bool get isRecursiveCollection {
92+
if (isNotCollection) return false;
93+
final typeArg = dartTypeArgs[0];
94+
if (typeArg.isDartCoreIterable ||
95+
typeArg.isDartCoreList ||
96+
typeArg.isDartCoreSet ||
97+
typeArg.isDartCoreMap) {
98+
return true;
99+
} else {
100+
return false;
101+
}
102+
}
103+
104+
/// Returns `true` if the static type does not represent
105+
/// `List`, `Set`, `Map`, or `Iterable`.
106+
bool get isNotCollection => !isList && !isSet && !isMap && !isIterable;
107+
108+
/// Returns a `List` of type arguments or an empty list.
109+
List<DartType> get dartTypeArgs {
110+
final dartType = type;
111+
return dartType is ParameterizedType
112+
? dartType.typeArguments
113+
: <DartType>[];
114+
}
115+
}

lib/src/type/type_utils.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// Returns `true` if the derived type [D] is a subtype of base type [B],
2+
/// and `false` otherwise.
3+
bool isSubType<D, B>() => Iterable<D>.empty() is Iterable<B>;
4+
5+
/// Returns `true` if [T] is exactly the same type as [S], and `false` otherwise.
6+
bool isExactly<T, S>() => isSubType<T, S>() && isSubType<S, T>();
7+
8+
/// Returns `true` if [T] is nullable and `false` otherwise.
9+
bool isNullable<T>() {
10+
return Iterable<T>.empty is Iterable<T?>;
11+
}
12+
13+
/// Returns `true` if `T` is a Dart `enum`.
14+
///
15+
/// Note: `T` must not be `dynamic`.
16+
bool isEnum<T>() => isSubType<Enum, T>();
17+
18+
/// Returns the [Type] representing [T]. Useful to get a instance [Type]
19+
/// representing a parameterized type.
20+
Type getType<T>() => T;

0 commit comments

Comments
 (0)