-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[GH-76] - callInfo analysis for When like method - vb
- Loading branch information
Showing
4 changed files
with
1,237 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/CallInfoDoAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...isualBasic/DiagnosticAnalyzersTests/CallInfoDoAnalyzerTests/CallInfoDoAnalyzerVerifier.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
Oops, something went wrong.