You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// A diagnostic analyzer that generates a suggestion whenever <c>[ObservableProperty]</c> is used on a semi-auto property when a partial property could be used instead.
// Get the symbol for [ObservableProperty] and ObservableObject
45
+
if(context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute")is not INamedTypeSymbolobservablePropertySymbol||
46
+
context.Compilation.GetTypeByMetadataName("CommunityToolkit.Mvvm.ComponentModel.ObservableObject")is not INamedTypeSymbolobservableObjectSymbol)
47
+
{
48
+
return;
49
+
}
50
+
51
+
// Get the symbol for the SetProperty<T> method as well
// We want to process both accessors, where we specifically need both the syntax
104
+
// and their semantic model to verify what they're doing. We can use a code callback.
105
+
context.RegisterOperationBlockAction(context =>
106
+
{
107
+
// Make sure the current symbol is a property accessor
108
+
if(context.OwningSymbolis not IMethodSymbol{MethodKind:MethodKind.PropertyGet or MethodKind.PropertySet,AssociatedSymbol:IPropertySymbolpropertySymbol})
109
+
{
110
+
return;
111
+
}
112
+
113
+
// If so, check that we are actually processing one of the properties we care about
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when a duplicate declaration of <see cref="INotifyPropertyChanged"/> would happen.
49
54
/// <para>
@@ -923,4 +928,20 @@ internal static class DiagnosticDescriptors
923
928
isEnabledByDefault:true,
924
929
description:"A property using [ObservableProperty] returns a pointer-like value ([ObservableProperty] must be used on properties of a non pointer-like type).",
/// Gets a <see cref="DiagnosticDescriptor"/> for when a semi-auto property can be converted to use <c>[ObservableProperty]</c> instead.
934
+
/// <para>
935
+
/// Format: <c>"The semi-auto property {0}.{1} can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)"</c>.
title:"Prefer using [ObservableProperty] over semi-auto properties",
941
+
messageFormat:"""The semi-auto property {0}.{1} can be converted to a partial property using [ObservableProperty], which is recommended (doing so makes the code less verbose and results in more optimized code)""",
description:"Semi-auto properties should be converted to partial properties using [ObservableProperty] when possible, which is recommended (doing so makes the code less verbose and results in more optimized code).",
0 commit comments