@@ -21,31 +21,43 @@ public sealed partial class ObservableValidatorValidateAllPropertiesGenerator :
21
21
/// <inheritdoc/>
22
22
public void Initialize ( IncrementalGeneratorInitializationContext context )
23
23
{
24
- // Get all class declarations. We intentionally skip generating code for abstract types, as that would never be used.
25
- // The methods that are generated by this generator are retrieved through reflection using the type of the invoking
26
- // instance as discriminator, which means a type that is abstract could never be used (since it couldn't be instantiated).
27
- IncrementalValuesProvider < INamedTypeSymbol > typeSymbols =
24
+ // Get the types that inherit from ObservableValidator and gather their info
25
+ IncrementalValuesProvider < ValidationInfo > validationInfo =
28
26
context . SyntaxProvider
29
27
. CreateSyntaxProvider (
30
28
static ( node , _ ) => node is ClassDeclarationSyntax ,
31
- static ( context , _ ) =>
29
+ static ( context , token ) =>
32
30
{
33
31
if ( ! context . SemanticModel . Compilation . HasLanguageVersionAtLeastEqualTo ( LanguageVersion . CSharp8 ) )
34
32
{
35
33
return default ;
36
34
}
37
35
38
- return ( context . Node , Symbol : ( INamedTypeSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node ) ! ) ;
39
- } )
40
- . Where ( static item => item . Symbol is { IsAbstract : false , IsGenericType : false } && item . Node . IsFirstSyntaxDeclarationForSymbol ( item . Symbol ) )
41
- . Select ( static ( item , _ ) => item . Symbol ) ;
36
+ INamedTypeSymbol typeSymbol = ( INamedTypeSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node , token ) ! ;
42
37
43
- // Get the types that inherit from ObservableValidator and gather their info
44
- IncrementalValuesProvider < ValidationInfo > validationInfo =
45
- typeSymbols
46
- . Where ( Execute . IsObservableValidator )
47
- . Select ( static ( item , _ ) => Execute . GetInfo ( item ) )
48
- . WithComparer ( ValidationInfo . Comparer . Default ) ;
38
+ // Skip generating code for abstract types, as that would never be used. The methods that are generated by
39
+ // this generator are retrieved through reflection using the type of the invoking instance as discriminator,
40
+ // which means a type that is abstract could never be used (since it couldn't be instantiated).
41
+ if ( typeSymbol is not { IsAbstract : false , IsGenericType : false } )
42
+ {
43
+ return default ;
44
+ }
45
+
46
+ // Just like in IMessengerRegisterAllGenerator, only select the first declaration for this type symbol
47
+ if ( ! context . Node . IsFirstSyntaxDeclarationForSymbol ( typeSymbol ) )
48
+ {
49
+ return default ;
50
+ }
51
+
52
+ // Only select types inheriting from ObservableValidator
53
+ if ( ! Execute . IsObservableValidator ( typeSymbol ) )
54
+ {
55
+ return default ;
56
+ }
57
+
58
+ return Execute . GetInfo ( typeSymbol ) ;
59
+ } )
60
+ . Where ( static item => item is not null ) ! ;
49
61
50
62
// Check whether the header file is needed
51
63
IncrementalValueProvider < bool > isHeaderFileNeeded =
0 commit comments