@@ -30,7 +30,7 @@ public sealed class RequestDelegateGenerator : IIncrementalGenerator
30
30
public void Initialize ( IncrementalGeneratorInitializationContext context )
31
31
{
32
32
var endpointsWithDiagnostics = context . SyntaxProvider . CreateSyntaxProvider (
33
- predicate : ( node , _ ) => node is InvocationExpressionSyntax
33
+ predicate : static ( node , _ ) => node is InvocationExpressionSyntax
34
34
{
35
35
Expression : MemberAccessExpressionSyntax
36
36
{
@@ -41,62 +41,63 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
41
41
} ,
42
42
ArgumentList : { Arguments : { Count : 2 } args }
43
43
} && _knownMethods . Contains ( method ) ,
44
- transform : ( context , token ) =>
44
+ transform : static ( context , token ) =>
45
45
{
46
- var operation = context . SemanticModel . GetOperation ( context . Node , token ) as IInvocationOperation ;
46
+ var operation = context . SemanticModel . GetOperation ( context . Node , token ) ;
47
47
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 ;
49
55
} )
56
+ . Where ( static endpoint => endpoint != null )
50
57
. WithTrackingName ( GeneratorSteps . EndpointModelStep ) ;
51
58
52
59
context . RegisterSourceOutput ( endpointsWithDiagnostics , ( context , endpoint ) =>
53
60
{
54
- var ( filePath , _) = endpoint . Location ;
55
- foreach ( var diagnostic in endpoint . Diagnostics )
61
+ foreach ( var diagnostic in endpoint ! . Diagnostics )
56
62
{
57
63
context . ReportDiagnostic ( diagnostic ) ;
58
64
}
59
65
} ) ;
60
66
61
67
var endpoints = endpointsWithDiagnostics
62
- . Where ( endpoint => endpoint . Diagnostics . Count == 0 )
68
+ . Where ( endpoint => endpoint ! . Diagnostics . Count == 0 )
63
69
. WithTrackingName ( GeneratorSteps . EndpointsWithoutDiagnosicsStep ) ;
64
70
65
71
var thunks = endpoints . Select ( ( endpoint , _ ) =>
66
72
{
67
73
using var stringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
68
74
using var codeWriter = new CodeWriter ( stringWriter , baseIndent : 3 ) ;
69
75
codeWriter . InitializeIndent ( ) ;
70
- codeWriter . WriteLine ( $ "[{ endpoint . EmitSourceKey ( ) } ] = (") ;
76
+ codeWriter . WriteLine ( $ "[{ endpoint ! . EmitSourceKey ( ) } ] = (") ;
71
77
codeWriter . Indent ++ ;
72
78
codeWriter . WriteLine ( "(methodInfo, options) =>" ) ;
73
79
codeWriter . StartBlock ( ) ;
74
80
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 ( ) } );") ;
76
82
codeWriter . WriteLine ( "return new RequestDelegateMetadataResult { EndpointMetadata = options.EndpointBuilder.Metadata.AsReadOnly() };" ) ;
77
83
codeWriter . EndBlockWithComma ( ) ;
78
84
codeWriter . WriteLine ( "(del, options, inferredMetadataResult) =>" ) ;
79
85
codeWriter . StartBlock ( ) ;
80
- codeWriter . WriteLine ( $ "var handler = ({ endpoint . EmitHandlerDelegateCast ( ) } )del;") ;
86
+ codeWriter . WriteLine ( $ "var handler = ({ endpoint ! . EmitHandlerDelegateCast ( ) } )del;") ;
81
87
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 ) ;
85
91
if ( endpoint . NeedsParameterArray )
86
92
{
87
93
codeWriter . WriteLine ( "var parameters = del.Method.GetParameters();" ) ;
88
94
}
89
95
codeWriter . WriteLineNoTabs ( string . Empty ) ;
90
96
codeWriter . WriteLine ( "if (options?.EndpointBuilder?.FilterFactories.Count > 0)" ) ;
91
97
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 =>" ) ;
100
101
codeWriter . StartBlock ( ) ;
101
102
codeWriter . WriteLine ( "if (ic.HttpContext.Response.StatusCode == 400)" ) ;
102
103
codeWriter . StartBlock ( ) ;
@@ -124,16 +125,16 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
124
125
. Collect ( )
125
126
. Select ( ( endpoints , _ ) =>
126
127
{
127
- var dedupedByDelegate = endpoints . Distinct ( EndpointDelegateComparer . Instance ) ;
128
+ var dedupedByDelegate = endpoints . Distinct < Endpoint > ( EndpointDelegateComparer . Instance ) ;
128
129
using var stringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
129
130
using var codeWriter = new CodeWriter ( stringWriter , baseIndent : 2 ) ;
130
131
foreach ( var endpoint in dedupedByDelegate )
131
132
{
132
- codeWriter . WriteLine ( $ "internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder { endpoint . HttpMethod } (") ;
133
+ codeWriter . WriteLine ( $ "internal static global::Microsoft.AspNetCore.Builder.RouteHandlerBuilder { endpoint ! . HttpMethod } (") ;
133
134
codeWriter . Indent ++ ;
134
135
codeWriter . WriteLine ( "this global::Microsoft.AspNetCore.Routing.IEndpointRouteBuilder endpoints," ) ;
135
136
codeWriter . WriteLine ( @"[global::System.Diagnostics.CodeAnalysis.StringSyntax(""Route"")] string pattern," ) ;
136
- codeWriter . WriteLine ( $ "global::{ endpoint . EmitHandlerDelegateType ( ) } handler,") ;
137
+ codeWriter . WriteLine ( $ "global::{ endpoint ! . EmitHandlerDelegateType ( ) } handler,") ;
137
138
codeWriter . WriteLine ( @"[global::System.Runtime.CompilerServices.CallerFilePath] string filePath = """"," ) ;
138
139
codeWriter . WriteLine ( "[global::System.Runtime.CompilerServices.CallerLineNumber]int lineNumber = 0)" ) ;
139
140
codeWriter . Indent -- ;
@@ -143,7 +144,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
143
144
codeWriter . WriteLine ( "endpoints," ) ;
144
145
codeWriter . WriteLine ( "pattern," ) ;
145
146
codeWriter . WriteLine ( "handler," ) ;
146
- codeWriter . WriteLine ( $ "{ endpoint . EmitVerb ( ) } ,") ;
147
+ codeWriter . WriteLine ( $ "{ endpoint ! . EmitVerb ( ) } ,") ;
147
148
codeWriter . WriteLine ( "filePath," ) ;
148
149
codeWriter . WriteLine ( "lineNumber);" ) ;
149
150
codeWriter . Indent -- ;
@@ -157,12 +158,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
157
158
. Collect ( )
158
159
. Select ( ( endpoints , _ ) =>
159
160
{
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 ) ;
166
167
167
168
using var stringWriter = new StringWriter ( CultureInfo . InvariantCulture ) ;
168
169
using var codeWriter = new CodeWriter ( stringWriter , baseIndent : 0 ) ;
0 commit comments