Skip to content

Commit d5f0643

Browse files
scheglovCommit Queue
authored and
Commit Queue
committed
Macro. Test for diagnostic context messages.
Small tweaks to textual format. Change-Id: Id019d8ca8fc0a6688a1ce3e6c52b00d0a68c3337 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/337120 Commit-Queue: Konstantin Shcheglov <[email protected]> Reviewed-by: Brian Wilkerson <[email protected]>
1 parent 1c7fe71 commit d5f0643

File tree

3 files changed

+115
-28
lines changed

3 files changed

+115
-28
lines changed

pkg/analyzer/test/src/summary/element_text.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -734,19 +734,21 @@ class _ElementWriter {
734734

735735
void _writeMacroDiagnostics(Element e) {
736736
void writeMessage(MacroDiagnosticMessage object) {
737-
// Write the text.
737+
// Write the message.
738738
final validator = configuration.macroDiagnosticMessageValidator;
739739
if (validator != null) {
740740
validator(object.message);
741741
} else {
742-
var messageToWrite = object.message;
742+
final message = object.message;
743743
const stackTraceText = 'Stack trace:';
744-
final stackTraceIndex = messageToWrite.indexOf(stackTraceText);
744+
final stackTraceIndex = message.indexOf(stackTraceText);
745745
if (stackTraceIndex >= 0) {
746746
final end = stackTraceIndex + stackTraceText.length;
747-
messageToWrite = '${messageToWrite.substring(0, end)} <cut>';
747+
final withoutStackTrace = message.substring(0, end);
748+
_sink.writelnWithIndent('message:\n$withoutStackTrace <cut>');
749+
} else {
750+
_sink.writelnWithIndent('message: $message');
748751
}
749-
_sink.writeln(messageToWrite);
750752
}
751753
// Write the target.
752754
final target = object.target;
@@ -778,14 +780,19 @@ class _ElementWriter {
778780
case MacroDiagnostic():
779781
_sink.writelnWithIndent('MacroDiagnostic');
780782
_sink.withIndent(() {
781-
_sink.writelnWithIndent('message');
783+
_sink.writelnWithIndent('message: MacroDiagnosticMessage');
782784
_sink.withIndent(() {
783785
writeMessage(diagnostic.message);
784786
});
785787
_sink.writeElements(
786788
'contextMessages',
787789
diagnostic.contextMessages,
788-
writeMessage,
790+
(message) {
791+
_sink.writelnWithIndent('MacroDiagnosticMessage');
792+
_sink.withIndent(() {
793+
writeMessage(message);
794+
});
795+
},
789796
);
790797
_sink.writelnWithIndent(
791798
'severity: ${diagnostic.severity.name}',

pkg/analyzer/test/src/summary/macro/diagnostic.dart

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,30 @@ import 'package:_fe_analyzer_shared/src/macros/api.dart';
4545
}
4646
}
4747

48+
/*macro*/ class ReportWithContextMessages implements ClassDeclarationsMacro {
49+
const ReportWithContextMessages();
50+
51+
@override
52+
buildDeclarationsForClass(declaration, builder) async {
53+
final methods = await builder.methodsOf(declaration);
54+
builder.report(
55+
Diagnostic(
56+
DiagnosticMessage(
57+
'Reported message',
58+
target: declaration.asDiagnosticTarget,
59+
),
60+
Severity.warning,
61+
contextMessages: methods.map((method) {
62+
return DiagnosticMessage(
63+
'See ${method.identifier.name}',
64+
target: method.asDiagnosticTarget,
65+
);
66+
}).toList(),
67+
),
68+
);
69+
}
70+
}
71+
4872
/*macro*/ class ReportWithoutTargetError implements ClassTypesMacro {
4973
const ReportWithoutTargetError();
5074

pkg/analyzer/test/src/summary/macro_test.dart

Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,7 @@ library
23682368
class A @35
23692369
macroDiagnostics
23702370
MacroDiagnostic
2371-
message
2371+
message: MacroDiagnosticMessage
23722372
target: ApplicationMacroDiagnosticTarget
23732373
annotationIndex: 0
23742374
severity: error
@@ -2400,8 +2400,8 @@ library
24002400
class A @62
24012401
macroDiagnostics
24022402
MacroDiagnostic
2403-
message
2404-
Reported message
2403+
message: MacroDiagnosticMessage
2404+
message: Reported message
24052405
target: ElementMacroDiagnosticTarget
24062406
element: self::@class::A
24072407
severity: warning
@@ -2435,8 +2435,8 @@ library
24352435
@70
24362436
macroDiagnostics
24372437
MacroDiagnostic
2438-
message
2439-
Reported message
2438+
message: MacroDiagnosticMessage
2439+
message: Reported message
24402440
target: ElementMacroDiagnosticTarget
24412441
element: self::@class::A::@constructor::new
24422442
severity: warning
@@ -2474,8 +2474,8 @@ library
24742474
shouldUseTypeForInitializerInference: true
24752475
macroDiagnostics
24762476
MacroDiagnostic
2477-
message
2478-
Reported message
2477+
message: MacroDiagnosticMessage
2478+
message: Reported message
24792479
target: ElementMacroDiagnosticTarget
24802480
element: self::@class::A::@field::foo
24812481
severity: warning
@@ -2515,14 +2515,64 @@ library
25152515
returnType: void
25162516
macroDiagnostics
25172517
MacroDiagnostic
2518-
message
2519-
Reported message
2518+
message: MacroDiagnosticMessage
2519+
message: Reported message
25202520
target: ElementMacroDiagnosticTarget
25212521
element: self::@class::A::@method::foo
25222522
severity: warning
25232523
''');
25242524
}
25252525

2526+
test_macroDiagnostics_report_contextMessages() async {
2527+
newFile(
2528+
'$testPackageLibPath/diagnostic.dart',
2529+
_getMacroCode('diagnostic.dart'),
2530+
);
2531+
2532+
final library = await buildLibrary(r'''
2533+
import 'diagnostic.dart';
2534+
2535+
@ReportWithContextMessages()
2536+
class A {
2537+
void foo() {}
2538+
void bar() {}
2539+
}
2540+
''');
2541+
2542+
configuration
2543+
..withConstructors = false
2544+
..withMetadata = false;
2545+
checkElementText(library, r'''
2546+
library
2547+
imports
2548+
package:test/diagnostic.dart
2549+
definingUnit
2550+
classes
2551+
class A @62
2552+
macroDiagnostics
2553+
MacroDiagnostic
2554+
message: MacroDiagnosticMessage
2555+
message: Reported message
2556+
target: ElementMacroDiagnosticTarget
2557+
element: self::@class::A
2558+
contextMessages
2559+
MacroDiagnosticMessage
2560+
message: See foo
2561+
target: ElementMacroDiagnosticTarget
2562+
element: self::@class::A::@method::foo
2563+
MacroDiagnosticMessage
2564+
message: See bar
2565+
target: ElementMacroDiagnosticTarget
2566+
element: self::@class::A::@method::bar
2567+
severity: warning
2568+
methods
2569+
foo @73
2570+
returnType: void
2571+
bar @89
2572+
returnType: void
2573+
''');
2574+
}
2575+
25262576
test_macroDiagnostics_report_withoutTarget_error() async {
25272577
newFile(
25282578
'$testPackageLibPath/diagnostic.dart',
@@ -2548,8 +2598,8 @@ library
25482598
class A @61
25492599
macroDiagnostics
25502600
MacroDiagnostic
2551-
message
2552-
Reported message
2601+
message: MacroDiagnosticMessage
2602+
message: Reported message
25532603
target: ApplicationMacroDiagnosticTarget
25542604
annotationIndex: 0
25552605
severity: error
@@ -2581,8 +2631,8 @@ library
25812631
class A @60
25822632
macroDiagnostics
25832633
MacroDiagnostic
2584-
message
2585-
Reported message
2634+
message: MacroDiagnosticMessage
2635+
message: Reported message
25862636
target: ApplicationMacroDiagnosticTarget
25872637
annotationIndex: 0
25882638
severity: info
@@ -2614,8 +2664,8 @@ library
26142664
class A @63
26152665
macroDiagnostics
26162666
MacroDiagnostic
2617-
message
2618-
Reported message
2667+
message: MacroDiagnosticMessage
2668+
message: Reported message
26192669
target: ApplicationMacroDiagnosticTarget
26202670
annotationIndex: 0
26212671
severity: warning
@@ -2647,7 +2697,8 @@ library
26472697
class A @68
26482698
macroDiagnostics
26492699
MacroDiagnostic
2650-
message
2700+
message: MacroDiagnosticMessage
2701+
message:
26512702
Unhandled error: My declarations phase
26522703
Stack trace: <cut>
26532704
target: ApplicationMacroDiagnosticTarget
@@ -2683,7 +2734,8 @@ library
26832734
@76
26842735
macroDiagnostics
26852736
MacroDiagnostic
2686-
message
2737+
message: MacroDiagnosticMessage
2738+
message:
26872739
Unhandled error: My declarations phase
26882740
Stack trace: <cut>
26892741
target: ApplicationMacroDiagnosticTarget
@@ -2723,7 +2775,8 @@ library
27232775
shouldUseTypeForInitializerInference: true
27242776
macroDiagnostics
27252777
MacroDiagnostic
2726-
message
2778+
message: MacroDiagnosticMessage
2779+
message:
27272780
Unhandled error: My declarations phase
27282781
Stack trace: <cut>
27292782
target: ApplicationMacroDiagnosticTarget
@@ -2770,7 +2823,8 @@ library
27702823
returnType: void
27712824
macroDiagnostics
27722825
MacroDiagnostic
2773-
message
2826+
message: MacroDiagnosticMessage
2827+
message:
27742828
Unhandled error: My declarations phase
27752829
Stack trace: <cut>
27762830
target: ApplicationMacroDiagnosticTarget
@@ -2804,7 +2858,8 @@ library
28042858
class A @67
28052859
macroDiagnostics
28062860
MacroDiagnostic
2807-
message
2861+
message: MacroDiagnosticMessage
2862+
message:
28082863
Unhandled error: My definitions phase
28092864
Stack trace: <cut>
28102865
target: ApplicationMacroDiagnosticTarget
@@ -2838,7 +2893,8 @@ library
28382893
class A @61
28392894
macroDiagnostics
28402895
MacroDiagnostic
2841-
message
2896+
message: MacroDiagnosticMessage
2897+
message:
28422898
Unhandled error: My types phase
28432899
Stack trace: <cut>
28442900
target: ApplicationMacroDiagnosticTarget

0 commit comments

Comments
 (0)