Skip to content

Commit 54baa39

Browse files
committed
Disable validation/recipient generator for abstract types
1 parent b8db4b0 commit 54baa39

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservableValidatorValidateAllPropertiesGenerator.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public sealed partial class ObservableValidatorValidateAllPropertiesGenerator :
2020
/// <inheritdoc/>
2121
public void Initialize(IncrementalGeneratorInitializationContext context)
2222
{
23-
// Get all class declarations
23+
// Get all class declarations. We intentionally skip generating code for abstract types, as that would never be used.
24+
// The methods that are generated by this generator are retrieved through reflection using the type of the invoking
25+
// instance as discriminator, which means a type that is abstract could never be used (since it couldn't be instantiated).
2426
IncrementalValuesProvider<INamedTypeSymbol> typeSymbols =
2527
context.SyntaxProvider
2628
.CreateSyntaxProvider(
2729
static (node, _) => node is ClassDeclarationSyntax,
2830
static (context, _) => (context.Node, Symbol: (INamedTypeSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!))
29-
.Where(static item => item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
31+
.Where(static item => !item.Symbol.IsAbstract && item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
3032
.Select(static (item, _) => item.Symbol);
3133

3234
// Get the types that inherit from ObservableValidator and gather their info

CommunityToolkit.Mvvm.SourceGenerators/Messaging/IMessengerRegisterAllGenerator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2525
// definitions (it might happen if a recipient has partial declarations). To do this, all pairs
2626
// of class declarations and associated symbols are gathered, and then only the pair where the
2727
// class declaration is the first syntax reference for the associated symbol is kept.
28+
// Just like with the ObservableValidator generator, we also intentionally skip abstract types.
2829
IncrementalValuesProvider<INamedTypeSymbol> typeSymbols =
2930
context.SyntaxProvider
3031
.CreateSyntaxProvider(
3132
static (node, _) => node is ClassDeclarationSyntax,
3233
static (context, _) => (context.Node, Symbol: (INamedTypeSymbol)context.SemanticModel.GetDeclaredSymbol(context.Node)!))
33-
.Where(static item => item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
34+
.Where(static item => !item.Symbol.IsAbstract && item.Node.IsFirstSyntaxDeclarationForSymbol(item.Symbol))
3435
.Select(static (item, _) => item.Symbol);
3536

3637
// Get the target IRecipient<TMessage> interfaces and filter out other types

0 commit comments

Comments
 (0)