From 7133a52346bbced8852f83739264d438a27860ff Mon Sep 17 00:00:00 2001 From: pshao25 <97225342+pshao25@users.noreply.github.com> Date: Tue, 15 Aug 2023 17:34:32 +0800 Subject: [PATCH] Update --- .../ClientMethodsAnalyzer.cs | 49 ++++++++++++------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/ClientMethodsAnalyzer.cs b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/ClientMethodsAnalyzer.cs index 97694829f49..7a974a2fb09 100644 --- a/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/ClientMethodsAnalyzer.cs +++ b/src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/ClientMethodsAnalyzer.cs @@ -3,6 +3,7 @@ using System.Collections.Immutable; using System.Linq; +using System.Reflection; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Diagnostics; @@ -28,23 +29,23 @@ public class ClientMethodsAnalyzer : ClientAnalyzerBase Descriptors.AZC0015 }); - private static void CheckCancellationTokenOrRequestContext(ISymbolAnalysisContext context, IMethodSymbol member) + private static bool IsRequestContext(IParameterSymbol parameterSymbol) { - static bool IsRequestContext(IParameterSymbol parameterSymbol) - { - return parameterSymbol.Name == "context" && parameterSymbol.Type.Name == "RequestContext"; - } + return parameterSymbol.Name == "context" && parameterSymbol.Type.Name == "RequestContext"; + } - static bool IsCancellationToken(IParameterSymbol parameterSymbol) - { - return parameterSymbol.Name == "cancellationToken" && parameterSymbol.Type.Name == "CancellationToken"; - } + private static bool IsCancellationToken(IParameterSymbol parameterSymbol) + { + return parameterSymbol.Name == "cancellationToken" && parameterSymbol.Type.Name == "CancellationToken"; + } - static bool IsCancellationOrRequestContext(IParameterSymbol parameterSymbol) - { - return IsCancellationToken(parameterSymbol) || IsRequestContext(parameterSymbol); - } + private static bool IsCancellationOrRequestContext(IParameterSymbol parameterSymbol) + { + return IsCancellationToken(parameterSymbol) || IsRequestContext(parameterSymbol); + } + private static void CheckCancellationTokenOrRequestContext(ISymbolAnalysisContext context, IMethodSymbol member) + { var lastArgument = member.Parameters.LastOrDefault(); var isCancellationOrRequestContext = lastArgument != null && IsCancellationOrRequestContext(lastArgument); @@ -136,6 +137,23 @@ private static bool IsClientMethodReturnType(ISymbolAnalysisContext context, IMe return false; } + private static IMethodSymbol FindSyncPeer(INamedTypeSymbol type, IMethodSymbol method) + { + IMethodSymbol syncMember = null; + var syncMemberName = method.Name.Substring(0, method.Name.Length - AsyncSuffix.Length); + syncMember = FindMethod(type.GetMembers(syncMemberName).OfType(), method.TypeParameters, method.Parameters); + if (syncMember == null) + { + var lastArgument = method.Parameters.LastOrDefault(); + if (lastArgument != null && IsCancellationToken(lastArgument)) + { + syncMember = FindMethod(type.GetMembers(syncMemberName).OfType(), method.TypeParameters, method.Parameters.RemoveAt(method.Parameters.Length - 1), p => IsCancellationToken(p)); + } + } + + return syncMember; + } + public override void AnalyzeCore(ISymbolAnalysisContext context) { INamedTypeSymbol type = (INamedTypeSymbol)context.Symbol; @@ -151,10 +169,7 @@ public override void AnalyzeCore(ISymbolAnalysisContext context) { CheckClientMethod(context, methodSymbol); - var syncMemberName = member.Name.Substring(0, member.Name.Length - AsyncSuffix.Length); - - var syncMember = FindMethod(type.GetMembers(syncMemberName).OfType(), methodSymbol.TypeParameters, methodSymbol.Parameters); - + var syncMember = FindSyncPeer(type, methodSymbol); if (syncMember == null) { context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0004, member.Locations.First()), member);