|
| 1 | +using System.Collections.Immutable; |
| 2 | +using Microsoft.CodeAnalysis; |
| 3 | +using Microsoft.CodeAnalysis.CSharp; |
| 4 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 5 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 6 | + |
| 7 | +namespace ExpressiveSharp.EntityFrameworkCore.CodeFixers; |
| 8 | + |
| 9 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 10 | +public sealed class WindowFunctionLiteralArgsAnalyzer : DiagnosticAnalyzer |
| 11 | +{ |
| 12 | + private const string WindowFunctionType = |
| 13 | + "ExpressiveSharp.EntityFrameworkCore.RelationalExtensions.WindowFunctions.WindowFunction"; |
| 14 | + |
| 15 | + public static readonly DiagnosticDescriptor NtileRequiresPositiveBuckets = new( |
| 16 | + id: "EXP0036", |
| 17 | + title: "WindowFunction.Ntile requires a positive bucket count", |
| 18 | + messageFormat: "WindowFunction.Ntile requires a positive bucket count; literal value {0} produces invalid SQL", |
| 19 | + category: "Usage", |
| 20 | + defaultSeverity: DiagnosticSeverity.Warning, |
| 21 | + isEnabledByDefault: true, |
| 22 | + description: "NTILE(n) divides ordered rows into n buckets. SQL requires n >= 1; non-positive values raise a database error at execution time."); |
| 23 | + |
| 24 | + public static readonly DiagnosticDescriptor NavigationOffsetMustBeNonNegative = new( |
| 25 | + id: "EXP0037", |
| 26 | + title: "WindowFunction.Lag/Lead offset must be non-negative", |
| 27 | + messageFormat: "WindowFunction.{0} offset must be non-negative; literal value {1} is rejected during EF translation", |
| 28 | + category: "Usage", |
| 29 | + defaultSeverity: DiagnosticSeverity.Warning, |
| 30 | + isEnabledByDefault: true, |
| 31 | + description: "LAG and LEAD offsets count rows backward or forward from the current row. SQL requires the offset to be >= 0; negative literals are rejected during EF translation."); |
| 32 | + |
| 33 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| 34 | + ImmutableArray.Create(NtileRequiresPositiveBuckets, NavigationOffsetMustBeNonNegative); |
| 35 | + |
| 36 | + public override void Initialize(AnalysisContext context) |
| 37 | + { |
| 38 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 39 | + context.EnableConcurrentExecution(); |
| 40 | + context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression); |
| 41 | + } |
| 42 | + |
| 43 | + private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context) |
| 44 | + { |
| 45 | + var invocation = (InvocationExpressionSyntax)context.Node; |
| 46 | + |
| 47 | + if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess) |
| 48 | + return; |
| 49 | + |
| 50 | + var name = memberAccess.Name.Identifier.Text; |
| 51 | + if (name != "Ntile" && name != "Lag" && name != "Lead") |
| 52 | + return; |
| 53 | + |
| 54 | + var symbolInfo = context.SemanticModel.GetSymbolInfo(invocation, context.CancellationToken); |
| 55 | + if (symbolInfo.Symbol is not IMethodSymbol method) |
| 56 | + return; |
| 57 | + |
| 58 | + if (method.ContainingType?.ToDisplayString() != WindowFunctionType) |
| 59 | + return; |
| 60 | + |
| 61 | + var args = invocation.ArgumentList.Arguments; |
| 62 | + if (args.Count == 0) |
| 63 | + return; |
| 64 | + |
| 65 | + switch (name) |
| 66 | + { |
| 67 | + case "Ntile": |
| 68 | + if (TryGetIntLiteral(context.SemanticModel, args[0].Expression, context.CancellationToken, out var buckets) && buckets <= 0) |
| 69 | + { |
| 70 | + context.ReportDiagnostic(Diagnostic.Create( |
| 71 | + NtileRequiresPositiveBuckets, |
| 72 | + args[0].GetLocation(), |
| 73 | + buckets)); |
| 74 | + } |
| 75 | + break; |
| 76 | + |
| 77 | + case "Lag": |
| 78 | + case "Lead": |
| 79 | + if (args.Count >= 2 && |
| 80 | + TryGetIntLiteral(context.SemanticModel, args[1].Expression, context.CancellationToken, out var offset) && |
| 81 | + offset < 0) |
| 82 | + { |
| 83 | + context.ReportDiagnostic(Diagnostic.Create( |
| 84 | + NavigationOffsetMustBeNonNegative, |
| 85 | + args[1].GetLocation(), |
| 86 | + name, |
| 87 | + offset)); |
| 88 | + } |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static bool TryGetIntLiteral( |
| 94 | + SemanticModel semanticModel, |
| 95 | + ExpressionSyntax expression, |
| 96 | + System.Threading.CancellationToken cancellationToken, |
| 97 | + out int value) |
| 98 | + { |
| 99 | + var constant = semanticModel.GetConstantValue(expression, cancellationToken); |
| 100 | + if (constant.HasValue && constant.Value is int i) |
| 101 | + { |
| 102 | + value = i; |
| 103 | + return true; |
| 104 | + } |
| 105 | + |
| 106 | + value = 0; |
| 107 | + return false; |
| 108 | + } |
| 109 | +} |
0 commit comments