Skip to content

Commit 3fba5fb

Browse files
srawlinsCommit Queue
authored andcommitted
DAS: Seal TypeDecl for simpler switches
Change-Id: I8b61f7e5fcace3438ae3692ca191975abe132879 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/401320 Reviewed-by: Konstantin Shcheglov <[email protected]> Commit-Queue: Samuel Rawlins <[email protected]>
1 parent 946f328 commit 3fba5fb

File tree

2 files changed

+15
-27
lines changed

2 files changed

+15
-27
lines changed

pkg/analysis_server/tool/spec/api.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ class Request extends ApiNode {
340340
}
341341

342342
/// Base class for all possible types.
343-
abstract class TypeDecl extends ApiNode {
343+
sealed class TypeDecl extends ApiNode {
344344
TypeDecl(super.html, {super.experimental, super.deprecated});
345345

346346
T accept<T>(ApiVisitor<T> visitor);

pkg/analysis_server/tool/spec/codegen_inttest_methods.dart

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -54,35 +54,23 @@ class CodegenInttestMethodsVisitor extends DartCodegenVisitor
5454
String formatArgument(TypeObjectField field) =>
5555
'${fieldDartType(field)} ${field.name}';
5656

57-
/// Figure out the appropriate Dart type for data having the given API
57+
/// Figures out the appropriate Dart type for data having the given API
5858
/// protocol [type].
5959
String jsonType(TypeDecl type) {
6060
type = resolveTypeReferenceChain(type);
61-
if (type is TypeEnum) {
62-
return 'String';
63-
} else if (type is TypeList) {
64-
return 'List<${jsonType(type.itemType)}>';
65-
} else if (type is TypeMap) {
66-
return 'Map<String, ${jsonType(type.valueType)}>';
67-
} else if (type is TypeObject) {
68-
return 'Map<String, dynamic>';
69-
} else if (type is TypeReference) {
70-
switch (type.typeName) {
71-
case 'String':
72-
case 'int':
73-
case 'bool':
74-
// These types correspond exactly to Dart types
75-
return type.typeName;
76-
case 'object':
77-
return 'Map<String, dynamic>';
78-
default:
79-
throw Exception(type.typeName);
80-
}
81-
} else if (type is TypeUnion) {
82-
return 'Object';
83-
} else {
84-
throw Exception('Unexpected kind of TypeDecl');
85-
}
61+
return switch (type) {
62+
TypeEnum() => 'String',
63+
TypeList() => 'List<${jsonType(type.itemType)}>',
64+
TypeMap() => 'Map<String, ${jsonType(type.valueType)}>',
65+
TypeObject() => 'Map<String, dynamic>',
66+
TypeReference() => switch (type.typeName) {
67+
// These types correspond exactly to Dart types.
68+
'String' || 'int' || 'bool' => type.typeName,
69+
'object' => 'Map<String, dynamic>',
70+
_ => throw Exception(type.typeName),
71+
},
72+
TypeUnion() => 'Object',
73+
};
8674
}
8775

8876
@override

0 commit comments

Comments
 (0)