Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Avoid passing properties to async void [RelayCommand] fixer
  • Loading branch information
Sergio0694 committed Jun 8, 2023
commit c6eedeea8621ddb6241ce458a09ee1b330f26fda
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using System.Composition;
using System.Threading;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.SourceGenerators;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes;
Expand Down Expand Up @@ -40,17 +39,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
Diagnostic diagnostic = context.Diagnostics[0];
TextSpan diagnosticSpan = context.Span;

// Retrieve the property passed by the analyzer
if (diagnostic.Properties[AsyncVoidReturningRelayCommandMethodAnalyzer.MethodNameKey] is not string methodName)
{
return;
}

SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);

// Get the method declaration from the target diagnostic
if (root!.FindNode(diagnosticSpan) is MethodDeclarationSyntax { Identifier.Text: string identifierName } methodDeclaration &&
identifierName == methodName)
if (root!.FindNode(diagnosticSpan) is MethodDeclarationSyntax methodDeclaration)
{
// Register the code fix to update the return type to be Task instead
context.RegisterCodeFix(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ namespace CommunityToolkit.Mvvm.SourceGenerators;
[DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class AsyncVoidReturningRelayCommandMethodAnalyzer : DiagnosticAnalyzer
{
/// <summary>
/// The key for the name of the target method to update.
/// </summary>
internal const string MethodNameKey = "MethodName";

/// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(AsyncVoidReturningRelayCommandMethod);

Expand Down Expand Up @@ -57,7 +52,6 @@ public override void Initialize(AnalysisContext context)
context.ReportDiagnostic(Diagnostic.Create(
AsyncVoidReturningRelayCommandMethod,
context.Symbol.Locations.FirstOrDefault(),
ImmutableDictionary.Create<string, string?>().Add(MethodNameKey, methodSymbol.Name),
context.Symbol));
}, SymbolKind.Method);
});
Expand Down