From da6de24bc35c88dd10eaf890a56277a925942481 Mon Sep 17 00:00:00 2001 From: Richard Murillo Date: Mon, 26 Aug 2024 12:33:49 -0700 Subject: [PATCH] Refactor Analyzer to move bulk of parameter validation logic to new method --- ...ignatureShouldMatchMockedMethodAnalyzer.cs | 61 ++++++++++--------- 1 file changed, 33 insertions(+), 28 deletions(-) diff --git a/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs b/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs index d5695217..b269fb17 100644 --- a/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs +++ b/src/Moq.Analyzers/CallbackSignatureShouldMatchMockedMethodAnalyzer.cs @@ -63,43 +63,48 @@ private static void Analyze(SyntaxNodeAnalysisContext context) } else { - for (int argumentIndex = 0; argumentIndex < mockedMethodArguments.Count; argumentIndex++) - { - TypeSyntax? lambdaParameterTypeSyntax = lambdaParameters[argumentIndex].Type; - - // TODO: Don't know if continue or break is the right thing to do here - if (lambdaParameterTypeSyntax is null) continue; + ValidateParameters(context, mockedMethodArguments, lambdaParameters, callbackLambda); + } + } - TypeInfo lambdaParameterType = context.SemanticModel.GetTypeInfo(lambdaParameterTypeSyntax, context.CancellationToken); + private static void ValidateParameters( + SyntaxNodeAnalysisContext context, + SeparatedSyntaxList mockedMethodArguments, + SeparatedSyntaxList lambdaParameters, + ParenthesizedLambdaExpressionSyntax callbackLambda) + { + for (int argumentIndex = 0; argumentIndex < mockedMethodArguments.Count; argumentIndex++) + { + TypeSyntax? lambdaParameterTypeSyntax = lambdaParameters[argumentIndex].Type; - TypeInfo mockedMethodArgumentType = context.SemanticModel.GetTypeInfo(mockedMethodArguments[argumentIndex].Expression, context.CancellationToken); + // TODO: Don't know if continue or break is the right thing to do here + if (lambdaParameterTypeSyntax is null) continue; + TypeInfo lambdaParameterType = context.SemanticModel.GetTypeInfo(lambdaParameterTypeSyntax, context.CancellationToken); + TypeInfo mockedMethodArgumentType = context.SemanticModel.GetTypeInfo(mockedMethodArguments[argumentIndex].Expression, context.CancellationToken); - // Check if types are assignable rather than strictly equal - ITypeSymbol? lambdaParameterTypeSymbol = lambdaParameterType.Type; - ITypeSymbol? mockedMethodTypeSymbol = mockedMethodArgumentType.Type; + // Check if types are assignable rather than strictly equal + ITypeSymbol? lambdaParameterTypeSymbol = lambdaParameterType.Type; + ITypeSymbol? mockedMethodTypeSymbol = mockedMethodArgumentType.Type; - if (lambdaParameterTypeSymbol is not null && mockedMethodTypeSymbol is not null) + if (lambdaParameterTypeSymbol is not null && mockedMethodTypeSymbol is not null) + { + if (!context.SemanticModel.Compilation.ClassifyConversion(lambdaParameterTypeSymbol, mockedMethodTypeSymbol).IsImplicit) { - - if (!context.SemanticModel.Compilation.ClassifyConversion(lambdaParameterTypeSymbol, mockedMethodTypeSymbol).IsImplicit) - { - - Diagnostic diagnostic = lambdaParameters[argumentIndex].GetLocation().CreateDiagnostic(Rule); - context.ReportDiagnostic(diagnostic); - } + Diagnostic diagnostic = lambdaParameters[argumentIndex].GetLocation().CreateDiagnostic(Rule); + context.ReportDiagnostic(diagnostic); } - else + } + else + { + string? mockedMethodTypeName = mockedMethodArgumentType.ConvertedType?.ToString(); + string? lambdaParameterTypeName = lambdaParameterType.ConvertedType?.ToString(); + + if (!string.Equals(mockedMethodTypeName, lambdaParameterTypeName, StringComparison.Ordinal)) { - string? mockedMethodTypeName = mockedMethodArgumentType.ConvertedType?.ToString(); - string? lambdaParameterTypeName = lambdaParameterType.ConvertedType?.ToString(); - - if (!string.Equals(mockedMethodTypeName, lambdaParameterTypeName, StringComparison.Ordinal)) - { - Diagnostic diagnostic = callbackLambda.ParameterList.CreateDiagnostic(Rule); - context.ReportDiagnostic(diagnostic); - } + Diagnostic diagnostic = callbackLambda.ParameterList.CreateDiagnostic(Rule); + context.ReportDiagnostic(diagnostic); } } }