|
| 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 | +} |
0 commit comments