Skip to content

Commit 1690599

Browse files
committed
Gather all symbols in RegisterCompilationStartAction in new analyzers
1 parent c70e973 commit 1690599

File tree

2 files changed

+44
-38
lines changed

2 files changed

+44
-38
lines changed

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/InvalidClassLevelNotifyDataErrorInfoAttributeAnalyzer.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,33 @@ public override void Initialize(AnalysisContext context)
2626
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
2727
context.EnableConcurrentExecution();
2828

29-
context.RegisterSymbolAction(static context =>
29+
context.RegisterCompilationStartAction(static context =>
3030
{
31-
// We're looking for all class declarations
32-
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class, IsImplicitlyDeclared: false } classSymbol)
33-
{
34-
return;
35-
}
36-
37-
// Only inspect classes that are using [NotifyDataErrorInfo]
31+
// Get the symbols for [NotifyDataErrorInfo] and ObservableValidator
3832
if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.NotifyDataErrorInfoAttribute") is not INamedTypeSymbol notifyDataErrorInfoAttributeSymbol ||
39-
!classSymbol.HasAttributeWithType(notifyDataErrorInfoAttributeSymbol))
33+
context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableValidator") is not INamedTypeSymbol observableValidatorSymbol)
4034
{
4135
return;
4236
}
4337

44-
// If the containing type is not valid, emit a diagnostic
45-
if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableValidator") is not INamedTypeSymbol observableValidatorSymbol ||
46-
!classSymbol.InheritsFromType(observableValidatorSymbol))
38+
context.RegisterSymbolAction(context =>
4739
{
48-
context.ReportDiagnostic(Diagnostic.Create(
49-
InvalidTypeForNotifyDataErrorInfoError,
50-
classSymbol.Locations.FirstOrDefault(),
51-
classSymbol));
52-
}
53-
}, SymbolKind.NamedType);
40+
// We're looking for all class declarations
41+
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class, IsImplicitlyDeclared: false } classSymbol)
42+
{
43+
return;
44+
}
45+
46+
// Emit a diagnostic for types that use [NotifyDataErrorInfo] but don't inherit from ObservableValidator
47+
if (classSymbol.HasAttributeWithType(notifyDataErrorInfoAttributeSymbol) &&
48+
!classSymbol.InheritsFromType(observableValidatorSymbol))
49+
{
50+
context.ReportDiagnostic(Diagnostic.Create(
51+
InvalidTypeForNotifyDataErrorInfoError,
52+
classSymbol.Locations.FirstOrDefault(),
53+
classSymbol));
54+
}
55+
}, SymbolKind.NamedType);
56+
});
5457
}
5558
}

src/CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/Analyzers/InvalidClassLevelNotifyPropertyChangedRecipientsAttributeAnalyzer.cs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,35 @@ public override void Initialize(AnalysisContext context)
2626
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics);
2727
context.EnableConcurrentExecution();
2828

29-
context.RegisterSymbolAction(static context =>
29+
context.RegisterCompilationStartAction(static context =>
3030
{
31-
// We're looking for all class declarations
32-
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class, IsImplicitlyDeclared: false } classSymbol)
33-
{
34-
return;
35-
}
36-
37-
// Only inspect classes that are using [NotifyPropertyChangedRecipients]
31+
// Get the symbols for [NotifyPropertyChangedRecipients], ObservableRecipient and [ObservableRecipient]
3832
if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.NotifyPropertyChangedRecipientsAttribute") is not INamedTypeSymbol notifyPropertyChangedRecipientsAttributeSymbol ||
39-
!classSymbol.HasAttributeWithType(notifyPropertyChangedRecipientsAttributeSymbol))
33+
context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableRecipient") is not INamedTypeSymbol observableRecipientSymbol ||
34+
context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableRecipientAttribute") is not INamedTypeSymbol observableRecipientAttributeSymbol)
4035
{
4136
return;
4237
}
4338

44-
// If the containing type is not valid, emit a diagnostic
45-
if (context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableRecipient") is INamedTypeSymbol observableRecipientSymbol &&
46-
context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableRecipientAttribute") is INamedTypeSymbol observableRecipientAttributeSymbol &&
47-
!classSymbol.InheritsFromType(observableRecipientSymbol) &&
48-
!classSymbol.HasOrInheritsAttributeWithType(observableRecipientAttributeSymbol))
39+
context.RegisterSymbolAction(context =>
4940
{
50-
context.ReportDiagnostic(Diagnostic.Create(
51-
InvalidTypeForNotifyPropertyChangedRecipientsError,
52-
classSymbol.Locations.FirstOrDefault(),
53-
classSymbol));
54-
}
55-
}, SymbolKind.NamedType);
41+
// We're looking for all class declarations
42+
if (context.Symbol is not INamedTypeSymbol { TypeKind: TypeKind.Class, IsImplicitlyDeclared: false } classSymbol)
43+
{
44+
return;
45+
}
46+
47+
// Emit a diagnstic for types that use [NotifyPropertyChangedRecipients] but are neither inheriting from ObservableRecipient nor using [ObservableRecipient]
48+
if (classSymbol.HasAttributeWithType(notifyPropertyChangedRecipientsAttributeSymbol) &&
49+
!classSymbol.InheritsFromType(observableRecipientSymbol) &&
50+
!classSymbol.HasOrInheritsAttributeWithType(observableRecipientAttributeSymbol))
51+
{
52+
context.ReportDiagnostic(Diagnostic.Create(
53+
InvalidTypeForNotifyPropertyChangedRecipientsError,
54+
classSymbol.Locations.FirstOrDefault(),
55+
classSymbol));
56+
}
57+
}, SymbolKind.NamedType);
58+
});
5659
}
5760
}

0 commit comments

Comments
 (0)