Add generic projection hooks with AsLeftJoin and AsProjectable support#311
Draft
Add generic projection hooks with AsLeftJoin and AsProjectable support#311
Conversation
…ting Co-authored-by: arika0093 <4524647+arika0093@users.noreply.github.com>
Co-authored-by: arika0093 <4524647+arika0093@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add AsLeftJoin and AsProjectable methods for EFCore
Add generic projection hooks with AsLeftJoin and AsProjectable support
Mar 18, 2026
There was a problem hiding this comment.
Pull request overview
Adds a generic “projection hook” mechanism to Linqraft’s projection pipeline, enabling explicit, user-authored markers in anonymous projections that the generator rewrites (built-in: AsLeftJoin() and AsProjectable()), plus analyzer enforcement and test coverage.
Changes:
- Introduces configurable projection hook definitions (defaulting to
AsLeftJoin+AsProjectable) and emits corresponding no-op hook extension declarations. - Extends the projection expression emitter to rewrite
AsLeftJoininto null-guarded access and to inline computed members viaAsProjectable. - Adds analyzer rule
LQRE003to block projection hook usage outside generated projection contexts, with generator/analyzer/EFCore tests.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| tests/Linqraft.Tests.SG/Program.cs | Adds SG coverage for default/custom hook declaration emission. |
| tests/Linqraft.Tests.EFCore/EfCoreModels.cs | Adds a computed query-shaped property for AsProjectable coverage. |
| tests/Linqraft.Tests.EFCore/EfCoreBasicProjectionTests.cs | Adds SQLite EFCore tests for AsLeftJoin and AsProjectable behavior. |
| tests/Linqraft.Tests.Analyzer/AnalyzerSmokeTests.cs | Adds LQRE003 smoke tests for hook usage inside/outside projections. |
| src/Linqraft.Core/SourceGenerator/ProjectionSupportExtensionClassGenerator.cs | Emits generated no-op hook extension methods based on ProjectionHooks. |
| src/Linqraft.Core/SourceGenerator/ProjectionExpressionEmitter.cs | Implements hook rewriting: left-join null-guards + projectable inlining. |
| src/Linqraft.Core/Configuration/LinqraftGeneratorOptionsCore.cs | Adds hook definitions (LinqraftProjectionHookDefinition, Kind) and defaults. |
| src/Linqraft.Analyzer/LinqraftCompositeAnalyzer.cs | Adds projection-hook usage enforcement diagnostic reporting. |
| src/Linqraft.Analyzer/DiagnosticDescriptors.cs | Registers new analyzer error descriptor LQRE003. |
| src/Linqraft.Analyzer/AnalyzerHelpers.cs | Adds helper detection for hook invocations and hook-context detection. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+952
to
+971
| private bool TryEmitProjectableHook( | ||
| InvocationExpressionSyntax invocation, | ||
| string? overrideReceiver, | ||
| out string rewritten | ||
| ) | ||
| { | ||
| rewritten = string.Empty; | ||
| if (!IsProjectionHookInvocation(invocation, LinqraftProjectionHookKind.Projectable)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| if (!TryExpandProjectableInvocation(invocation, overrideReceiver, out var expanded)) | ||
| { | ||
| return false; | ||
| } | ||
|
|
||
| rewritten = Emit(expanded); | ||
| return true; | ||
| } |
Comment on lines
+34
to
+56
| public static bool IsProjectionExprInvocation(InvocationExpressionSyntax invocation) | ||
| { | ||
| return GetInvocationName(invocation.Expression) is "SelectExpr" or "SelectManyExpr" or "GroupByExpr"; | ||
| } | ||
|
|
||
| public static bool IsProjectionHookInvocation(InvocationExpressionSyntax invocation) | ||
| { | ||
| return ProjectionHookNames.Contains(GetInvocationName(invocation.Expression)); | ||
| } | ||
|
|
||
| public static bool IsInsideProjectionHookContext(SyntaxNode node) | ||
| { | ||
| return node.AncestorsAndSelf() | ||
| .OfType<InvocationExpressionSyntax>() | ||
| .Any(invocation => | ||
| IsProjectionExprInvocation(invocation) | ||
| || string.Equals( | ||
| GetInvocationName(invocation.Expression), | ||
| "Generate", | ||
| StringComparison.Ordinal | ||
| ) | ||
| ); | ||
| } |
Comment on lines
+116
to
+135
| private static void AnalyzeProjectionHookUsage( | ||
| SyntaxNodeAnalysisContext context, | ||
| InvocationExpressionSyntax invocation | ||
| ) | ||
| { | ||
| if ( | ||
| !AnalyzerHelpers.IsProjectionHookInvocation(invocation) | ||
| || AnalyzerHelpers.IsInsideProjectionHookContext(invocation) | ||
| ) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| context.ReportDiagnostic( | ||
| Diagnostic.Create( | ||
| DiagnosticDescriptors.ProjectionHookUsage, | ||
| invocation.GetLocation(), | ||
| AnalyzerHelpers.GetInvocationName(invocation.Expression) | ||
| ) | ||
| ); |
Comment on lines
+41
to
+49
| foreach ( | ||
| var hook in generatorOptions | ||
| .ProjectionHooks.GroupBy( | ||
| hook => hook.MethodName, | ||
| global::System.StringComparer.Ordinal | ||
| ) | ||
| .Select(group => group.First()) | ||
| ) | ||
| { |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change adds a reusable projection-hook mechanism for
*Exprgeneration so user-authored anonymous projections can opt into custom rewrites. It introduces built-in support forAsLeftJoin()andAsProjectable(), while enforcing that these hooks are only used inside generated projection contexts.Projection hook infrastructure
AsLeftJoin()rewriteAsLeftJoin()hook for nullable navigation access inside generated projections.x.Child.AsLeftJoin().Foointo an explicit null-guarded form so the generated projection preserves outer rows instead of collapsing into inner-join-like behavior.AsProjectable()rewriteAsProjectable()hook for inlining source-defined computed members into generated projections.*Exprbehaves like the query body was written inline.Analyzer enforcement
SelectExpr/SelectManyExpr/GroupByExpr/Generate.Coverage
Example usage:
This enables the intended flow:
user anonymous projection -> explicit hook -> generated *Expr rewrite📱 Kick off Copilot coding agent tasks wherever you are with GitHub Mobile, available on iOS and Android.