Skip to content

Commit 5a08c7a

Browse files
committed
Added ICommandGenerator diagnostics
1 parent 56f386d commit 5a08c7a

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

Microsoft.Toolkit.Mvvm.SourceGenerators/AnalyzerReleases.Unshipped.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,6 @@ MVVMTK0006 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableObjectGenerator |
1414
MVVMTK0007 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableRecipientGenerator | Error | See https://aka.ms/mvvmtoolkit
1515
MVVMTK0008 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservableRecipientGenerator | Error | See https://aka.ms/mvvmtoolkit
1616
MVVMTK0009 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit
17-
MVVMTK0010 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit
17+
MVVMTK0010 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit
18+
MVVMTK0011 | Microsoft.Toolkit.Mvvm.SourceGenerators.ObservablePropertyGenerator | Error | See https://aka.ms/mvvmtoolkit
19+
MVVMTK0012 | Microsoft.Toolkit.Mvvm.SourceGenerators.ICommandGenerator | Error | See https://aka.ms/mvvmtoolkit

Microsoft.Toolkit.Mvvm.SourceGenerators/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.ComponentModel;
66
using Microsoft.CodeAnalysis;
77
using Microsoft.Toolkit.Mvvm.ComponentModel;
8+
using Microsoft.Toolkit.Mvvm.Input;
89

910
namespace Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics
1011
{
@@ -150,7 +151,7 @@ internal static class DiagnosticDescriptors
150151
public static readonly DiagnosticDescriptor MissingObservableValidatorInheritanceError = new(
151152
id: "MVVMTK0009",
152153
title: "Missing ObservableValidator inheritance",
153-
messageFormat: $"The field {{0}}.{{1}} cannot be used to generate an observable property, as it has {{2}} validation attribute(s) but is declared in a type that doesn't inherit from ObservableValidator",
154+
messageFormat: "The field {0}.{1} cannot be used to generate an observable property, as it has {2} validation attribute(s) but is declared in a type that doesn't inherit from ObservableValidator",
154155
category: typeof(ObservablePropertyGenerator).FullName,
155156
defaultSeverity: DiagnosticSeverity.Error,
156157
isEnabledByDefault: true,
@@ -172,5 +173,37 @@ internal static class DiagnosticDescriptors
172173
isEnabledByDefault: true,
173174
description: $"The {nameof(ObservablePropertyGenerator)} generator encountered an error while processing a type. Please report this issue at https://aka.ms/mvvmtoolkit.",
174175
helpLinkUri: "https://aka.ms/mvvmtoolkit");
176+
177+
/// <summary>
178+
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when <see cref="ICommandGenerator"/> failed to run on a given type.
179+
/// <para>
180+
/// Format: <c>"The generator ICommandGenerator failed to execute on type {0}"</c>.
181+
/// </para>
182+
/// </summary>
183+
public static readonly DiagnosticDescriptor ICommandGeneratorError = new(
184+
id: "MVVMTK0011",
185+
title: $"Internal error for {nameof(ICommandGenerator)}",
186+
messageFormat: $"The generator {nameof(ICommandGenerator)} failed to execute on type {{0}}",
187+
category: typeof(ICommandGenerator).FullName,
188+
defaultSeverity: DiagnosticSeverity.Error,
189+
isEnabledByDefault: true,
190+
description: $"The {nameof(ICommandGenerator)} generator encountered an error while processing a type. Please report this issue at https://aka.ms/mvvmtoolkit.",
191+
helpLinkUri: "https://aka.ms/mvvmtoolkit");
192+
193+
/// <summary>
194+
/// Gets a <see cref="DiagnosticDescriptor"/> indicating when an annotated method to generate a command for has an invalid signature.
195+
/// <para>
196+
/// Format: <c>"The method {0}.{1} cannot be used to generate a command property, as its signature isn't compatible with any of the existing relay command types"</c>.
197+
/// </para>
198+
/// </summary>
199+
public static readonly DiagnosticDescriptor InvalidICommandMethodSignatureError = new(
200+
id: "MVVMTK0012",
201+
title: "Invalid ICommand method signature",
202+
messageFormat: "The method {0}.{1} cannot be used to generate a command property, as its signature isn't compatible with any of the existing relay command types",
203+
category: typeof(ICommandGenerator).FullName,
204+
defaultSeverity: DiagnosticSeverity.Error,
205+
isEnabledByDefault: true,
206+
description: $"Cannot apply [{nameof(ICommandAttribute)}] to methods with a signature that doesn't match any of the existing relay command types.",
207+
helpLinkUri: "https://aka.ms/mvvmtoolkit");
175208
}
176209
}

Microsoft.Toolkit.Mvvm.SourceGenerators/Input/ICommandGenerator.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
using Microsoft.CodeAnalysis.CSharp.Syntax;
1414
using Microsoft.CodeAnalysis.Text;
1515
using Microsoft.Toolkit.Mvvm.Input;
16+
using Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics;
1617
using Microsoft.Toolkit.Mvvm.SourceGenerators.Extensions;
1718
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;
1819
using static Microsoft.CodeAnalysis.SymbolDisplayTypeQualificationStyle;
20+
using static Microsoft.Toolkit.Mvvm.SourceGenerators.Diagnostics.DiagnosticDescriptors;
1921

2022
namespace Microsoft.Toolkit.Mvvm.SourceGenerators
2123
{
@@ -52,7 +54,7 @@ public void Execute(GeneratorExecutionContext context)
5254
}
5355
catch
5456
{
55-
// TODO
57+
context.ReportDiagnostic(ICommandGeneratorError, classDeclaration, items.Key);
5658
}
5759
}
5860
}
@@ -136,6 +138,8 @@ private static IEnumerable<MemberDeclarationSyntax> CreateCommandMembers(Generat
136138
out ITypeSymbol? commandClassTypeSymbol,
137139
out ITypeSymbol? delegateTypeSymbol))
138140
{
141+
context.ReportDiagnostic(InvalidICommandMethodSignatureError, methodSymbol, methodSymbol.ContainingType, methodSymbol);
142+
139143
return Array.Empty<MemberDeclarationSyntax>();
140144
}
141145

UnitTests/UnitTests.SourceGenerators/Test_SourceGeneratorsDiagnostics.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,25 @@ public partial class SampleViewModel
221221
VerifyGeneratedDiagnostics<ObservablePropertyGenerator>(source, "MVVMTK0009");
222222
}
223223

224+
[TestCategory("Mvvm")]
225+
[TestMethod]
226+
public void InvalidICommandMethodSignatureError()
227+
{
228+
string source = @"
229+
using Microsoft.Toolkit.Mvvm.Input;
230+
231+
namespace MyApp
232+
{
233+
public partial class SampleViewModel
234+
{
235+
[ICommand]
236+
private string GreetUser() => ""Hello world!"";
237+
}
238+
}";
239+
240+
VerifyGeneratedDiagnostics<ICommandGenerator>(source, "MVVMTK0012");
241+
}
242+
224243
/// <summary>
225244
/// Verifies the output of a source generator.
226245
/// </summary>

0 commit comments

Comments
 (0)