Skip to content

Commit

Permalink
[GH-76] - callInfo analysis for When like method - vb
Browse files Browse the repository at this point in the history
  • Loading branch information
tpodolak committed Mar 30, 2019
1 parent 4055b6a commit 7d508b5
Show file tree
Hide file tree
Showing 4 changed files with 1,237 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using System;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.VisualBasic.Syntax;
using NSubstitute.Analyzers.Shared.Extensions;
using NSubstitute.Analyzers.VisualBasic.Extensions;

namespace NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers
{
internal class CallInfoDoAnalyzer : CallInfoAnalyzer
{
private readonly Lazy<WhenSubstituteCallFinder> _whenSubstituteCallFinderProxy = new Lazy<WhenSubstituteCallFinder>(() => new WhenSubstituteCallFinder());

private WhenSubstituteCallFinder WhenSubstituteCallFinder => _whenSubstituteCallFinderProxy.Value;

protected override bool SupportsCallInfo(SyntaxNodeAnalysisContext syntaxNodeContext, InvocationExpressionSyntax syntax, IMethodSymbol methodSymbol)
{
if (methodSymbol.Name != "Do")
{
return false;
}

var allArguments = GetArgumentExpressions(syntax);
return allArguments.Any(arg => syntaxNodeContext.SemanticModel.GetTypeInfo(arg).IsCallInfoDelegate(syntaxNodeContext.SemanticModel));
}

protected override SyntaxNode GetSubstituteCall(SyntaxNodeAnalysisContext syntaxNodeContext, IMethodSymbol methodSymbol, InvocationExpressionSyntax invocationExpressionSyntax)
{
var parentInvocationExpression = invocationExpressionSyntax.GetParentInvocationExpression();

if (parentInvocationExpression == null)
{
return null;
}

if (syntaxNodeContext.SemanticModel.GetSymbolInfo(parentInvocationExpression).Symbol is IMethodSymbol parentInvocationSymbol)
{
var argumentExpression = parentInvocationSymbol.MethodKind == MethodKind.ReducedExtension
? parentInvocationExpression.ArgumentList.Arguments.First().GetExpression()
: parentInvocationExpression.ArgumentList.Arguments.Skip(1).First().GetExpression();

return WhenSubstituteCallFinder.Find(syntaxNodeContext, argumentExpression).FirstOrDefault();
}

return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Microsoft.CodeAnalysis.Diagnostics;
using NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.CallInfoAnalyzerTests;
using NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers;

namespace NSubstitute.Analyzers.Tests.VisualBasic.DiagnosticAnalyzersTests.CallInfoDoAnalyzerTests
{
public abstract class CallInfoDoAnalyzerVerifier : CallInfoDiagnosticVerifier
{
protected override DiagnosticAnalyzer GetDiagnosticAnalyzer()
{
return new CallInfoDoAnalyzer();
}
}
}
Loading

0 comments on commit 7d508b5

Please sign in to comment.