|
| 1 | +using System.Collections.Immutable; |
| 2 | +using System.Linq; |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | +using Microsoft.CodeAnalysis.CSharp; |
| 6 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 7 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 8 | + |
| 9 | +using Sql.Analyzer.Extensions; |
| 10 | + |
| 11 | +namespace Sql.Analyzer |
| 12 | +{ |
| 13 | + [DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 14 | + public class DapperQueryMisuseAnalyzer : DiagnosticAnalyzer |
| 15 | + { |
| 16 | + public const string DiagnosticId = "SQL003"; |
| 17 | + |
| 18 | + public const string MessageFormat = "Consider using '{0}' method here"; |
| 19 | + |
| 20 | + private const string Category = "API Guidance"; |
| 21 | + |
| 22 | + private static readonly string Description = "The 'Query' method is designed to select a collection. For a single entity, there are most efficient alternatives like 'QueryFirst' or 'QuerySingle'."; |
| 23 | + |
| 24 | + private static readonly string Title = "Using 'Query' method is not optimal here"; |
| 25 | + |
| 26 | + private static readonly DiagnosticDescriptor Rule = new DiagnosticDescriptor( |
| 27 | + DiagnosticId, |
| 28 | + Title, |
| 29 | + MessageFormat, |
| 30 | + Category, |
| 31 | + DiagnosticSeverity.Warning, |
| 32 | + true, |
| 33 | + Description, |
| 34 | + "https://github.com/StackExchange/Dapper#performance"); |
| 35 | + |
| 36 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(Rule); |
| 37 | + |
| 38 | + public override void Initialize(AnalysisContext context) |
| 39 | + { |
| 40 | + context.RegisterSyntaxNodeAction(AnalyzeInvocationExpression, SyntaxKind.InvocationExpression); |
| 41 | + } |
| 42 | + |
| 43 | + private void AnalyzeInvocationExpression(SyntaxNodeAnalysisContext context) |
| 44 | + { |
| 45 | + var invocationExpressionSyntax = (InvocationExpressionSyntax)context.Node; |
| 46 | + |
| 47 | + var methodSymbol = context.SemanticModel.GetSymbolInfo(invocationExpressionSyntax).Symbol as IMethodSymbol; |
| 48 | + if (methodSymbol == null) |
| 49 | + { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + var dapperClass = context.SemanticModel.GetDapperSqlMapperSymbol(); |
| 54 | + const string QueryPrefix = "Query"; |
| 55 | + if (methodSymbol.ContainingType != dapperClass || !methodSymbol.Name.StartsWith(QueryPrefix)) |
| 56 | + { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + var firstInvocationExpression = invocationExpressionSyntax |
| 61 | + .Ancestors() |
| 62 | + .TakeWhile(n => !(n is StatementSyntax)) |
| 63 | + .OfType<InvocationExpressionSyntax>() |
| 64 | + .FirstOrDefault(); |
| 65 | + |
| 66 | + if (firstInvocationExpression == null || firstInvocationExpression.ArgumentList.Arguments.Count != 0) |
| 67 | + { |
| 68 | + return; |
| 69 | + } |
| 70 | + |
| 71 | + var linqExtensionMethodSymbol = context.SemanticModel.GetSymbolInfo(firstInvocationExpression).Symbol as IMethodSymbol; |
| 72 | + var linqEnumerableSymbol = context.SemanticModel.GetLinqEnumerableSymbol(); |
| 73 | + if (linqExtensionMethodSymbol == null || linqExtensionMethodSymbol.ContainingType != linqEnumerableSymbol) |
| 74 | + { |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + var alternativeMethod = QueryPrefix + linqExtensionMethodSymbol.Name; |
| 79 | + const string AsyncPostfix = "Async"; |
| 80 | + if (methodSymbol.Name.EndsWith(AsyncPostfix)) |
| 81 | + { |
| 82 | + alternativeMethod += AsyncPostfix; |
| 83 | + } |
| 84 | + |
| 85 | + var queryMethod = methodSymbol.ContainingType.GetMembers(alternativeMethod); |
| 86 | + if (queryMethod.Any()) |
| 87 | + { |
| 88 | + context.ReportDiagnostic(Diagnostic.Create(Rule, invocationExpressionSyntax.Expression.GetLocation(), alternativeMethod)); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments