Skip to content

Commit 17aa984

Browse files
committed
ready reporter
1 parent 4e4c9c2 commit 17aa984

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

src/ConsoleAppFramework/ConsoleAppGenerator.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,10 +568,12 @@ static void EmitConsoleAppRun(SourceProductionContext sourceProductionContext, R
568568

569569
var wellKnownTypes = new WellKnownTypes(model.Compilation);
570570

571-
var parser = new Parser(sourceProductionContext, node, model, wellKnownTypes, DelegateBuildType.MakeDelegateWhenHasDefaultValue, []);
571+
var reporter = new DiagnosticReporter();
572+
var parser = new Parser(reporter, node, model, wellKnownTypes, DelegateBuildType.MakeDelegateWhenHasDefaultValue, []);
572573
var command = parser.ParseAndValidateForRun();
573574
if (command == null)
574575
{
576+
reporter.ReportToContext(sourceProductionContext);
575577
return;
576578
}
577579
if (command.HasFilter)
@@ -657,12 +659,13 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
657659
return;
658660
}
659661

662+
var reporter = new DiagnosticReporter();
660663
var names = new HashSet<string>();
661664
var commands1 = methodGroup["Add"]
662665
.Select(x =>
663666
{
664667
var wellKnownTypes = new WellKnownTypes(x.Model.Compilation);
665-
var parser = new Parser(sourceProductionContext, x.Node, x.Model, wellKnownTypes, DelegateBuildType.OnlyActionFunc, globalFilters);
668+
var parser = new Parser(reporter, x.Node, x.Model, wellKnownTypes, DelegateBuildType.OnlyActionFunc, globalFilters);
666669
var command = parser.ParseAndValidateForBuilderDelegateRegistration();
667670

668671
// validation command name duplicate
@@ -681,7 +684,7 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
681684
.SelectMany(x =>
682685
{
683686
var wellKnownTypes = new WellKnownTypes(x.Model.Compilation);
684-
var parser = new Parser(sourceProductionContext, x.Node, x.Model, wellKnownTypes, DelegateBuildType.None, globalFilters);
687+
var parser = new Parser(reporter, x.Node, x.Model, wellKnownTypes, DelegateBuildType.None, globalFilters);
685688
var commands = parser.ParseAndValidateForBuilderClassRegistration();
686689

687690
// validation command name duplicate
@@ -699,6 +702,12 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
699702

700703
var commands = commands1.Concat(commands2).ToArray();
701704

705+
if (reporter.HasDiagnostics)
706+
{
707+
reporter.ReportToContext(sourceProductionContext);
708+
return;
709+
}
710+
702711
// don't emit if exists failure(already reported error)
703712
if (commands.Any(x => x == null))
704713
{

src/ConsoleAppFramework/DiagnosticDescriptors.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,40 @@
22

33
namespace ConsoleAppFramework;
44

5+
internal sealed class DiagnosticReporter
6+
{
7+
List<Diagnostic>? diagnostics;
8+
9+
public bool HasDiagnostics => diagnostics != null && diagnostics.Count != 0;
10+
11+
public void ReportDiagnostic(DiagnosticDescriptor diagnosticDescriptor, Location location, params object?[]? messageArgs)
12+
{
13+
var diagnostic = Diagnostic.Create(diagnosticDescriptor, location, messageArgs);
14+
if (diagnostics == null)
15+
{
16+
diagnostics = new();
17+
}
18+
diagnostics.Add(diagnostic);
19+
}
20+
21+
public void ReportToContext(SourceProductionContext context)
22+
{
23+
if (diagnostics != null)
24+
{
25+
foreach (var item in diagnostics)
26+
{
27+
context.ReportDiagnostic(item);
28+
}
29+
}
30+
}
31+
}
32+
533
internal static class DiagnosticDescriptors
634
{
735
const string Category = "GenerateConsoleAppFramework";
836

937
public static void ReportDiagnostic(this SourceProductionContext context, DiagnosticDescriptor diagnosticDescriptor, Location location, params object?[]? messageArgs)
1038
{
11-
// must use location.Clone(), incremental cached code + diagnostic craches visual studio however use Clone() can avoid it.
12-
location = location.Clone();
1339
var diagnostic = Diagnostic.Create(diagnosticDescriptor, location, messageArgs);
1440
context.ReportDiagnostic(diagnostic);
1541
}

src/ConsoleAppFramework/Parser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace ConsoleAppFramework;
66

7-
internal class Parser(SourceProductionContext context, InvocationExpressionSyntax node, SemanticModel model, WellKnownTypes wellKnownTypes, DelegateBuildType delegateBuildType, FilterInfo[] globalFilters)
7+
internal class Parser(DiagnosticReporter context, InvocationExpressionSyntax node, SemanticModel model, WellKnownTypes wellKnownTypes, DelegateBuildType delegateBuildType, FilterInfo[] globalFilters)
88
{
99
public Command? ParseAndValidateForRun() // for ConsoleApp.Run, lambda or method or &method
1010
{

0 commit comments

Comments
 (0)