File tree Expand file tree Collapse file tree 4 files changed +49
-0
lines changed Expand file tree Collapse file tree 4 files changed +49
-0
lines changed Original file line number Diff line number Diff line change @@ -25,3 +25,4 @@ MVVMTK0017 | CommunityToolkit.Mvvm.SourceGenerators.INotifyPropertyChangedGenera
25
25
MVVMTK0018 | CommunityToolkit.Mvvm.SourceGenerators.ObservableObjectGenerator | Error | See https://aka.ms/mvvmtoolkit/error
26
26
MVVMTK0019 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit/error
27
27
MVVMTK0020 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit/error
28
+ MVVMTK0021 | CommunityToolkit.Mvvm.SourceGenerators.ObservableRecipientGenerator | Error | See https://aka.ms/mvvmtoolkit/error
Original file line number Diff line number Diff line change @@ -79,6 +79,16 @@ protected override bool ValidateTargetType(INamedTypeSymbol typeSymbol, Observab
79
79
return false ;
80
80
}
81
81
82
+ // Check if the type already inherits [ObservableRecipient]
83
+ if ( typeSymbol . InheritsAttributeWithFullyQualifiedName ( "global::CommunityToolkit.Mvvm.ComponentModel.ObservableRecipientAttribute" ) )
84
+ {
85
+ builder . Add ( InvalidAttributeCombinationForObservableRecipientAttributeError , typeSymbol , typeSymbol ) ;
86
+
87
+ diagnostics = builder . ToImmutable ( ) ;
88
+
89
+ return false ;
90
+ }
91
+
82
92
// In order to use [ObservableRecipient], the target type needs to inherit from ObservableObject,
83
93
// or be annotated with [ObservableObject] or [INotifyPropertyChanged] (with additional helpers).
84
94
if ( ! typeSymbol . InheritsFromFullyQualifiedName ( "global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject" ) &&
Original file line number Diff line number Diff line change @@ -331,4 +331,20 @@ internal static class DiagnosticDescriptors
331
331
isEnabledByDefault : true ,
332
332
description : "Fields not annotated with [ObservableProperty] cannot use [AlsoNotifyChangeFor] and [AlsoNotifyCanExecuteFor]." ,
333
333
helpLinkUri : "https://aka.ms/mvvmtoolkit" ) ;
334
+
335
+ /// <summary>
336
+ /// Gets a <see cref="DiagnosticDescriptor"/> indicating when <c>[ObservableRecipient]</c> is applied to a type with an attribute already.
337
+ /// <para>
338
+ /// Format: <c>"Cannot apply [ObservableRecipient] to type {0}, as it already inherits this attribute from a base type"</c>.
339
+ /// </para>
340
+ /// </summary>
341
+ public static readonly DiagnosticDescriptor InvalidAttributeCombinationForObservableRecipientAttributeError = new DiagnosticDescriptor (
342
+ id : "MVVMTK0021" ,
343
+ title : "Invalid target type for [ObservableRecipient]" ,
344
+ messageFormat : "Cannot apply [ObservableRecipient] to type {0}, as it already inherits this attribute from a base type" ,
345
+ category : typeof ( ObservableRecipientGenerator ) . FullName ,
346
+ defaultSeverity : DiagnosticSeverity . Error ,
347
+ isEnabledByDefault : true ,
348
+ description : "Cannot apply [ObservableRecipient] to a type that already inherits this attribute from a base type." ,
349
+ helpLinkUri : "https://aka.ms/mvvmtoolkit" ) ;
334
350
}
Original file line number Diff line number Diff line change @@ -998,6 +998,28 @@ public partial class MyViewModel
998
998
VerifyGeneratedDiagnostics < ObservablePropertyGenerator > ( source , "MVVMTK0020" ) ;
999
999
}
1000
1000
1001
+ [ TestMethod ]
1002
+ public void InvalidAttributeCombinationForObservableRecipientAttributeError ( )
1003
+ {
1004
+ string source = @"
1005
+ using CommunityToolkit.Mvvm.ComponentModel;
1006
+
1007
+ namespace MyApp
1008
+ {
1009
+ [ObservableRecipient]
1010
+ public partial class A : ObservableObject
1011
+ {
1012
+ }
1013
+
1014
+ [ObservableRecipient]
1015
+ public partial class B : A
1016
+ {
1017
+ }
1018
+ }" ;
1019
+
1020
+ VerifyGeneratedDiagnostics < ObservableRecipientGenerator > ( source , "MVVMTK0021" ) ;
1021
+ }
1022
+
1001
1023
/// <summary>
1002
1024
/// Verifies the output of a source generator.
1003
1025
/// </summary>
You can’t perform that action at this time.
0 commit comments