Skip to content

Commit ec20773

Browse files
jakemac53Commit Queue
authored and
Commit Queue
committed
[macros] output better error messages for unrecognized types in the JSON macro language test
Change-Id: Ib672dcdcff11f376014a58a9b9f7630de8d2539a Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/349601 Commit-Queue: Morgan :) <[email protected]> Auto-Submit: Jake Macdonald <[email protected]> Reviewed-by: Morgan :) <[email protected]>
1 parent ec6e106 commit ec20773

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

tests/language/macros/json/json_serializable.dart

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ macro class FromJson implements ConstructorDefinitionMacro {
128128
'Only named types are allowed on serializable classes',
129129
target: type.asDiagnosticTarget),
130130
Severity.error));
131-
return RawCode.fromString('<Unable to deserialize type ${type.code}>');
131+
return RawCode.fromString(
132+
'<Unable to deserialize type ${type.code.debugString}>');
132133
}
133134
var typeDecl = await builder.typeDeclarationOf(type.identifier);
134135
while (typeDecl is TypeAliasDeclaration) {
@@ -140,7 +141,8 @@ macro class FromJson implements ConstructorDefinitionMacro {
140141
'type alias ${type.code} resolved to a ${aliasedType.code}.',
141142
target: type.asDiagnosticTarget),
142143
Severity.error));
143-
return RawCode.fromString('<Unable to deserialize type ${type.code}>');
144+
return RawCode.fromString(
145+
'<Unable to deserialize type ${type.code.debugString}>');
144146
}
145147
typeDecl = await builder.typeDeclarationOf(aliasedType.identifier);
146148
}
@@ -151,7 +153,8 @@ macro class FromJson implements ConstructorDefinitionMacro {
151153
'serializable classes',
152154
target: type.asDiagnosticTarget),
153155
Severity.error));
154-
return RawCode.fromString('<Unable to deserialize type ${type.code}>');
156+
return RawCode.fromString(
157+
'<Unable to deserialize type ${type.code.debugString}>');
155158
}
156159

157160
// The static type of the expected type, without any type arguments.
@@ -364,7 +367,8 @@ macro class ToJson implements MethodDefinitionMacro {
364367
'Only fields with named types are allowed on serializable classes',
365368
target: type.asDiagnosticTarget),
366369
Severity.error));
367-
return RawCode.fromString('<Unable to serialize type ${type.code}>');
370+
return RawCode.fromString(
371+
'<Unable to serialize type ${type.code.debugString}>');
368372
}
369373
var typeDecl = await builder.typeDeclarationOf(type.identifier);
370374
while (typeDecl is TypeAliasDeclaration) {
@@ -375,7 +379,8 @@ macro class ToJson implements MethodDefinitionMacro {
375379
'Only fields with named types are allowed on serializable classes',
376380
target: type.asDiagnosticTarget),
377381
Severity.error));
378-
return RawCode.fromString('<Unable to serialize type ${type.code}>');
382+
return RawCode.fromString(
383+
'<Unable to serialize type ${type.code.debugString}>');
379384
}
380385
typeDecl = await builder.typeDeclarationOf(aliasedType.identifier);
381386
}
@@ -385,7 +390,8 @@ macro class ToJson implements MethodDefinitionMacro {
385390
'Only classes are supported as field types for serializable classes',
386391
target: type.asDiagnosticTarget),
387392
Severity.error));
388-
return RawCode.fromString('<Unable to serialize type ${type.code}>');
393+
return RawCode.fromString(
394+
'<Unable to serialize type ${type.code.debugString}>');
389395
}
390396

391397
var typeDeclType = await builder.resolve(
@@ -497,3 +503,24 @@ extension _FirstWhereOrNull<T> on Iterable<T> {
497503
return null;
498504
}
499505
}
506+
507+
extension on Code {
508+
String get debugString {
509+
final buffer = StringBuffer();
510+
_writeDebugString(buffer);
511+
return buffer.toString();
512+
}
513+
514+
void _writeDebugString(StringBuffer buffer) {
515+
for (var part in parts) {
516+
switch(part) {
517+
case Code():
518+
part._writeDebugString(buffer);
519+
case Identifier():
520+
buffer.write(part.name);
521+
default:
522+
buffer.write(part);
523+
}
524+
}
525+
}
526+
}

0 commit comments

Comments
 (0)