@@ -30,7 +30,7 @@ public sealed class RequestDelegateGenerator : IIncrementalGenerator
3030 public void Initialize ( IncrementalGeneratorInitializationContext context )
3131 {
3232 var endpointsWithDiagnostics = context . SyntaxProvider . CreateSyntaxProvider (
33- predicate : ( node , _ ) => node is InvocationExpressionSyntax
33+ predicate : static ( node , _ ) => node is InvocationExpressionSyntax
3434 {
3535 Expression : MemberAccessExpressionSyntax
3636 {
@@ -41,62 +41,63 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
4141 } ,
4242 ArgumentList : { Arguments : { Count : 2 } args }
4343 } && _knownMethods . Contains ( method ) ,
44- transform : ( context , token ) =>
44+ transform : static ( context , token ) =>
4545 {
46- var operation = context . SemanticModel . GetOperation ( context . Node , token ) as IInvocationOperation ;
46+ var operation = context . SemanticModel . GetOperation ( context . Node , token ) ;
4747 var wellKnownTypes = WellKnownTypes . GetOrCreate ( context . SemanticModel . Compilation ) ;
48- return new Endpoint ( operation , wellKnownTypes ) ;
48+ if ( operation is IInvocationOperation { Arguments : { Length : 3 } parameters } invocationOperation &&
49+ invocationOperation . GetRouteHandlerArgument ( ) is { Parameter . Type : { } delegateType } &&
50+ SymbolEqualityComparer . Default . Equals ( delegateType , wellKnownTypes . Get ( WellKnownTypeData . WellKnownType . System_Delegate ) ) )
51+ {
52+ return new Endpoint ( invocationOperation , wellKnownTypes , context . SemanticModel ) ;
53+ }
54+ return null ;
4955 } )
56+ . Where ( static endpoint => endpoint != null )
5057 . WithTrackingName ( GeneratorSteps . EndpointModelStep ) ;
5158
5259 context . RegisterSourceOutput ( endpointsWithDiagnostics , ( context , endpoint ) =>
5360 {
54- var ( filePath , _) = endpoint . Location ;
55- foreach ( var diagnostic in endpoint . Diagnostics )
61+ foreach ( var diagnostic in endpoint ! . Diagnostics )
5662 {
5763 context . ReportDiagnostic ( diagnostic ) ;
5864 }
5965 } ) ;
6066
6167 var endpoints = endpointsWithDiagnostics
62- . Where ( endpoint => endpoint . Diagnostics . Count == 0 )
68+ . Where ( endpoint => endpoint ! . Diagnostics . Count == 0 )
6369 . WithTrackingName ( GeneratorSteps . EndpointsWithoutDiagnosicsStep ) ;
6470
6571 var thunks = endpoints . Select ( ( endpoint , _ ) =>
6672 {
6773 using var stringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
6874 using var codeWriter = new CodeWriter ( stringWriter , baseIndent : 3 ) ;
6975 codeWriter . InitializeIndent ( ) ;
70- codeWriter . WriteLine ( $ "[{ endpoint . EmitSourceKey ( ) } ] = (") ;
76+ codeWriter . WriteLine ( $ "[{ endpoint ! . EmitSourceKey ( ) } ] = (") ;
7177 codeWriter . Indent ++ ;
7278 codeWriter . WriteLine ( "(methodInfo, options) =>" ) ;
7379 codeWriter . StartBlock ( ) ;
7480 codeWriter . WriteLine ( @"Debug.Assert(options?.EndpointBuilder != null, ""EndpointBuilder not found."");" ) ;
75- codeWriter . WriteLine ( $ "options.EndpointBuilder.Metadata.Add(new SourceKey{ endpoint . EmitSourceKey ( ) } );") ;
81+ codeWriter . WriteLine ( $ "options.EndpointBuilder.Metadata.Add(new SourceKey{ endpoint ! . EmitSourceKey ( ) } );") ;
7682 codeWriter . WriteLine ( "return new RequestDelegateMetadataResult { EndpointMetadata = options.EndpointBuilder.Metadata.AsReadOnly() };" ) ;
7783 codeWriter . EndBlockWithComma ( ) ;
7884 codeWriter . WriteLine ( "(del, options, inferredMetadataResult) =>" ) ;
7985 codeWriter . StartBlock ( ) ;
80- codeWriter . WriteLine ( $ "var handler = ({ endpoint . EmitHandlerDelegateCast ( ) } )del;") ;
86+ codeWriter . WriteLine ( $ "var handler = ({ endpoint ! . EmitHandlerDelegateCast ( ) } )del;") ;
8187 codeWriter . WriteLine ( "EndpointFilterDelegate? filteredInvocation = null;" ) ;
82- endpoint . EmitRouteOrQueryResolver ( codeWriter ) ;
83- endpoint . EmitJsonBodyOrServicePreparation ( codeWriter ) ;
84- endpoint . EmitJsonPreparation ( codeWriter ) ;
88+ endpoint ! . EmitRouteOrQueryResolver ( codeWriter ) ;
89+ endpoint ! . EmitJsonBodyOrServicePreparation ( codeWriter ) ;
90+ endpoint ! . Response ? . EmitJsonPreparation ( codeWriter ) ;
8591 if ( endpoint . NeedsParameterArray )
8692 {
8793 codeWriter . WriteLine ( "var parameters = del.Method.GetParameters();" ) ;
8894 }
8995 codeWriter . WriteLineNoTabs ( string . Empty ) ;
9096 codeWriter . WriteLine ( "if (options?.EndpointBuilder?.FilterFactories.Count > 0)" ) ;
9197 codeWriter . StartBlock ( ) ;
92- if ( endpoint . Response . IsAwaitable )
93- {
94- codeWriter . WriteLine ( "filteredInvocation = GeneratedRouteBuilderExtensionsCore.BuildFilterDelegate(async ic =>" ) ;
95- }
96- else
97- {
98- codeWriter . WriteLine ( "filteredInvocation = GeneratedRouteBuilderExtensionsCore.BuildFilterDelegate(ic =>" ) ;
99- }
98+ codeWriter . WriteLine ( endpoint ! . Response ? . IsAwaitable == true
99+ ? "filteredInvocation = GeneratedRouteBuilderExtensionsCore.BuildFilterDelegate(async ic =>"
100+ : "filteredInvocation = GeneratedRouteBuilderExtensionsCore.BuildFilterDelegate(ic =>" ) ;
100101 codeWriter . StartBlock ( ) ;
101102 codeWriter . WriteLine ( "if (ic.HttpContext.Response.StatusCode == 400)" ) ;
102103 codeWriter . StartBlock ( ) ;
@@ -124,16 +125,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
124125 . Collect ( )
125126 . Select ( ( endpoints , _ ) =>
126127 {
127- var dedupedByDelegate = endpoints . Distinct ( EndpointDelegateComparer . Instance ) ;
128+ var dedupedByDelegate = endpoints . Distinct < Endpoint > ( EndpointDelegateComparer . Instance ) ;
128129 using var stringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
129130 using var codeWriter = new CodeWriter ( stringWriter , baseIndent : 2 ) ;
130131 foreach ( var endpoint in dedupedByDelegate )
131132 {
132- codeWriter . WriteLine ( $ "internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder { endpoint . HttpMethod } (") ;
133+ codeWriter . WriteLine ( $ "internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder { endpoint ! . HttpMethod } (") ;
133134 codeWriter . Indent ++ ;
134135 codeWriter . WriteLine ( "this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints," ) ;
135136 codeWriter . WriteLine ( @"[global::System.Diagnostics.CodeAnalysis.StringSyntax(""Route"")] string pattern," ) ;
136- codeWriter . WriteLine ( $ "global::{ endpoint . EmitHandlerDelegateType ( ) } handler,") ;
137+ codeWriter . WriteLine ( $ "global::{ endpoint ! . EmitHandlerDelegateType ( ) } handler,") ;
137138 codeWriter . WriteLine ( @"[global::System.Runtime.CompilerServices.CallerFilePath] string filePath = """"," ) ;
138139 codeWriter . WriteLine ( "[global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)" ) ;
139140 codeWriter . Indent -- ;
@@ -143,7 +144,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
143144 codeWriter . WriteLine ( "endpoints," ) ;
144145 codeWriter . WriteLine ( "pattern," ) ;
145146 codeWriter . WriteLine ( "handler," ) ;
146- codeWriter . WriteLine ( $ "{ endpoint . EmitVerb ( ) } ,") ;
147+ codeWriter . WriteLine ( $ "{ endpoint ! . EmitVerb ( ) } ,") ;
147148 codeWriter . WriteLine ( "filePath," ) ;
148149 codeWriter . WriteLine ( "lineNumber);" ) ;
149150 codeWriter . Indent -- ;
@@ -157,12 +158,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
157158 . Collect ( )
158159 . Select ( ( endpoints , _ ) =>
159160 {
160- var hasJsonBodyOrService = endpoints . Any ( endpoint => endpoint . EmitterContext . HasJsonBodyOrService ) ;
161- var hasJsonBody = endpoints . Any ( endpoint => endpoint . EmitterContext . HasJsonBody ) ;
162- var hasRouteOrQuery = endpoints . Any ( endpoint => endpoint . EmitterContext . HasRouteOrQuery ) ;
163- var hasBindAsync = endpoints . Any ( endpoint => endpoint . EmitterContext . HasBindAsync ) ;
164- var hasParsable = endpoints . Any ( endpoint => endpoint . EmitterContext . HasParsable ) ;
165- var hasJsonResponse = endpoints . Any ( endpoint => endpoint . EmitterContext . HasJsonResponse ) ;
161+ var hasJsonBodyOrService = endpoints . Any ( endpoint => endpoint ! . EmitterContext . HasJsonBodyOrService ) ;
162+ var hasJsonBody = endpoints . Any ( endpoint => endpoint ! . EmitterContext . HasJsonBody ) ;
163+ var hasRouteOrQuery = endpoints . Any ( endpoint => endpoint ! . EmitterContext . HasRouteOrQuery ) ;
164+ var hasBindAsync = endpoints . Any ( endpoint => endpoint ! . EmitterContext . HasBindAsync ) ;
165+ var hasParsable = endpoints . Any ( endpoint => endpoint ! . EmitterContext . HasParsable ) ;
166+ var hasJsonResponse = endpoints . Any ( endpoint => endpoint ! . EmitterContext . HasJsonResponse ) ;
166167
167168 using var stringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
168169 using var codeWriter = new CodeWriter ( stringWriter , baseIndent : 0 ) ;
0 commit comments