@@ -22,37 +22,46 @@ public sealed partial class IMessengerRegisterAllGenerator : IIncrementalGenerat
22
22
/// <inheritdoc/>
23
23
public void Initialize ( IncrementalGeneratorInitializationContext context )
24
24
{
25
- // Get all class declarations. This pipeline step also needs to filter out duplicate recipient
26
- // definitions (it might happen if a recipient has partial declarations). To do this, all pairs
27
- // of class declarations and associated symbols are gathered, and then only the pair where the
28
- // class declaration is the first syntax reference for the associated symbol is kept.
29
- // Just like with the ObservableValidator generator, we also intentionally skip abstract types.
30
- IncrementalValuesProvider < INamedTypeSymbol > typeSymbols =
25
+ // Get the recipient info for all target types
26
+ IncrementalValuesProvider < RecipientInfo > recipientInfo =
31
27
context . SyntaxProvider
32
28
. CreateSyntaxProvider (
33
29
static ( node , _ ) => node is ClassDeclarationSyntax ,
34
- static ( context , _ ) =>
30
+ static ( context , token ) =>
35
31
{
36
32
if ( ! context . SemanticModel . Compilation . HasLanguageVersionAtLeastEqualTo ( LanguageVersion . CSharp8 ) )
37
33
{
38
34
return default ;
39
35
}
40
36
41
- return ( context . Node , Symbol : ( INamedTypeSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node ) ! ) ;
42
- } )
43
- . Where ( static item => item . Symbol is { IsAbstract : false , IsGenericType : false } && item . Node . IsFirstSyntaxDeclarationForSymbol ( item . Symbol ) )
44
- . Select ( static ( item , _ ) => item . Symbol ) ;
37
+ INamedTypeSymbol typeSymbol = ( INamedTypeSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node , token ) ! ;
45
38
46
- // Get the target IRecipient<TMessage> interfaces and filter out other types
47
- IncrementalValuesProvider < ( INamedTypeSymbol Type , ImmutableArray < INamedTypeSymbol > Interfaces ) > typeAndInterfaceSymbols =
48
- typeSymbols
49
- . Select ( static ( item , _ ) => ( item , Interfaces : Execute . GetInterfaces ( item ) ) )
50
- . Where ( static item => ! item . Interfaces . IsEmpty ) ;
39
+ // The type must be a non-abstract, non-generic type (just like with the ObservableValidator generator)
40
+ if ( typeSymbol is not { IsAbstract : false , IsGenericType : false } )
41
+ {
42
+ return default ;
43
+ }
51
44
52
- // Get the recipient info for all target types
53
- IncrementalValuesProvider < RecipientInfo > recipientInfo =
54
- typeAndInterfaceSymbols
55
- . Select ( static ( item , _ ) => Execute . GetInfo ( item . Type , item . Interfaces ) )
45
+ // This pipeline step also needs to filter out duplicate recipient definitions (it might happen if a
46
+ // recipient has partial declarations). To do this, all pairs of class declarations and associated
47
+ // symbols are gathered, and then only the pair where the class declaration is the first syntax
48
+ // reference for the associated symbol is kept.
49
+ if ( ! context . Node . IsFirstSyntaxDeclarationForSymbol ( typeSymbol ) )
50
+ {
51
+ return default ;
52
+ }
53
+
54
+ ImmutableArray < INamedTypeSymbol > interfaceSymbols = Execute . GetInterfaces ( typeSymbol ) ;
55
+
56
+ // Check that the type implements at least one IRecipient<TMessage> interface
57
+ if ( interfaceSymbols . IsEmpty )
58
+ {
59
+ return default ;
60
+ }
61
+
62
+ return Execute . GetInfo ( typeSymbol , interfaceSymbols ) ;
63
+ } )
64
+ . Where ( static item => item is not null ) !
56
65
. WithComparer ( RecipientInfo . Comparer . Default ) ;
57
66
58
67
// Check whether the header file is needed
@@ -68,7 +77,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
68
77
69
78
// Gather the conditional flag and attribute availability
70
79
IncrementalValueProvider < ( bool IsHeaderFileNeeded , bool IsDynamicallyAccessedMembersAttributeAvailable ) > headerFileInfo =
71
- isHeaderFileNeeded . Combine ( isDynamicallyAccessedMembersAttributeAvailable ) ;
80
+ isHeaderFileNeeded
81
+ . Combine ( isDynamicallyAccessedMembersAttributeAvailable ) ;
72
82
73
83
// Generate the header file with the attributes
74
84
context . RegisterConditionalImplementationSourceOutput ( headerFileInfo , static ( context , item ) =>
0 commit comments