提交 c6eedeea 编写于 作者: S Sergio Pedri

Avoid passing properties to async void [RelayCommand] fixer

上级 ce23265d
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
using System.Composition; using System.Composition;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommunityToolkit.Mvvm.SourceGenerators;
using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeActions; using Microsoft.CodeAnalysis.CodeActions;
using Microsoft.CodeAnalysis.CodeFixes; using Microsoft.CodeAnalysis.CodeFixes;
...@@ -40,17 +39,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) ...@@ -40,17 +39,10 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
Diagnostic diagnostic = context.Diagnostics[0]; Diagnostic diagnostic = context.Diagnostics[0];
TextSpan diagnosticSpan = context.Span; 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); SyntaxNode? root = await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(false);
// Get the method declaration from the target diagnostic // Get the method declaration from the target diagnostic
if (root!.FindNode(diagnosticSpan) is MethodDeclarationSyntax { Identifier.Text: string identifierName } methodDeclaration && if (root!.FindNode(diagnosticSpan) is MethodDeclarationSyntax methodDeclaration)
identifierName == methodName)
{ {
// Register the code fix to update the return type to be Task instead // Register the code fix to update the return type to be Task instead
context.RegisterCodeFix( context.RegisterCodeFix(
......
...@@ -17,11 +17,6 @@ namespace CommunityToolkit.Mvvm.SourceGenerators; ...@@ -17,11 +17,6 @@ namespace CommunityToolkit.Mvvm.SourceGenerators;
[DiagnosticAnalyzer(LanguageNames.CSharp)] [DiagnosticAnalyzer(LanguageNames.CSharp)]
public sealed class AsyncVoidReturningRelayCommandMethodAnalyzer : DiagnosticAnalyzer public sealed class AsyncVoidReturningRelayCommandMethodAnalyzer : DiagnosticAnalyzer
{ {
/// <summary>
/// The key for the name of the target method to update.
/// </summary>
internal const string MethodNameKey = "MethodName";
/// <inheritdoc/> /// <inheritdoc/>
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(AsyncVoidReturningRelayCommandMethod); public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics { get; } = ImmutableArray.Create(AsyncVoidReturningRelayCommandMethod);
...@@ -57,7 +52,6 @@ public override void Initialize(AnalysisContext context) ...@@ -57,7 +52,6 @@ public override void Initialize(AnalysisContext context)
context.ReportDiagnostic(Diagnostic.Create( context.ReportDiagnostic(Diagnostic.Create(
AsyncVoidReturningRelayCommandMethod, AsyncVoidReturningRelayCommandMethod,
context.Symbol.Locations.FirstOrDefault(), context.Symbol.Locations.FirstOrDefault(),
ImmutableDictionary.Create<string, string?>().Add(MethodNameKey, methodSymbol.Name),
context.Symbol)); context.Symbol));
}, SymbolKind.Method); }, SymbolKind.Method);
}); });
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册