Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
pshao25 committed Aug 15, 2023
1 parent d831c73 commit 7133a52
Showing 1 changed file with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Immutable;
using System.Linq;
using System.Reflection;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;

Expand All @@ -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);

Expand Down Expand Up @@ -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<IMethodSymbol>(), method.TypeParameters, method.Parameters);
if (syncMember == null)
{
var lastArgument = method.Parameters.LastOrDefault();
if (lastArgument != null && IsCancellationToken(lastArgument))
{
syncMember = FindMethod(type.GetMembers(syncMemberName).OfType<IMethodSymbol>(), 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;
Expand All @@ -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<IMethodSymbol>(), methodSymbol.TypeParameters, methodSymbol.Parameters);

var syncMember = FindSyncPeer(type, methodSymbol);
if (syncMember == null)
{
context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0004, member.Locations.First()), member);
Expand Down

0 comments on commit 7133a52

Please sign in to comment.