Skip to content

Commit d6ff119

Browse files
bwilkersonCommit Bot
authored and
Commit Bot
committed
Partial implementation of record types in DartEditBuilder
While the work is incomplete (see the TODOs), this fixes an exception that I saw this morning and is, I believe, a step in the right direction. Change-Id: I6adc0e0df4ea9b64ea0c5d1dcd42a078d30c4a9b Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/258507 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Brian Wilkerson <[email protected]>
1 parent 168e744 commit d6ff119

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

pkg/analyzer_plugin/lib/src/utilities/change_builder/change_builder_dart.dart

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,12 @@ class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder {
906906
return true;
907907
}
908908

909+
if (type is RecordType) {
910+
// TODO(brianwilkerson) This should return `false` if the `records`
911+
// feature is not enabled.
912+
return true;
913+
}
914+
909915
throw UnimplementedError('(${type.runtimeType}) $type');
910916
}
911917

@@ -1279,6 +1285,50 @@ class DartEditBuilderImpl extends EditBuilderImpl implements DartEditBuilder {
12791285
return true;
12801286
}
12811287

1288+
if (type is RecordType) {
1289+
// TODO(brianwilkerson) This should return `false` if the `records`
1290+
// feature is not enabled. More importantly, we can't currently return
1291+
// `false` if some portion of a type has already been written, so we
1292+
// need to figure out what to do when a record type is nested in another
1293+
// type in a context where it isn't allowed. For example, we might
1294+
// enhance `_canWriteType` to be recursive, then guard all invocations of
1295+
// this method with a call to `_canWriteType` (and remove the return type
1296+
// from this method).
1297+
write('(');
1298+
var isFirst = true;
1299+
for (var field in type.positionalFields) {
1300+
if (isFirst) {
1301+
isFirst = false;
1302+
} else {
1303+
write(', ');
1304+
}
1305+
_writeType(field.type);
1306+
}
1307+
var namedFields = type.namedFields;
1308+
if (namedFields.isNotEmpty) {
1309+
if (isFirst) {
1310+
write('{');
1311+
} else {
1312+
write(', {');
1313+
}
1314+
isFirst = true;
1315+
for (var field in namedFields) {
1316+
if (isFirst) {
1317+
isFirst = false;
1318+
} else {
1319+
write(', ');
1320+
}
1321+
_writeType(field.type);
1322+
write(' ');
1323+
write(field.name);
1324+
}
1325+
write('}');
1326+
}
1327+
write(')');
1328+
_writeTypeNullability(type);
1329+
return true;
1330+
}
1331+
12821332
throw UnimplementedError('(${type.runtimeType}) $type');
12831333
}
12841334

pkg/analyzer_plugin/test/src/utilities/change_builder/change_builder_dart_test.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,22 @@ class DartEditBuilderImpl_WithNullSafetyTest extends DartEditBuilderImplTest {
6161
Future<void> test_writeType_Never_question() async {
6262
await _assertWriteType('Never?');
6363
}
64+
65+
Future<void> test_writeType_recordType_mixed() async {
66+
await _assertWriteType('(int, {int y})');
67+
}
68+
69+
Future<void> test_writeType_recordType_named() async {
70+
await _assertWriteType('({int x, int y})');
71+
}
72+
73+
Future<void> test_writeType_recordType_nullable() async {
74+
await _assertWriteType('(int, {int y})?');
75+
}
76+
77+
Future<void> test_writeType_recordType_positional() async {
78+
await _assertWriteType('(int, int)');
79+
}
6480
}
6581

6682
@reflectiveTest

0 commit comments

Comments
 (0)