-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
32 changed files
with
10,725 additions
and
5 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/NSubstitute.Analyzers.CSharp/DiagnosticAnalyzers/NonVirtualSetupWhenAnalyzer.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,73 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
|
||
namespace NSubstitute.Analyzers.CSharp.DiagnosticAnalyzers | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
internal class NonVirtualSetupWhenAnalyzer : AbstractNonVirtualWhenAnalyzer<SyntaxKind, InvocationExpressionSyntax> | ||
{ | ||
public NonVirtualSetupWhenAnalyzer() | ||
: base(new DiagnosticDescriptorsProvider()) | ||
{ | ||
} | ||
|
||
protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression; | ||
|
||
protected override IEnumerable<SyntaxNode> GetExpressionsForAnalysys(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, IMethodSymbol methodSymbol, InvocationExpressionSyntax invocationExpressionSyntax) | ||
{ | ||
var argumentListArguments = invocationExpressionSyntax.ArgumentList.Arguments; | ||
var argumentSyntax = methodSymbol.MethodKind == MethodKind.ReducedExtension ? argumentListArguments.First() : argumentListArguments.Skip(1).First(); | ||
return GetExpressionsForAnalysys(syntaxNodeAnalysisContext, argumentSyntax.Expression); | ||
} | ||
|
||
private IEnumerable<SyntaxNode> GetExpressionsForAnalysys(SyntaxNodeAnalysisContext syntaxNodeContext, SyntaxNode argumentSyntax) | ||
{ | ||
SyntaxNode body = null; | ||
switch (argumentSyntax) | ||
{ | ||
case SimpleLambdaExpressionSyntax simpleLambdaExpressionSyntax: | ||
body = simpleLambdaExpressionSyntax.Body; | ||
break; | ||
case AnonymousFunctionExpressionSyntax anonymousFunctionExpressionSyntax: | ||
body = anonymousFunctionExpressionSyntax.Body; | ||
break; | ||
case LocalFunctionStatementSyntax localFunctionStatementSyntax: | ||
body = (SyntaxNode)localFunctionStatementSyntax.Body ?? localFunctionStatementSyntax.ExpressionBody; | ||
break; | ||
case MethodDeclarationSyntax methodDeclarationSyntax: | ||
body = (SyntaxNode)methodDeclarationSyntax.Body ?? methodDeclarationSyntax.ExpressionBody; | ||
break; | ||
case IdentifierNameSyntax identifierNameSyntax: | ||
var symbol = syntaxNodeContext.SemanticModel.GetSymbolInfo(identifierNameSyntax); | ||
if (symbol.Symbol != null && symbol.Symbol.Locations.Any()) | ||
{ | ||
var location = symbol.Symbol.Locations.First(); | ||
var syntaxNode = location.SourceTree.GetRoot().FindNode(location.SourceSpan); | ||
|
||
foreach (var expressionsForAnalysy in GetExpressionsForAnalysys(syntaxNodeContext, syntaxNode)) | ||
{ | ||
yield return expressionsForAnalysy; | ||
} | ||
} | ||
|
||
break; | ||
} | ||
|
||
if (body == null) | ||
{ | ||
yield break; | ||
} | ||
|
||
foreach (var invocationExpressionSyntax in body.DescendantNodes().Where(node => node.IsKind(SyntaxKind.SimpleMemberAccessExpression) || | ||
node.IsKind(SyntaxKind.ElementAccessExpression))) | ||
{ | ||
yield return invocationExpressionSyntax; | ||
} | ||
} | ||
} | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
86 changes: 86 additions & 0 deletions
86
src/NSubstitute.Analyzers.Shared/DiagnosticAnalyzers/AbstractNonVirtualWhenAnalyzer.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,86 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NSubstitute.Analyzers.Shared.Extensions; | ||
|
||
namespace NSubstitute.Analyzers.Shared.DiagnosticAnalyzers | ||
{ | ||
internal abstract class AbstractNonVirtualWhenAnalyzer<TSyntaxKind, TInvocationExpressionSyntax> : AbstractDiagnosticAnalyzer | ||
where TInvocationExpressionSyntax : SyntaxNode | ||
where TSyntaxKind : struct | ||
{ | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(DiagnosticDescriptorsProvider.NonVirtualWhenSetupSpecification); | ||
|
||
private static readonly ImmutableHashSet<string> MethodNames = ImmutableHashSet.Create( | ||
MetadataNames.NSubstituteWhenMethod, | ||
MetadataNames.NSubstituteWhenForAnyArgsMethod); | ||
|
||
protected abstract TSyntaxKind InvocationExpressionKind { get; } | ||
|
||
protected AbstractNonVirtualWhenAnalyzer(IDiagnosticDescriptorsProvider diagnosticDescriptorsProvider) | ||
: base(diagnosticDescriptorsProvider) | ||
{ | ||
} | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
context.RegisterSyntaxNodeAction(AnalyzeInvocation, InvocationExpressionKind); | ||
} | ||
|
||
protected abstract IEnumerable<SyntaxNode> GetExpressionsForAnalysys(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, IMethodSymbol methodSymbol, TInvocationExpressionSyntax invocationExpressionSyntax); | ||
|
||
private void AnalyzeInvocation(SyntaxNodeAnalysisContext syntaxNodeContext) | ||
{ | ||
var invocationExpression = (TInvocationExpressionSyntax)syntaxNodeContext.Node; | ||
var methodSymbolInfo = syntaxNodeContext.SemanticModel.GetSymbolInfo(invocationExpression); | ||
|
||
if (methodSymbolInfo.Symbol?.Kind != SymbolKind.Method) | ||
{ | ||
return; | ||
} | ||
|
||
var methodSymbol = (IMethodSymbol)methodSymbolInfo.Symbol; | ||
if (methodSymbol == null) | ||
{ | ||
return; | ||
} | ||
|
||
if (IsWhenLikeMethod(syntaxNodeContext, invocationExpression, methodSymbol.Name) == false) | ||
{ | ||
return; | ||
} | ||
|
||
var expressionsForAnalysys = GetExpressionsForAnalysys(syntaxNodeContext, methodSymbol, invocationExpression); | ||
var typeSymbol = methodSymbol.TypeArguments.FirstOrDefault() ?? methodSymbol.ReceiverType; | ||
foreach (var analysedSyntax in expressionsForAnalysys) | ||
{ | ||
var symbolInfo = syntaxNodeContext.SemanticModel.GetSymbolInfo(analysedSyntax); | ||
if (symbolInfo.Symbol != null && symbolInfo.Symbol.ContainingType == typeSymbol && symbolInfo.Symbol.CanBeSetuped() == false) | ||
{ | ||
var diagnostic = Diagnostic.Create( | ||
DiagnosticDescriptorsProvider.NonVirtualWhenSetupSpecification, | ||
analysedSyntax.GetLocation(), | ||
symbolInfo.Symbol.Name); | ||
|
||
syntaxNodeContext.ReportDiagnostic(diagnostic); | ||
} | ||
} | ||
} | ||
|
||
private bool IsWhenLikeMethod(SyntaxNodeAnalysisContext syntaxNodeContext, SyntaxNode syntax, string memberName) | ||
{ | ||
if (MethodNames.Contains(memberName) == false) | ||
{ | ||
return false; | ||
} | ||
|
||
var symbol = syntaxNodeContext.SemanticModel.GetSymbolInfo(syntax); | ||
|
||
return symbol.Symbol?.ContainingAssembly?.Name.Equals(MetadataNames.NSubstituteAssemblyName, StringComparison.OrdinalIgnoreCase) == true && | ||
symbol.Symbol?.ContainingType?.ToString().Equals(MetadataNames.NSubstituteSubstituteExtensionsFullTypeName, StringComparison.OrdinalIgnoreCase) == true; | ||
} | ||
} | ||
} |
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
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
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
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
114 changes: 114 additions & 0 deletions
114
src/NSubstitute.Analyzers.VisualBasic/DiagnosticAnalyzers/NonVirtualSetupWhenAnalyzer.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,114 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Microsoft.CodeAnalysis.VisualBasic; | ||
using Microsoft.CodeAnalysis.VisualBasic.Syntax; | ||
using NSubstitute.Analyzers.Shared.DiagnosticAnalyzers; | ||
|
||
namespace NSubstitute.Analyzers.VisualBasic.DiagnosticAnalyzers | ||
{ | ||
[DiagnosticAnalyzer(LanguageNames.VisualBasic)] | ||
internal class NonVirtualSetupWhenAnalyzer : AbstractNonVirtualWhenAnalyzer<SyntaxKind, InvocationExpressionSyntax> | ||
{ | ||
public NonVirtualSetupWhenAnalyzer() | ||
: base(new DiagnosticDescriptorsProvider()) | ||
{ | ||
} | ||
|
||
protected override SyntaxKind InvocationExpressionKind { get; } = SyntaxKind.InvocationExpression; | ||
|
||
protected override IEnumerable<SyntaxNode> GetExpressionsForAnalysys(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext, IMethodSymbol methodSymbol, InvocationExpressionSyntax invocationExpressionSyntax) | ||
{ | ||
var argumentListArguments = invocationExpressionSyntax.ArgumentList.Arguments; | ||
var argumentSyntax = methodSymbol.MethodKind == MethodKind.ReducedExtension ? argumentListArguments.First() : argumentListArguments.Skip(1).First(); | ||
return GetExpressionsForAnalysys(syntaxNodeAnalysisContext, argumentSyntax.GetExpression()); | ||
} | ||
|
||
private IEnumerable<SyntaxNode> GetExpressionsForAnalysys(SyntaxNodeAnalysisContext syntaxNodeContext, SyntaxNode syntax) | ||
{ | ||
SyntaxNode body = null; | ||
switch (syntax) | ||
{ | ||
case SingleLineLambdaExpressionSyntax _: | ||
case ExpressionStatementSyntax _: | ||
case LocalDeclarationStatementSyntax _: | ||
case AssignmentStatementSyntax _: | ||
body = syntax; | ||
break; | ||
case MultiLineLambdaExpressionSyntax simpleLambdaExpressionSyntax: | ||
foreach (var syntaxNode in IterateStatements(simpleLambdaExpressionSyntax.Statements)) | ||
{ | ||
yield return syntaxNode; | ||
} | ||
|
||
break; | ||
case MethodBlockSyntax methodBlockSyntax: | ||
foreach (var syntaxNode in IterateStatements(methodBlockSyntax.Statements)) | ||
{ | ||
yield return syntaxNode; | ||
} | ||
|
||
break; | ||
case UnaryExpressionSyntax unaryExpressionSyntax: | ||
foreach (var syntaxNode in GetExpressionsForAnalysys(syntaxNodeContext, unaryExpressionSyntax.Operand)) | ||
{ | ||
yield return syntaxNode; | ||
} | ||
|
||
break; | ||
case IdentifierNameSyntax identifierNameSyntax: | ||
var symbol = syntaxNodeContext.SemanticModel.GetSymbolInfo(identifierNameSyntax); | ||
if (symbol.Symbol != null && symbol.Symbol.Locations.Any()) | ||
{ | ||
var location = symbol.Symbol.Locations.First(); | ||
var syntaxNode = location.SourceTree.GetRoot().FindNode(location.SourceSpan); | ||
|
||
SyntaxNode innerNode = null; | ||
if (syntaxNode is MethodStatementSyntax methodStatementSyntax) | ||
{ | ||
innerNode = methodStatementSyntax.Parent; | ||
} | ||
|
||
innerNode = innerNode ?? syntaxNode; | ||
foreach (var expressionsForAnalysy in GetExpressionsForAnalysys(syntaxNodeContext, innerNode)) | ||
{ | ||
yield return expressionsForAnalysy; | ||
} | ||
} | ||
|
||
break; | ||
} | ||
|
||
if (body == null) | ||
{ | ||
yield break; | ||
} | ||
|
||
var memberAccessExpressions = body.DescendantNodes().Where(node => node.IsKind(SyntaxKind.SimpleMemberAccessExpression)); | ||
var invocationExpressions = body.DescendantNodes().Where(node => node.IsKind(SyntaxKind.InvocationExpression)); | ||
|
||
// rather ugly but prevents reporting two times the same thing | ||
// as VB syntax is based on statements, you can't access body of method directly | ||
if (invocationExpressions.Any()) | ||
{ | ||
foreach (var invocationExpression in invocationExpressions) | ||
{ | ||
yield return invocationExpression; | ||
} | ||
} | ||
else if (memberAccessExpressions.Any()) | ||
{ | ||
foreach (var memberAccessExpression in memberAccessExpressions) | ||
{ | ||
yield return memberAccessExpression; | ||
} | ||
} | ||
|
||
IEnumerable<SyntaxNode> IterateStatements(IEnumerable<StatementSyntax> statements) | ||
{ | ||
return statements.SelectMany(statement => GetExpressionsForAnalysys(syntaxNodeContext, statement)); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.