Skip to content

Refactor ExpressionSyntaxRewriter into focused partial class files#163

Merged
PhenX merged 3 commits intomasterfrom
copilot/refactor-expression-syntax-rewriter
Mar 1, 2026
Merged

Refactor ExpressionSyntaxRewriter into focused partial class files#163
PhenX merged 3 commits intomasterfrom
copilot/refactor-expression-syntax-rewriter

Conversation

Copy link
Contributor

Copilot AI commented Mar 1, 2026

ExpressionSyntaxRewriter had 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 class and file-scoped namespaces:

  • ExpressionSyntaxRewriter.cs — Class declaration, constructor, fields, and core visit methods (VisitIdentifierName, VisitInvocationExpression, VisitIsPatternExpression, etc.)
  • EnumMethodExpander.csTryExpandEnumMethodCall + CreateMethodCallOnEnumValue
  • SwitchExpressionRewriter.csVisitSwitchExpression, ReplaceVariableWithCast, ConvertPatternToExpression, ConvertRecursivePattern, TypeRequiresNullCheck
  • NullConditionalRewriter.csVisitConditionalAccessExpression, VisitMemberBindingExpression, VisitElementBindingExpression

All files use file-scoped namespaces. No behavioral changes — all existing tests pass unchanged.

Original prompt

This section details on the original issue you should resolve

<issue_title>Refactor and simplify ExpressionSyntaxRewriter</issue_title>
<issue_description>This class has become huge, it is time to simplify it.

I suggest this :

  • Create EnumMethodExpander.cs - extract TryExpandEnumMethodCall and CreateMethodCallOnEnumValue
  • Create SwitchExpressionRewriter.cs - extract VisitSwitchExpression and ReplaceVariableWithCast
  • Create NullConditionalRewriter.cs - extract logic for null coalesce operators
  • Simplify main file and delegate to these new classes
  • Use file-scoped namespace
  • All existing tests must still pass exactly the same</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 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.

…tiple files

Co-authored-by: PhenX <42170+PhenX@users.noreply.github.com>
Copilot AI changed the title [WIP] Refactor and simplify ExpressionSyntaxRewriter class Refactor ExpressionSyntaxRewriter into focused partial class files Mar 1, 2026
Copilot finished work on behalf of PhenX March 1, 2026 21:51
@PhenX PhenX requested a review from Copilot March 1, 2026 21:58
@PhenX PhenX marked this pull request as ready for review March 1, 2026 21:59
@PhenX PhenX merged commit dfbece7 into master Mar 1, 2026
6 checks passed
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +8 to +56
{
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);
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

Suggested change
{
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);

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor and simplify ExpressionSyntaxRewriter

3 participants