@@ -24,38 +24,33 @@ public sealed partial class ObservablePropertyGenerator : IIncrementalGenerator
24
24
/// <inheritdoc/>
25
25
public void Initialize ( IncrementalGeneratorInitializationContext context )
26
26
{
27
- // Get all field declarations with at least one attribute
28
- IncrementalValuesProvider < IFieldSymbol > fieldSymbols =
27
+ // Gather info for all annotated fields
28
+ IncrementalValuesProvider < ( HierarchyInfo Hierarchy , Result < PropertyInfo ? > Info ) > propertyInfoWithErrors =
29
29
context . SyntaxProvider
30
30
. CreateSyntaxProvider (
31
- static ( node , _ ) => node is FieldDeclarationSyntax { Parent : ClassDeclarationSyntax or RecordDeclarationSyntax , AttributeLists . Count : > 0 } ,
32
- static ( context , _ ) =>
31
+ static ( node , _ ) => node is VariableDeclaratorSyntax { Parent : VariableDeclarationSyntax { Parent : FieldDeclarationSyntax { Parent : ClassDeclarationSyntax or RecordDeclarationSyntax , AttributeLists . Count : > 0 } } } ,
32
+ static ( context , token ) =>
33
33
{
34
34
if ( ! context . SemanticModel . Compilation . HasLanguageVersionAtLeastEqualTo ( LanguageVersion . CSharp8 ) )
35
35
{
36
36
return default ;
37
37
}
38
38
39
- return ( ( FieldDeclarationSyntax ) context . Node ) . Declaration . Variables . Select ( v => ( IFieldSymbol ) context . SemanticModel . GetDeclaredSymbol ( v ) ! ) ;
40
- } )
41
- . Where ( static items => items is not null )
42
- . SelectMany ( static ( item , _ ) => item ! ) ! ;
39
+ IFieldSymbol fieldSymbol = ( IFieldSymbol ) context . SemanticModel . GetDeclaredSymbol ( context . Node , token ) ! ;
43
40
44
- // Filter the fields using [ObservableProperty]
45
- IncrementalValuesProvider < IFieldSymbol > fieldSymbolsWithAttribute =
46
- fieldSymbols
47
- . Where ( static item => item . HasAttributeWithFullyQualifiedName ( "global::CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute" ) ) ;
41
+ // Filter the fields using [ObservableProperty]
42
+ if ( ! fieldSymbol . HasAttributeWithFullyQualifiedName ( "global::CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute" ) )
43
+ {
44
+ return default ;
45
+ }
48
46
49
- // Gather info for all annotated fields
50
- IncrementalValuesProvider < ( HierarchyInfo Hierarchy , Result < PropertyInfo ? > Info ) > propertyInfoWithErrors =
51
- fieldSymbolsWithAttribute
52
- . Select ( static ( item , _ ) =>
53
- {
54
- HierarchyInfo hierarchy = HierarchyInfo . From ( item . ContainingType ) ;
55
- PropertyInfo ? propertyInfo = Execute . TryGetInfo ( item , out ImmutableArray < Diagnostic > diagnostics ) ;
47
+ // Produce the incremental models
48
+ HierarchyInfo hierarchy = HierarchyInfo . From ( fieldSymbol . ContainingType ) ;
49
+ PropertyInfo ? propertyInfo = Execute . TryGetInfo ( fieldSymbol , out ImmutableArray < Diagnostic > diagnostics ) ;
56
50
57
- return ( hierarchy , new Result < PropertyInfo ? > ( propertyInfo , diagnostics ) ) ;
58
- } ) ;
51
+ return ( Hierarchy : hierarchy , new Result < PropertyInfo ? > ( propertyInfo , diagnostics ) ) ;
52
+ } )
53
+ . Where ( static item => item . Hierarchy is not null ) ;
59
54
60
55
// Output the diagnostics
61
56
context . ReportDiagnostics ( propertyInfoWithErrors . Select ( static ( item , _ ) => item . Info . Errors ) ) ;
0 commit comments