Skip to content

Commit 6e497b1

Browse files
committed
Fix diagnostics for generated properties
1 parent ce44cff commit 6e497b1

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

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

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,34 @@ private static bool TryGatherDependentPropertyChangedNames(
138138
}
139139
else
140140
{
141-
diagnostics.Add(
142-
AlsoNotifyChangeForInvalidTargetError,
143-
fieldSymbol,
144-
dependentPropertyName ?? "",
145-
fieldSymbol.ContainingType);
146-
}
141+
bool isTargetValid = false;
142+
143+
// Check for generated properties too
144+
foreach (ISymbol member in fieldSymbol.ContainingType.GetMembers())
145+
{
146+
if (member is IFieldSymbol otherFieldSymbol &&
147+
!SymbolEqualityComparer.Default.Equals(fieldSymbol, otherFieldSymbol) &&
148+
otherFieldSymbol.HasAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.ComponentModel.ObservablePropertyAttribute") &&
149+
dependentPropertyName == GetGeneratedPropertyName(otherFieldSymbol))
150+
{
151+
propertyChangedNames.Add(dependentPropertyName);
152+
153+
isTargetValid = true;
154+
155+
break;
156+
}
157+
}
158+
159+
// Add the diagnostic if the target is definitely invalid
160+
if (!isTargetValid)
161+
{
162+
diagnostics.Add(
163+
AlsoNotifyChangeForInvalidTargetError,
164+
fieldSymbol,
165+
dependentPropertyName ?? "",
166+
fieldSymbol.ContainingType);
167+
}
168+
}
147169
}
148170

149171
return true;

CommunityToolkit.Mvvm.SourceGenerators/Extensions/ISymbolExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Collections.Immutable;
56
using Microsoft.CodeAnalysis;
67

78
namespace CommunityToolkit.Mvvm.SourceGenerators.Extensions;
@@ -32,6 +33,27 @@ public static bool HasFullyQualifiedName(this ISymbol symbol, string name)
3233
return symbol.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat) == name;
3334
}
3435

36+
/// <summary>
37+
/// Checks whether or not a given symbol has an attribute with the specified full name.
38+
/// </summary>
39+
/// <param name="symbol">The input <see cref="ISymbol"/> instance to check.</param>
40+
/// <param name="name">The attribute name to look for.</param>
41+
/// <returns>Whether or not <paramref name="symbol"/> has an attribute with the specified name.</returns>
42+
public static bool HasAttributeWithFullyQualifiedName(this ISymbol symbol, string name)
43+
{
44+
ImmutableArray<AttributeData> attributes = symbol.GetAttributes();
45+
46+
foreach (AttributeData attribute in attributes)
47+
{
48+
if (attribute.AttributeClass?.HasFullyQualifiedName(name) == true)
49+
{
50+
return true;
51+
}
52+
}
53+
54+
return false;
55+
}
56+
3557
/// <summary>
3658
/// Calculates the effective accessibility for a given symbol.
3759
/// </summary>

0 commit comments

Comments
 (0)