|
| 1 | +using System.Collections.Immutable; |
| 2 | +using ExpressiveSharp.CodeFixers.Internal; |
| 3 | +using Microsoft.CodeAnalysis; |
| 4 | +using Microsoft.CodeAnalysis.CSharp; |
| 5 | +using Microsoft.CodeAnalysis.CSharp.Syntax; |
| 6 | +using Microsoft.CodeAnalysis.Diagnostics; |
| 7 | + |
| 8 | +namespace ExpressiveSharp.CodeFixers; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Reports EXP0036 at the *dropout call* itself: a method invocation whose receiver implements |
| 12 | +/// <c>IExpressiveQueryable<T></c> but whose return type is plain <c>IQueryable<T></c>. |
| 13 | +/// The chain stops being expressive at this call; downstream LINQ skips ExpressiveSharp rewriting. |
| 14 | +/// </summary> |
| 15 | +/// <remarks> |
| 16 | +/// Exemptions: |
| 17 | +/// <list type="bullet"> |
| 18 | +/// <item><description><c>AsQueryable()</c> — sanctioned explicit downcast.</description></item> |
| 19 | +/// <item><description>Methods marked <c>[NotExpressive]</c> — opt-out for intentional dropouts.</description></item> |
| 20 | +/// </list> |
| 21 | +/// </remarks> |
| 22 | +[DiagnosticAnalyzer(LanguageNames.CSharp)] |
| 23 | +public sealed class ExpressiveQueryableDropoutAnalyzer : DiagnosticAnalyzer |
| 24 | +{ |
| 25 | + public static readonly DiagnosticDescriptor ExpressiveQueryableDropout = new( |
| 26 | + id: "EXP0036", |
| 27 | + title: "IExpressiveQueryable<T> chain dropped to plain IQueryable<T>", |
| 28 | + messageFormat: "'{0}' returns IQueryable<T> from an IExpressiveQueryable<T> receiver, dropping the expressive chain. Downstream LINQ skips ExpressiveSharp rewriting and [Expressive] members may evaluate on the client. Add an IExpressiveQueryable<T>-typed overload of '{0}', wrap the result with .AsExpressive(), or mark the method [NotExpressive] if the dropout is intentional.", |
| 29 | + category: "Usage", |
| 30 | + defaultSeverity: DiagnosticSeverity.Info, |
| 31 | + isEnabledByDefault: true, |
| 32 | + description: "Once an IExpressiveQueryable<T> chain is upcast back to plain IQueryable<T>, the ExpressiveSharp rewrite layer is no longer applied to subsequent calls. User-defined helpers that take and return IQueryable<T> are the most common cause; sibling overloads typed on IExpressiveQueryable<T> preserve the chain."); |
| 33 | + |
| 34 | + public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => |
| 35 | + ImmutableArray.Create(ExpressiveQueryableDropout); |
| 36 | + |
| 37 | + public override void Initialize(AnalysisContext context) |
| 38 | + { |
| 39 | + context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.None); |
| 40 | + context.EnableConcurrentExecution(); |
| 41 | + context.RegisterSyntaxNodeAction(AnalyzeInvocation, SyntaxKind.InvocationExpression); |
| 42 | + } |
| 43 | + |
| 44 | + private static void AnalyzeInvocation(SyntaxNodeAnalysisContext context) |
| 45 | + { |
| 46 | + var invocation = (InvocationExpressionSyntax)context.Node; |
| 47 | + |
| 48 | + if (invocation.Expression is not MemberAccessExpressionSyntax memberAccess) |
| 49 | + return; |
| 50 | + |
| 51 | + var calledName = memberAccess.Name.Identifier.Text; |
| 52 | + |
| 53 | + // Sanctioned explicit downcast — user knows exactly what they're doing. |
| 54 | + if (calledName == "AsQueryable") |
| 55 | + return; |
| 56 | + |
| 57 | + var symbolInfo = context.SemanticModel.GetSymbolInfo(invocation, context.CancellationToken); |
| 58 | + if (symbolInfo.Symbol is not IMethodSymbol method) |
| 59 | + return; |
| 60 | + |
| 61 | + // Method-level opt-out. Honored same as EXP0027 honors it on referenced members. |
| 62 | + if (ExpressiveSymbolHelpers.HasNotExpressiveAttribute(method)) |
| 63 | + return; |
| 64 | + |
| 65 | + var receiverType = context.SemanticModel.GetTypeInfo( |
| 66 | + memberAccess.Expression, context.CancellationToken).Type; |
| 67 | + if (receiverType is null) |
| 68 | + return; |
| 69 | + if (!ExpressiveSymbolHelpers.IsOrImplementsExpressiveQueryable(receiverType)) |
| 70 | + return; |
| 71 | + |
| 72 | + var resultType = context.SemanticModel.GetTypeInfo(invocation, context.CancellationToken).Type; |
| 73 | + if (resultType is null) |
| 74 | + return; |
| 75 | + |
| 76 | + // Terminating calls (.ToList, .Count, scalars, void) don't continue the chain — no dropout. |
| 77 | + if (!ImplementsIQueryable(resultType)) |
| 78 | + return; |
| 79 | + |
| 80 | + // Still expressive — chain preserved, no dropout. |
| 81 | + if (ExpressiveSymbolHelpers.IsOrImplementsExpressiveQueryable(resultType)) |
| 82 | + return; |
| 83 | + |
| 84 | + context.ReportDiagnostic(Diagnostic.Create( |
| 85 | + ExpressiveQueryableDropout, |
| 86 | + memberAccess.Name.GetLocation(), |
| 87 | + properties: null, |
| 88 | + calledName)); |
| 89 | + } |
| 90 | + |
| 91 | + private static bool ImplementsIQueryable(ITypeSymbol type) |
| 92 | + { |
| 93 | + if (IsIQueryable(type)) |
| 94 | + return true; |
| 95 | + |
| 96 | + foreach (var iface in type.AllInterfaces) |
| 97 | + { |
| 98 | + if (IsIQueryable(iface)) |
| 99 | + return true; |
| 100 | + } |
| 101 | + |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + private static bool IsIQueryable(ITypeSymbol type) => |
| 106 | + type.Name == "IQueryable" && |
| 107 | + type.ContainingNamespace?.ToDisplayString() == "System.Linq"; |
| 108 | +} |
0 commit comments