@@ -36,7 +36,17 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
3636 }
3737
3838 return false ;
39- } , ( context , ct ) => new RunContext ( ( InvocationExpressionSyntax ) context . Node , context . SemanticModel ) )
39+ } , ( context , ct ) =>
40+ {
41+ var reporter = new DiagnosticReporter ( ) ;
42+ var node = ( InvocationExpressionSyntax ) context . Node ;
43+ var wellknownTypes = new WellKnownTypes ( context . SemanticModel . Compilation ) ;
44+ var parser = new Parser ( reporter , node , context . SemanticModel , wellknownTypes , DelegateBuildType . MakeDelegateWhenHasDefaultValue , [ ] ) ;
45+ var isRunAsync = ( node . Expression as MemberAccessExpressionSyntax ) ? . Name . Identifier . Text == "RunAsync" ;
46+
47+ var command = parser . ParseAndValidateForRun ( ) ;
48+ return new CommandContext ( command , isRunAsync , reporter , node ) ;
49+ } )
4050 . WithTrackingName ( "ConsoleApp.Run.0_CreateSyntaxProvider" ) ; // annotate for IncrementalGeneratorTest
4151
4252 context . RegisterSourceOutput ( runSource , EmitConsoleAppRun ) ;
@@ -561,36 +571,29 @@ namespace ConsoleAppFramework;
561571
562572""" ;
563573
564- static void EmitConsoleAppRun ( SourceProductionContext sourceProductionContext , RunContext runNode )
574+ static void EmitConsoleAppRun ( SourceProductionContext sourceProductionContext , CommandContext commandContext )
565575 {
566- var node = runNode . Node ;
567- var model = runNode . SemanticModel ;
568-
569- var wellKnownTypes = new WellKnownTypes ( model . Compilation ) ;
570-
571- var reporter = new DiagnosticReporter ( ) ;
572- var parser = new Parser ( reporter , node , model , wellKnownTypes , DelegateBuildType . MakeDelegateWhenHasDefaultValue , [ ] ) ;
573- var command = parser . ParseAndValidateForRun ( ) ;
574- if ( command == null )
576+ if ( commandContext . DiagnosticReporter . HasDiagnostics )
575577 {
576- reporter . ReportToContext ( sourceProductionContext ) ;
578+ commandContext . DiagnosticReporter . ReportToContext ( sourceProductionContext ) ;
577579 return ;
578580 }
581+ var command = commandContext . Command ;
582+ if ( command == null ) return ;
583+
579584 if ( command . HasFilter )
580585 {
581- sourceProductionContext . ReportDiagnostic ( DiagnosticDescriptors . CommandHasFilter , node . GetLocation ( ) ) ;
586+ sourceProductionContext . ReportDiagnostic ( DiagnosticDescriptors . CommandHasFilter , commandContext . Node . GetLocation ( ) ) ;
582587 return ;
583588 }
584589
585- var isRunAsync = ( ( node . Expression as MemberAccessExpressionSyntax ) ? . Name . Identifier . Text == "RunAsync" ) ;
586-
587590 var sb = new SourceBuilder ( 0 ) ;
588591 sb . AppendLine ( GeneratedCodeHeader ) ;
589592 using ( sb . BeginBlock ( "internal static partial class ConsoleApp" ) )
590593 {
591594 var emitter = new Emitter ( ) ;
592595 var withId = new Emitter . CommandWithId ( null , command , - 1 ) ;
593- emitter . EmitRun ( sb , withId , isRunAsync ) ;
596+ emitter . EmitRun ( sb , withId , command . IsAsync ) ;
594597 }
595598 sourceProductionContext . AddSource ( "ConsoleApp.Run.g.cs" , sb . ToString ( ) ) ;
596599
@@ -756,19 +759,20 @@ static void EmitConsoleAppBuilder(SourceProductionContext sourceProductionContex
756759 sourceProductionContext . AddSource ( "ConsoleApp.Builder.Help.g.cs" , help . ToString ( ) ) ;
757760 }
758761
759- class CommandContext ( Command command , bool isAsync , DiagnosticReporter diagnosticReporter ) : IEquatable < CommandContext >
762+ class CommandContext ( Command ? command , bool isAsync , DiagnosticReporter diagnosticReporter , InvocationExpressionSyntax node ) : IEquatable < CommandContext >
760763 {
761- public Command Command => command ;
764+ public Command ? Command => command ;
762765 public DiagnosticReporter DiagnosticReporter => diagnosticReporter ;
766+ public InvocationExpressionSyntax Node => node ;
763767 public bool IsAsync => isAsync ;
764768
765769 public bool Equals ( CommandContext other )
766770 {
767771 // has diagnostics, always go to modified(don't cache)
768772 if ( diagnosticReporter . HasDiagnostics || other . DiagnosticReporter . HasDiagnostics ) return false ;
773+ if ( command == null || other . Command == null ) return false ; // maybe has diagnostics
769774
770775 if ( isAsync != other . IsAsync ) return false ;
771-
772776 return command . Equals ( other . Command ) ;
773777 }
774778 }
0 commit comments