6
6
using System . ComponentModel ;
7
7
using System . Linq ;
8
8
using CommunityToolkit . Mvvm . SourceGenerators . ComponentModel . Models ;
9
+ using CommunityToolkit . Mvvm . SourceGenerators . Diagnostics ;
9
10
using CommunityToolkit . Mvvm . SourceGenerators . Extensions ;
10
11
using Microsoft . CodeAnalysis ;
11
12
using Microsoft . CodeAnalysis . CSharp ;
12
13
using Microsoft . CodeAnalysis . CSharp . Syntax ;
14
+ using static CommunityToolkit . Mvvm . SourceGenerators . Diagnostics . DiagnosticDescriptors ;
13
15
using static Microsoft . CodeAnalysis . CSharp . SyntaxFactory ;
14
16
15
17
namespace CommunityToolkit . Mvvm . SourceGenerators ;
@@ -26,9 +28,12 @@ internal static class Execute
26
28
/// Processes a given field.
27
29
/// </summary>
28
30
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
31
+ /// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
29
32
/// <returns>The resulting <see cref="PropertyInfo"/> instance for <paramref name="fieldSymbol"/>.</returns>
30
- public static PropertyInfo GetInfo ( IFieldSymbol fieldSymbol )
33
+ public static PropertyInfo GetInfo ( IFieldSymbol fieldSymbol , out ImmutableArray < Diagnostic > diagnostics )
31
34
{
35
+ ImmutableArray < Diagnostic > . Builder builder = ImmutableArray . CreateBuilder < Diagnostic > ( ) ;
36
+
32
37
// Check whether the containing type implements INotifyPropertyChanging and whether it inherits from ObservableValidator
33
38
bool isObservableObject = fieldSymbol . ContainingType . InheritsFrom ( "global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject" ) ;
34
39
bool isObservableValidator = fieldSymbol . ContainingType . InheritsFrom ( "global::CommunityToolkit.Mvvm.ComponentModel.ObservableValidator" ) ;
@@ -81,6 +86,20 @@ public static PropertyInfo GetInfo(IFieldSymbol fieldSymbol)
81
86
}
82
87
}
83
88
89
+ // Log the diagnostics if needed
90
+ if ( validationAttributes . Count > 0 &&
91
+ ! isObservableValidator )
92
+ {
93
+ builder . Add (
94
+ MissingObservableValidatorInheritanceError ,
95
+ fieldSymbol ,
96
+ fieldSymbol . ContainingType ,
97
+ fieldSymbol . Name ,
98
+ validationAttributes . Count ) ;
99
+ }
100
+
101
+ diagnostics = builder . ToImmutable ( ) ;
102
+
84
103
return new (
85
104
typeName ,
86
105
isNullableReferenceType ,
0 commit comments