Skip to content

Commit f1f3f9c

Browse files
committed
Don't propagate symbols in ObservablePropertyGenerator
1 parent 73ebfb5 commit f1f3f9c

File tree

1 file changed

+16
-21
lines changed

1 file changed

+16
-21
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,33 @@ public sealed partial class ObservablePropertyGenerator : IIncrementalGenerator
2424
/// <inheritdoc/>
2525
public void Initialize(IncrementalGeneratorInitializationContext context)
2626
{
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 =
2929
context.SyntaxProvider
3030
.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) =>
3333
{
3434
if (!context.SemanticModel.Compilation.HasLanguageVersionAtLeastEqualTo(LanguageVersion.CSharp8))
3535
{
3636
return default;
3737
}
3838

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)!;
4340

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+
}
4846

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);
5650

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);
5954

6055
// Output the diagnostics
6156
context.ReportDiagnostics(propertyInfoWithErrors.Select(static (item, _) => item.Info.Errors));

0 commit comments

Comments
 (0)