Skip to content

Commit 780b596

Browse files
committed
Add new checks and diagnostics for properties causing conflicts
1 parent df07309 commit 780b596

File tree

4 files changed

+73
-0
lines changed

4 files changed

+73
-0
lines changed

CommunityToolkit.Mvvm.SourceGenerators/AnalyzerReleases.Shipped.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ MVVMTK0020 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator
3030
MVVMTK0021 | CommunityToolkit.Mvvm.SourceGenerators.ObservableRecipientGenerator | Error | See https://aka.ms/mvvmtoolkit/error
3131
MVVMTK0022 | CommunityToolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit/error
3232
MVVMTK0023 | CommunityToolkit.Mvvm.SourceGenerators.ICommandGenerator | Error | See https://aka.ms/mvvmtoolkit/error
33+
MVVMTK0024 | CommunityToolkit.Mvvm.SourceGenerators.ICommandGenerator | Error | See https://aka.ms/mvvmtoolkit/error

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ internal static class Execute
7070
return null;
7171
}
7272

73+
// Check for special cases that are explicitly not allowed
74+
if (IsGeneratedPropertyInvalid(propertyName, fieldSymbol.Type))
75+
{
76+
builder.Add(
77+
InvalidObservablePropertyError,
78+
fieldSymbol,
79+
fieldSymbol.ContainingType,
80+
fieldSymbol.Name);
81+
82+
diagnostics = builder.ToImmutable();
83+
84+
return null;
85+
}
86+
7387
ImmutableArray<string>.Builder propertyChangedNames = ImmutableArray.CreateBuilder<string>();
7488
ImmutableArray<string>.Builder propertyChangingNames = ImmutableArray.CreateBuilder<string>();
7589
ImmutableArray<string>.Builder notifiedCommandNames = ImmutableArray.CreateBuilder<string>();
@@ -170,6 +184,29 @@ private static bool IsTargetTypeValid(
170184
return isObservableObject || hasObservableObjectAttribute || hasINotifyPropertyChangedAttribute;
171185
}
172186

187+
/// <summary>
188+
/// Checks whether the generated property would be a special case that is marked as invalid.
189+
/// </summary>
190+
/// <param name="propertyName">The property name.</param>
191+
/// <param name="propertyType">The property type.</param>
192+
/// <returns>Whether the generated property is invalid.</returns>
193+
private static bool IsGeneratedPropertyInvalid(string propertyName, ITypeSymbol propertyType)
194+
{
195+
// If the generated property name is called "Property" and the type is either object or it is PropertyChangedEventArgs or
196+
// PropertyChangingEventArgs (or a type derived from either of those two types), consider it invalid. This is needed because
197+
// if such a property was generated, the partial On<PROPERTY_NAME>Changing and OnPropertyChanging(PropertyChangingEventArgs)
198+
// methods, as well as the partial On<PROPERTY_NAME>Changed and OnPropertyChanged(PropertyChangedEventArgs) methods.
199+
if (propertyName == "Property")
200+
{
201+
return
202+
propertyType.SpecialType == SpecialType.System_Object ||
203+
propertyType.HasOrInheritsFromFullyQualifiedName("global::System.ComponentModel.PropertyChangedEventArgs") ||
204+
propertyType.HasOrInheritsFromFullyQualifiedName("global::System.ComponentModel.PropertyChangingEventArgs");
205+
}
206+
207+
return false;
208+
}
209+
173210
/// <summary>
174211
/// Tries to gather dependent properties from the given attribute.
175212
/// </summary>

CommunityToolkit.Mvvm.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,4 +379,20 @@ internal static class DiagnosticDescriptors
379379
isEnabledByDefault: true,
380380
description: "Methods with multiple overloads cannot be annotated with [ICommand], as command methods must be unique within their containing type.",
381381
helpLinkUri: "https://aka.ms/mvvmtoolkit");
382+
383+
/// <summary>
384+
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when a generated property created with <c>[ObservableProperty]</c> would cause conflicts with other generated members.
385+
/// <para>
386+
/// Format: <c>"The field {0}.{1} cannot be used to generate an observable property, as its name or type would cause conflicts with other generated members"</c>.
387+
/// </para>
388+
/// </summary>
389+
public static readonly DiagnosticDescriptor InvalidObservablePropertyError = new DiagnosticDescriptor(
390+
id: "MVVMTK0024",
391+
title: "Invalid generated property declaration",
392+
messageFormat: "The field {0}.{1} cannot be used to generate an observable property, as its name or type would cause conflicts with other generated members",
393+
category: typeof(ObservablePropertyGenerator).FullName,
394+
defaultSeverity: DiagnosticSeverity.Error,
395+
isEnabledByDefault: true,
396+
description: "The fields annotated with [ObservableProperty] cannot result in a property name or have a type that would cause conflicts with other generated members.",
397+
helpLinkUri: "https://aka.ms/mvvmtoolkit");
382398
}

CommunityToolkit.Mvvm.SourceGenerators/Extensions/ITypeSymbolExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,25 @@ namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
1313
/// </summary>
1414
internal static class ITypeSymbolExtensions
1515
{
16+
/// <summary>
17+
/// Checks whether or not a given <see cref="ITypeSymbol"/> has or inherits from a specified type.
18+
/// </summary>
19+
/// <param name="typeSymbol">The target <see cref="ITypeSymbol"/> instance to check.</param>
20+
/// <param name="name">The full name of the type to check for inheritance.</param>
21+
/// <returns>Whether or not <paramref name="typeSymbol"/> is or inherits from <paramref name="name"/>.</returns>
22+
public static bool HasOrInheritsFromFullyQualifiedName(this ITypeSymbol typeSymbol, string name)
23+
{
24+
for (ITypeSymbol? currentType = typeSymbol; currentType is not null; currentType = currentType.BaseType)
25+
{
26+
if (currentType.HasFullyQualifiedName(name))
27+
{
28+
return true;
29+
}
30+
}
31+
32+
return false;
33+
}
34+
1635
/// <summary>
1736
/// Checks whether or not a given <see cref="ITypeSymbol"/> inherits from a specified type.
1837
/// </summary>

0 commit comments

Comments
 (0)