|
5 | 5 | import 'package:analyzer/dart/constant/value.dart'; |
6 | 6 | import 'package:analyzer/dart/element/element.dart'; |
7 | 7 |
|
| 8 | +import 'type_checker.dart'; |
| 9 | + |
8 | 10 | /// Throws an exception if [root] or its super(s) does not contain [name]. |
9 | 11 | void _assertHasField(ClassElement root, String name) { |
10 | 12 | var element = root; |
@@ -95,6 +97,9 @@ abstract class ConstantReader { |
95 | 97 | /// Returns whether this constant represents `null`. |
96 | 98 | bool get isNull; |
97 | 99 |
|
| 100 | + /// Returns whether this constant matches [checker]. |
| 101 | + bool instanceOf(TypeChecker checker); |
| 102 | + |
98 | 103 | /// Reads[ field] from the constant as another constant value. |
99 | 104 | ConstantReader read(String field); |
100 | 105 | } |
@@ -140,6 +145,9 @@ class _NullConstant implements ConstantReader { |
140 | 145 | @override |
141 | 146 | bool get isString => false; |
142 | 147 |
|
| 148 | + @override |
| 149 | + bool instanceOf(TypeChecker checker) => false; |
| 150 | + |
143 | 151 | @override |
144 | 152 | ConstantReader read(_) => throw new UnsupportedError('Null'); |
145 | 153 | } |
@@ -176,17 +184,21 @@ class _Constant implements ConstantReader { |
176 | 184 | bool get isInt => _object.toIntValue() != null; |
177 | 185 |
|
178 | 186 | @override |
179 | | - bool get isList => _object?.toListValue() != null; |
| 187 | + bool get isList => _object.toListValue() != null; |
180 | 188 |
|
181 | 189 | @override |
182 | 190 | bool get isNull => _isNull(_object); |
183 | 191 |
|
184 | 192 | @override |
185 | | - bool get isMap => _object?.toMapValue() != null; |
| 193 | + bool get isMap => _object.toMapValue() != null; |
186 | 194 |
|
187 | 195 | @override |
188 | 196 | bool get isString => _object.toStringValue() != null; |
189 | 197 |
|
| 198 | + @override |
| 199 | + bool instanceOf(TypeChecker checker) => |
| 200 | + checker.isAssignableFromType(_object.type); |
| 201 | + |
190 | 202 | @override |
191 | 203 | ConstantReader read(String field) { |
192 | 204 | final constant = new ConstantReader(_getFieldRecursive(_object, field)); |
|
0 commit comments