Skip to content

Commit

Permalink
Avoid passing properties to async void [RelayCommand] fixer
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio0694 committed Jun 8, 2023
1 parent ce23265 commit c6eedee
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 15 deletions.
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

0 comments on commit c6eedee

Please sign in to comment.