Skip to content

Commit 8bbc309

Browse files
committed
Enable diagnostics for ObservablePropertyGenerator
1 parent bf02bee commit 8bbc309

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.Execute.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
using System.ComponentModel;
77
using System.Linq;
88
using CommunityToolkit.Mvvm.SourceGenerators.ComponentModel.Models;
9+
using CommunityToolkit.Mvvm.SourceGenerators.Diagnostics;
910
using CommunityToolkit.Mvvm.SourceGenerators.Extensions;
1011
using Microsoft.CodeAnalysis;
1112
using Microsoft.CodeAnalysis.CSharp;
1213
using Microsoft.CodeAnalysis.CSharp.Syntax;
14+
using static CommunityToolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1315
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1416

1517
namespace CommunityToolkit.Mvvm.SourceGenerators;
@@ -26,9 +28,12 @@ internal static class Execute
2628
/// Processes a given field.
2729
/// </summary>
2830
/// <param name="fieldSymbol">The input <see cref="IFieldSymbol"/> instance to process.</param>
31+
/// <param name="diagnostics">The resulting diagnostics from the processing operation.</param>
2932
/// <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)
3134
{
35+
ImmutableArray<Diagnostic>.Builder builder = ImmutableArray.CreateBuilder<Diagnostic>();
36+
3237
// Check whether the containing type implements INotifyPropertyChanging and whether it inherits from ObservableValidator
3338
bool isObservableObject = fieldSymbol.ContainingType.InheritsFrom("global::CommunityToolkit.Mvvm.ComponentModel.ObservableObject");
3439
bool isObservableValidator = fieldSymbol.ContainingType.InheritsFrom("global::CommunityToolkit.Mvvm.ComponentModel.ObservableValidator");
@@ -81,6 +86,20 @@ public static PropertyInfo GetInfo(IFieldSymbol fieldSymbol)
8186
}
8287
}
8388

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+
84103
return new(
85104
typeName,
86105
isNullableReferenceType,

CommunityToolkit.Mvvm.SourceGenerators/ComponentModel/ObservablePropertyGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
5959
.Select(static (item, _) =>
6060
{
6161
HierarchyInfo hierarchy = HierarchyInfo.From(item.ContainingType);
62-
PropertyInfo propertyInfo = Execute.GetInfo(item);
62+
PropertyInfo propertyInfo = Execute.GetInfo(item, out ImmutableArray<Diagnostic> diagnostics);
6363

64-
return (hierarchy, new Result<PropertyInfo>(propertyInfo, ImmutableArray<Diagnostic>.Empty));
64+
return (hierarchy, new Result<PropertyInfo>(propertyInfo, diagnostics));
6565
});
6666

6767
// Output the diagnostics

0 commit comments

Comments
 (0)