Skip to content

Commit 929bef6

Browse files
committed
Fix diagnostics for generated commands
1 parent 6e497b1 commit 929bef6

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,32 @@ propertySymbol is INamedTypeSymbol typeSymbol &&
203203
}
204204
else
205205
{
206-
diagnostics.Add(
206+
bool isTargetValid = false;
207+
208+
// Check for generated commands too
209+
foreach (ISymbol member in fieldSymbol.ContainingType.GetMembers())
210+
{
211+
if (member is IMethodSymbol methodSymbol &&
212+
methodSymbol.HasAttributeWithFullyQualifiedName("global::CommunityToolkit.Mvvm.Input.ICommandAttribute") &&
213+
commandName == ICommandGenerator.Execute.GetGeneratedFieldAndPropertyNames(methodSymbol).PropertyName)
214+
{
215+
notifiedCommandNames.Add(commandName);
216+
217+
isTargetValid = true;
218+
219+
break;
220+
}
221+
}
222+
223+
// Add the diagnostic if the target is definitely invalid
224+
if (!isTargetValid)
225+
{
226+
diagnostics.Add(
207227
AlsoNotifyCanExecuteForInvalidTargetError,
208228
fieldSymbol,
209229
commandName ?? "",
210230
fieldSymbol.ContainingType);
231+
}
211232
}
212233
}
213234

CommunityToolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.Execute.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ partial class ICommandGenerator
2323
/// <summary>
2424
/// A container for all the logic for <see cref="ICommandGenerator"/>.
2525
/// </summary>
26-
private static class Execute
26+
internal static class Execute
2727
{
2828
/// <summary>
2929
/// Processes a given target method.
@@ -305,7 +305,7 @@ public static ImmutableArray<MemberDeclarationSyntax> GetSyntax(CommandInfo comm
305305
/// </summary>
306306
/// <param name="methodSymbol">The input <see cref="IMethodSymbol"/> instance to process.</param>
307307
/// <returns>The generated field and property names for <paramref name="methodSymbol"/>.</returns>
308-
private static (string FieldName, string PropertyName) GetGeneratedFieldAndPropertyNames(IMethodSymbol methodSymbol)
308+
public static (string FieldName, string PropertyName) GetGeneratedFieldAndPropertyNames(IMethodSymbol methodSymbol)
309309
{
310310
string propertyName = methodSymbol.Name;
311311

0 commit comments

Comments
 (0)