Refactor ExpressionSyntaxRewriter into focused partial class files#163
Refactor ExpressionSyntaxRewriter into focused partial class files#163
Conversation
…tiple files Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Refactors the Roslyn-based ExpressionSyntaxRewriter in the generator into multiple focused partial class files (with file-scoped namespaces) to reduce the size/complexity of the original monolithic implementation while preserving behavior.
Changes:
- Split switch-expression rewriting and pattern-to-expression conversion into a dedicated partial file.
- Split null-conditional (
?.) rewriting into a dedicated partial file. - Split enum method-call expansion into a dedicated partial file, leaving the core visitor logic in
ExpressionSyntaxRewriter.cs.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs | Converted to file-scoped namespace and reduced to core visitor logic + shared fields/ctor. |
| src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.SwitchExpressionRewrite.cs | Extracted switch expression rewriting + pattern conversion helpers. |
| src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.NullConditionalRewrite.cs | Extracted null-conditional rewriting (ConditionalAccessExpression + bindings). |
| src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.EnumMethodExpansion.cs | Extracted enum method expansion (TryExpandEnumMethodCall, helper call builder). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| { | ||
| public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node) | ||
| { | ||
| var targetExpression = (ExpressionSyntax)Visit(node.Expression); | ||
|
|
||
| _conditionalAccessExpressionsStack.Push(targetExpression); | ||
|
|
||
| if (_nullConditionalRewriteSupport == NullConditionalRewriteSupport.None) | ||
| { | ||
| var diagnostic = Diagnostic.Create(Diagnostics.NullConditionalRewriteUnsupported, node.GetLocation(), node); | ||
| _context.ReportDiagnostic(diagnostic); | ||
|
|
||
| // Return the original node, do not attempt further rewrites | ||
| return node; | ||
| } | ||
|
|
||
| else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Ignore) | ||
| { | ||
| // Ignore the conditional access and simply visit the WhenNotNull expression | ||
| return Visit(node.WhenNotNull); | ||
| } | ||
|
|
||
| else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Rewrite) | ||
| { | ||
| var typeInfo = _semanticModel.GetTypeInfo(node); | ||
|
|
||
| // Do not translate until we can resolve the target type | ||
| if (typeInfo.ConvertedType is not null) | ||
| { | ||
| // Translate null-conditional into a conditional expression, wrapped inside parenthesis | ||
| return SyntaxFactory.ParenthesizedExpression( | ||
| SyntaxFactory.ConditionalExpression( | ||
| SyntaxFactory.BinaryExpression( | ||
| SyntaxKind.NotEqualsExpression, | ||
| targetExpression.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | ||
| SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")) | ||
| ).WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | ||
| SyntaxFactory.ParenthesizedExpression( | ||
| (ExpressionSyntax)Visit(node.WhenNotNull) | ||
| ).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")).WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | ||
| SyntaxFactory.CastExpression( | ||
| SyntaxFactory.ParseName(typeInfo.ConvertedType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)), | ||
| SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression) | ||
| ).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")) | ||
| ).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia())); | ||
| } | ||
| } | ||
|
|
||
| return base.VisitConditionalAccessExpression(node); |
There was a problem hiding this comment.
VisitConditionalAccessExpression pushes onto _conditionalAccessExpressionsStack before checking _nullConditionalRewriteSupport. In the None branch it returns the original node without popping, leaving the stack unbalanced for the rest of the traversal and potentially growing without bound when multiple conditional accesses are present. Consider popping before returning (or restructuring with a try/finally so every push is matched with a pop).
| { | |
| public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node) | |
| { | |
| var targetExpression = (ExpressionSyntax)Visit(node.Expression); | |
| _conditionalAccessExpressionsStack.Push(targetExpression); | |
| if (_nullConditionalRewriteSupport == NullConditionalRewriteSupport.None) | |
| { | |
| var diagnostic = Diagnostic.Create(Diagnostics.NullConditionalRewriteUnsupported, node.GetLocation(), node); | |
| _context.ReportDiagnostic(diagnostic); | |
| // Return the original node, do not attempt further rewrites | |
| return node; | |
| } | |
| else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Ignore) | |
| { | |
| // Ignore the conditional access and simply visit the WhenNotNull expression | |
| return Visit(node.WhenNotNull); | |
| } | |
| else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Rewrite) | |
| { | |
| var typeInfo = _semanticModel.GetTypeInfo(node); | |
| // Do not translate until we can resolve the target type | |
| if (typeInfo.ConvertedType is not null) | |
| { | |
| // Translate null-conditional into a conditional expression, wrapped inside parenthesis | |
| return SyntaxFactory.ParenthesizedExpression( | |
| SyntaxFactory.ConditionalExpression( | |
| SyntaxFactory.BinaryExpression( | |
| SyntaxKind.NotEqualsExpression, | |
| targetExpression.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | |
| SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")) | |
| ).WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | |
| SyntaxFactory.ParenthesizedExpression( | |
| (ExpressionSyntax)Visit(node.WhenNotNull) | |
| ).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")).WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | |
| SyntaxFactory.CastExpression( | |
| SyntaxFactory.ParseName(typeInfo.ConvertedType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)), | |
| SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression) | |
| ).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")) | |
| ).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia())); | |
| } | |
| } | |
| return base.VisitConditionalAccessExpression(node); | |
| { | |
| public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node) | |
| { | |
| var targetExpression = (ExpressionSyntax)Visit(node.Expression); | |
| if (_nullConditionalRewriteSupport == NullConditionalRewriteSupport.None) | |
| { | |
| var diagnostic = Diagnostic.Create(Diagnostics.NullConditionalRewriteUnsupported, node.GetLocation(), node); | |
| _context.ReportDiagnostic(diagnostic); | |
| // Return the original node, do not attempt further rewrites | |
| return node; | |
| } | |
| _conditionalAccessExpressionsStack.Push(targetExpression); | |
| else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Ignore) | |
| { | |
| // Ignore the conditional access and simply visit the WhenNotNull expression | |
| return Visit(node.WhenNotNull); | |
| } | |
| else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Rewrite) | |
| { | |
| var typeInfo = _semanticModel.GetTypeInfo(node); | |
| // Do not translate until we can resolve the target type | |
| if (typeInfo.ConvertedType is not null) | |
| { | |
| // Translate null-conditional into a conditional expression, wrapped inside parenthesis | |
| return SyntaxFactory.ParenthesizedExpression( | |
| SyntaxFactory.ConditionalExpression( | |
| SyntaxFactory.BinaryExpression( | |
| SyntaxKind.NotEqualsExpression, | |
| targetExpression.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | |
| SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")) | |
| ).WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | |
| SyntaxFactory.ParenthesizedExpression( | |
| (ExpressionSyntax)Visit(node.WhenNotNull) | |
| ).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")).WithTrailingTrivia(SyntaxFactory.Whitespace(" ")), | |
| SyntaxFactory.CastExpression( | |
| SyntaxFactory.ParseName(typeInfo.ConvertedType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)), | |
| SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression) | |
| ).WithLeadingTrivia(SyntaxFactory.Whitespace(" ")) | |
| ).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia())); | |
| } | |
| } | |
| return base.VisitConditionalAccessExpression(node); |
Updated [EntityFrameworkCore.Projectables](https://github.com/EFNext/EntityFrameworkCore.Projectables) from 5.0.2 to 6.0.0. <details> <summary>Release notes</summary> _Sourced from [EntityFrameworkCore.Projectables's releases](https://github.com/EFNext/EntityFrameworkCore.Projectables/releases)._ ## 6.0.0 ## What's Changed ### Major changes * Add support for projectable method overloads by @PhenX in EFNext/EntityFrameworkCore.Projectables#143 * Add C#14 extension members by @PhenX in EFNext/EntityFrameworkCore.Projectables#148 * Support explicitly implemented interface members and default interface properties by @rhodon-jargon in EFNext/EntityFrameworkCore.Projectables#135 * Support block-bodied members with [Projectable] attribute by @Copilot in EFNext/EntityFrameworkCore.Projectables#152 * Add ExpandEnumMethods to expand enum extension calls into ternary expressions by @Copilot in EFNext/EntityFrameworkCore.Projectables#150 * Add support for pattern matching in various cases by @PhenX in EFNext/EntityFrameworkCore.Projectables#158 * Add support for projectable constructors by @PhenX in EFNext/EntityFrameworkCore.Projectables#161 * AOT-compatible static projection registry + SyntaxFactory-based emission by @Copilot in EFNext/EntityFrameworkCore.Projectables#166 * Improve UseMemberBody and add new diagnostics by @PhenX in EFNext/EntityFrameworkCore.Projectables#174 * Add code fixes for EFP0001, EFP0002 and EFP0008, with tests by @PhenX in EFNext/EntityFrameworkCore.Projectables#181 * Add a code fixer and a code refactorer to transform factory methods into constructors by @PhenX in EFNext/EntityFrameworkCore.Projectables#183 * Improve source generator and analyzer responsiveness by @PhenX in EFNext/EntityFrameworkCore.Projectables#171 * Docs website by @PhenX in EFNext/EntityFrameworkCore.Projectables#194 * Implement global MSBuild defaults for [Projectable] options by @koenbeuk in EFNext/EntityFrameworkCore.Projectables#191 ### Other changes * Check source generator output compilation by @PhenX in EFNext/EntityFrameworkCore.Projectables#156 * Fix GetImplementingProperty issue when using interfaces by @PhenX in EFNext/EntityFrameworkCore.Projectables#144 * Full qualification not needed anymore by @PhenX in EFNext/EntityFrameworkCore.Projectables#157 * Split generator tests in different classes by @PhenX in EFNext/EntityFrameworkCore.Projectables#162 * Refactor ExpressionSyntaxRewriter into focused partial class files by @Copilot in EFNext/EntityFrameworkCore.Projectables#163 * Update readme and add new test for constructors by @PhenX in EFNext/EntityFrameworkCore.Projectables#165 * Remove obsolete code by @PhenX in EFNext/EntityFrameworkCore.Projectables#167 * Add source generator self-benchmark covering all expression transformers by @Copilot in EFNext/EntityFrameworkCore.Projectables#168 * Fix null conditional rewrite generating invalid member access on nullable value types by @Copilot in EFNext/EntityFrameworkCore.Projectables#169 * Improve generator benchmark with more realistic scenario by @PhenX in EFNext/EntityFrameworkCore.Projectables#170 * Fix stale incremental generator cache when referenced types change in other source files by @Copilot in EFNext/EntityFrameworkCore.Projectables#172 * Add tests for properties with a getter and setter by @PhenX in EFNext/EntityFrameworkCore.Projectables#173 * UseMemberBody more strict with expressions by @PhenX in EFNext/EntityFrameworkCore.Projectables#175 * Add custom Copilot instructions by @PhenX in EFNext/EntityFrameworkCore.Projectables#177 * Reorganize generator project by @PhenX in EFNext/EntityFrameworkCore.Projectables#178 * Add closure resolution benchmark by @PhenX in EFNext/EntityFrameworkCore.Projectables#179 * Feature/optimize closures by @PhenX in EFNext/EntityFrameworkCore.Projectables#180 * Feature/optimize resolver by @PhenX in EFNext/EntityFrameworkCore.Projectables#182 * Update github project urls by @PhenX in EFNext/EntityFrameworkCore.Projectables#184 * Update deps by @PhenX in EFNext/EntityFrameworkCore.Projectables#185 * Remove obsolete verified files and name net8 files correctly by @PhenX in EFNext/EntityFrameworkCore.Projectables#186 * Remove all allocations when resolving and make it even faster by @PhenX in EFNext/EntityFrameworkCore.Projectables#189 * Add devcontainer configuration for C# (.NET) development by @koenbeuk in EFNext/EntityFrameworkCore.Projectables#190 **Full Changelog**: EFNext/EntityFrameworkCore.Projectables@v5.0.2...v6.0.0 Commits viewable in [compare view](EFNext/EntityFrameworkCore.Projectables@v5.0.2...v6.0.0). </details> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: James Gunn <james@gunn.io>
Updated [EntityFrameworkCore.Projectables](https://github.com/EFNext/EntityFrameworkCore.Projectables) from 5.0.2 to 6.0.0. <details> <summary>Release notes</summary> _Sourced from [EntityFrameworkCore.Projectables's releases](https://github.com/EFNext/EntityFrameworkCore.Projectables/releases)._ ## 6.0.0 ## What's Changed ### Major changes * Add support for projectable method overloads by @PhenX in EFNext/EntityFrameworkCore.Projectables#143 * Add C#14 extension members by @PhenX in EFNext/EntityFrameworkCore.Projectables#148 * Support explicitly implemented interface members and default interface properties by @rhodon-jargon in EFNext/EntityFrameworkCore.Projectables#135 * Support block-bodied members with [Projectable] attribute by @Copilot in EFNext/EntityFrameworkCore.Projectables#152 * Add ExpandEnumMethods to expand enum extension calls into ternary expressions by @Copilot in EFNext/EntityFrameworkCore.Projectables#150 * Add support for pattern matching in various cases by @PhenX in EFNext/EntityFrameworkCore.Projectables#158 * Add support for projectable constructors by @PhenX in EFNext/EntityFrameworkCore.Projectables#161 * AOT-compatible static projection registry + SyntaxFactory-based emission by @Copilot in EFNext/EntityFrameworkCore.Projectables#166 * Improve UseMemberBody and add new diagnostics by @PhenX in EFNext/EntityFrameworkCore.Projectables#174 * Add code fixes for EFP0001, EFP0002 and EFP0008, with tests by @PhenX in EFNext/EntityFrameworkCore.Projectables#181 * Add a code fixer and a code refactorer to transform factory methods into constructors by @PhenX in EFNext/EntityFrameworkCore.Projectables#183 * Improve source generator and analyzer responsiveness by @PhenX in EFNext/EntityFrameworkCore.Projectables#171 * Docs website by @PhenX in EFNext/EntityFrameworkCore.Projectables#194 * Implement global MSBuild defaults for [Projectable] options by @koenbeuk in EFNext/EntityFrameworkCore.Projectables#191 ### Other changes * Check source generator output compilation by @PhenX in EFNext/EntityFrameworkCore.Projectables#156 * Fix GetImplementingProperty issue when using interfaces by @PhenX in EFNext/EntityFrameworkCore.Projectables#144 * Full qualification not needed anymore by @PhenX in EFNext/EntityFrameworkCore.Projectables#157 * Split generator tests in different classes by @PhenX in EFNext/EntityFrameworkCore.Projectables#162 * Refactor ExpressionSyntaxRewriter into focused partial class files by @Copilot in EFNext/EntityFrameworkCore.Projectables#163 * Update readme and add new test for constructors by @PhenX in EFNext/EntityFrameworkCore.Projectables#165 * Remove obsolete code by @PhenX in EFNext/EntityFrameworkCore.Projectables#167 * Add source generator self-benchmark covering all expression transformers by @Copilot in EFNext/EntityFrameworkCore.Projectables#168 * Fix null conditional rewrite generating invalid member access on nullable value types by @Copilot in EFNext/EntityFrameworkCore.Projectables#169 * Improve generator benchmark with more realistic scenario by @PhenX in EFNext/EntityFrameworkCore.Projectables#170 * Fix stale incremental generator cache when referenced types change in other source files by @Copilot in EFNext/EntityFrameworkCore.Projectables#172 * Add tests for properties with a getter and setter by @PhenX in EFNext/EntityFrameworkCore.Projectables#173 * UseMemberBody more strict with expressions by @PhenX in EFNext/EntityFrameworkCore.Projectables#175 * Add custom Copilot instructions by @PhenX in EFNext/EntityFrameworkCore.Projectables#177 * Reorganize generator project by @PhenX in EFNext/EntityFrameworkCore.Projectables#178 * Add closure resolution benchmark by @PhenX in EFNext/EntityFrameworkCore.Projectables#179 * Feature/optimize closures by @PhenX in EFNext/EntityFrameworkCore.Projectables#180 * Feature/optimize resolver by @PhenX in EFNext/EntityFrameworkCore.Projectables#182 * Update github project urls by @PhenX in EFNext/EntityFrameworkCore.Projectables#184 * Update deps by @PhenX in EFNext/EntityFrameworkCore.Projectables#185 * Remove obsolete verified files and name net8 files correctly by @PhenX in EFNext/EntityFrameworkCore.Projectables#186 * Remove all allocations when resolving and make it even faster by @PhenX in EFNext/EntityFrameworkCore.Projectables#189 * Add devcontainer configuration for C# (.NET) development by @koenbeuk in EFNext/EntityFrameworkCore.Projectables#190 **Full Changelog**: EFNext/EntityFrameworkCore.Projectables@v5.0.2...v6.0.0 Commits viewable in [compare view](EFNext/EntityFrameworkCore.Projectables@v5.0.2...v6.0.0). </details> [](https://docs.github.com/en/github/managing-security-vulnerabilities/about-dependabot-security-updates#about-compatibility-scores) Dependabot will resolve any conflicts with this PR as long as you don't alter it yourself. You can also trigger a rebase manually by commenting `@dependabot rebase`. [//]: # (dependabot-automerge-start) [//]: # (dependabot-automerge-end) --- <details> <summary>Dependabot commands and options</summary> <br /> You can trigger Dependabot actions by commenting on this PR: - `@dependabot rebase` will rebase this PR - `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it - `@dependabot show <dependency name> ignore conditions` will show all of the ignore conditions of the specified dependency - `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself) - `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself) </details> --------- Signed-off-by: dependabot[bot] <support@github.com> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> Co-authored-by: James Gunn <james@gunn.io>
ExpressionSyntaxRewriterhad grown into a single ~880-line file mixing enum expansion, switch expression rewriting, null-conditional handling, and pattern conversion logic.Changes
Split the class into four focused files using
partial classand file-scoped namespaces:ExpressionSyntaxRewriter.cs— Class declaration, constructor, fields, and core visit methods (VisitIdentifierName,VisitInvocationExpression,VisitIsPatternExpression, etc.)EnumMethodExpander.cs—TryExpandEnumMethodCall+CreateMethodCallOnEnumValueSwitchExpressionRewriter.cs—VisitSwitchExpression,ReplaceVariableWithCast,ConvertPatternToExpression,ConvertRecursivePattern,TypeRequiresNullCheckNullConditionalRewriter.cs—VisitConditionalAccessExpression,VisitMemberBindingExpression,VisitElementBindingExpressionAll files use file-scoped namespaces. No behavioral changes — all existing tests pass unchanged.
Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.