Skip to content

Commit 5c6892b

Browse files
committed
Fixed bug in isEnum.
1 parent ac34db0 commit 5c6892b

File tree

1 file changed

+86
-1
lines changed

1 file changed

+86
-1
lines changed

lib/src/extension/type_methods.dart

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
// ignore_for_file: type_literal_in_constant_pattern
2+
13
import 'package:analyzer/dart/constant/value.dart' show DartObject;
4+
import 'package:analyzer/dart/element/element.dart' show EnumElement;
25
import 'package:analyzer/dart/element/type.dart'
36
show DynamicType, DartType, ParameterizedType;
47

@@ -18,7 +21,8 @@ extension TypeMethods on DartObject {
1821

1922
// Returns `true` if `this` represents a constant expression
2023
/// with type [Enum].
21-
bool get isEnum => type?.isDartCoreEnum ?? false;
24+
// enum.computeConstantValue()?.type?.element is EnumElement.
25+
bool get isEnum => type?.element is EnumElement;
2226

2327
// Returns `true` if `this` represents a constant expression
2428
/// with type [int].
@@ -113,3 +117,84 @@ extension TypeMethods on DartObject {
113117
: <DartType>[];
114118
}
115119
}
120+
121+
typedef ListOfBool = List<bool>;
122+
typedef ListOfDouble = List<double>;
123+
typedef ListOfDynamic = List<dynamic>;
124+
typedef ListOfInt = List<int>;
125+
typedef ListOfNum = List<num>;
126+
typedef ListOfString = List<String>;
127+
typedef ListOfSymbol = List<Symbol>;
128+
typedef ListOfType = List<Type>;
129+
130+
extension GenericTypeMethods on DartType {
131+
/// Returns a list with elements of type [DartType]
132+
/// containing the type arguments if
133+
/// `this` is a [ParameterizedType] and and empty list else.
134+
List<DartType> get typeArgs {
135+
return (this is ParameterizedType)
136+
? (this as ParameterizedType).typeArguments
137+
: <DartType>[];
138+
}
139+
140+
bool isCoreType<T>() => switch (T) {
141+
(bool) when isDartCoreBool => true,
142+
(double) when isDartCoreDouble => true,
143+
(int) when isDartCoreInt => true,
144+
(num) when isDartCoreNum => true,
145+
(String) when isDartCoreString => true,
146+
(Symbol) when isDartCoreSymbol => true,
147+
(Type) when isDartCoreType => true,
148+
(dynamic) when this is DynamicType => true,
149+
150+
(ListOfBool)
151+
when isDartCoreList &&
152+
typeArgs.isNotEmpty &&
153+
typeArgs.first.isDartCoreBool =>
154+
true,
155+
156+
(ListOfDouble)
157+
when isDartCoreList &&
158+
typeArgs.isNotEmpty &&
159+
typeArgs.first.isDartCoreDouble =>
160+
true,
161+
162+
(ListOfDynamic)
163+
when isDartCoreList &&
164+
typeArgs.isNotEmpty &&
165+
typeArgs.first is DynamicType =>
166+
true,
167+
168+
(ListOfInt)
169+
when isDartCoreList &&
170+
typeArgs.isNotEmpty &&
171+
typeArgs.first.isDartCoreInt =>
172+
true,
173+
174+
(ListOfNum)
175+
when isDartCoreList &&
176+
typeArgs.isNotEmpty &&
177+
typeArgs.first.isDartCoreNum =>
178+
true,
179+
180+
(ListOfString)
181+
when isDartCoreList &&
182+
typeArgs.isNotEmpty &&
183+
typeArgs.first.isDartCoreString =>
184+
true,
185+
186+
(ListOfSymbol)
187+
when isDartCoreList &&
188+
typeArgs.isNotEmpty &&
189+
typeArgs.first.isDartCoreSymbol =>
190+
true,
191+
192+
(ListOfType)
193+
when isDartCoreList &&
194+
typeArgs.isNotEmpty &&
195+
typeArgs.first.isDartCoreType =>
196+
true,
197+
198+
_ => false,
199+
};
200+
}

0 commit comments

Comments
 (0)