Skip to content

Commit 99315cb

Browse files
simolus3Commit Queue
authored andcommitted
[analyzer] Add DartObject.toRecordValue()
The `DartObject` class in the analyzer represents a known constant value. It has methods that convert that representation into a more specific one (e.g. `toListValue()` that returns a list of `DartObject`). While constant records can be represented as `DartObject`, there's no method to directly extract the positional and named fields from that instance. Users could extract the shape from the record type and then call `getField`, but that is an error-prone process. So, this adds `toRecordValue()`, which returns a list for positional and a map for named parameters when called on a `DartObject` representing a record. TEST=pkg/analyzer/test/src/dart/constant/value_test.dart Change-Id: I56f3e877ea78ab0f3ccfb7ac73ffda80ee4fe846 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/394860 Reviewed-by: Brian Wilkerson <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]> Reviewed-by: Samuel Rawlins <[email protected]>
1 parent 3bc27ff commit 99315cb

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

pkg/analyzer/lib/dart/constant/value.dart

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ abstract class DartObject {
121121
/// * the value of the object being represented is `null`.
122122
Map<DartObject?, DartObject?>? toMapValue();
123123

124+
/// If this [DartObject] represents a record, returns the positional and named
125+
/// fields of that record.
126+
///
127+
/// If the object being represented is not a [Record] `null` is returned
128+
/// instead.
129+
({List<DartObject> positional, Map<String, DartObject> named})?
130+
toRecordValue();
131+
124132
/// Return a set corresponding to the value of the object being represented,
125133
/// or `null` if
126134
/// * this object is not of type 'Set', or

pkg/analyzer/lib/src/dart/constant/value.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,16 @@ class DartObjectImpl implements DartObject, Constant {
924924
return null;
925925
}
926926

927+
@override
928+
({List<DartObject> positional, Map<String, DartObject> named})?
929+
toRecordValue() {
930+
if (state case RecordState(:var positionalFields, :var namedFields)) {
931+
return (positional: positionalFields, named: namedFields);
932+
} else {
933+
return null;
934+
}
935+
}
936+
927937
@override
928938
Set<DartObjectImpl>? toSetValue() {
929939
var state = this.state;

pkg/analyzer/test/src/dart/constant/value_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1928,6 +1928,25 @@ class DartObjectImplTest {
19281928
_assertTimes(_intValue(null), _intValue(null), _intValue(3));
19291929
}
19301930

1931+
void test_toRecordValue_notARecord() {
1932+
expect(
1933+
_listValue(_typeProvider.boolType, [_boolValue(true), _boolValue(false)])
1934+
.toRecordValue(),
1935+
isNull,
1936+
);
1937+
}
1938+
1939+
void test_toRecordValue_null() {
1940+
expect(_nullValue().toRecordValue(), isNull);
1941+
}
1942+
1943+
void test_toRecordValue_record() {
1944+
var constant = _recordValue([_intValue(1)], {'bool': _boolValue(true)});
1945+
var (:positional, :named) = constant.toRecordValue()!;
1946+
expect(positional, [_intValue(1)]);
1947+
expect(named, {'bool': _boolValue(true)});
1948+
}
1949+
19311950
/// Assert that the result of executing [fn] is the [expected] value, or, if
19321951
/// [expected] is `null`, that the operation throws an exception .
19331952
void _assert(DartObjectImpl? expected, DartObjectImpl? Function() fn) {

0 commit comments

Comments
 (0)