support for rewriting null conditional access expressions

This commit is contained in:
Koen Bekkenutte
2021-10-18 22:36:06 +08:00
parent 654dcba6ff
commit 86223a11c9
29 changed files with 682 additions and 11 deletions
@@ -0,0 +1,30 @@
namespace EntityFrameworkCore.Projectables
{
/// <summary>
/// Configures how null-conditional operators are handeled
/// </summary>
public enum NullConditionalRewriteSupport
{
/// <summary>
/// Don't rewrite null conditional operators (Default behavior).
/// Usage of null conditional operators is thereby not allowed
/// </summary>
None,
/// <summary>
/// Ignore null-conditional operators in the generated expression tree
/// </summary>
/// <remarks>
/// <c>(A?.B)</c> is rewritten as expression: <c>(A.B)</c>
/// </remarks>
Ignore,
/// <summary>
/// Translates null-conditional operators into explicit null checks
/// </summary>
/// <remarks>
/// <c>(A?.B)</c> is rewritten as expression: <c>(A != null ? A.B : null)</c>
/// </remarks>
Rewrite
}
}
@@ -9,5 +9,6 @@ namespace EntityFrameworkCore.Projectables
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class ProjectableAttribute : Attribute
{
public NullConditionalRewriteSupport NullConditionalRewriteSupport { get; set; }
}
}
@@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="..\EntityFrameworkCore.Projectables.Abstractions\NullConditionalRewriteSupport.cs" Link="NullConditionalRewriteSupport.cs" />
<Compile Include="..\EntityFrameworkCore.Projectables\Services\ProjectionExpressionClassNameGenerator.cs" Link="ProjectionExpressionClassNameGenerator.cs" />
</ItemGroup>
@@ -14,11 +14,76 @@ namespace EntityFrameworkCore.Projectables.Generator
{
readonly INamedTypeSymbol _targetTypeSymbol;
readonly SemanticModel _semanticModel;
readonly NullConditionalRewriteSupport _nullConditionalRewriteSupport;
readonly Stack<ExpressionSyntax> _conditionalAccessExpressionsStack = new();
public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, SemanticModel semanticModel)
public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, SemanticModel semanticModel, NullConditionalRewriteSupport nullConditionalRewriteSupport)
{
_targetTypeSymbol = targetTypeSymbol;
_semanticModel = semanticModel;
_nullConditionalRewriteSupport = nullConditionalRewriteSupport;
}
public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
{
var targetExpression = (ExpressionSyntax)Visit(node.Expression);
_conditionalAccessExpressionsStack.Push(targetExpression);
return _nullConditionalRewriteSupport switch {
NullConditionalRewriteSupport.Ignore => Visit(node.WhenNotNull),
NullConditionalRewriteSupport.Rewrite =>
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.LiteralExpression(SyntaxKind.NullLiteralExpression)
.WithLeadingTrivia(SyntaxFactory.Whitespace(" "))
),
_ => base.VisitConditionalAccessExpression(node)
};
}
public override SyntaxNode? VisitMemberBindingExpression(MemberBindingExpressionSyntax node)
{
if (_conditionalAccessExpressionsStack.Count == 0)
{
throw new InvalidOperationException("Expected at least one conditional expression on the stack");
}
var targetExpression = _conditionalAccessExpressionsStack.Pop();
return _nullConditionalRewriteSupport switch {
NullConditionalRewriteSupport.Ignore => SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, targetExpression, node.Name),
NullConditionalRewriteSupport.Rewrite => SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, targetExpression, node.Name),
_ => node
};
}
public override SyntaxNode? VisitElementBindingExpression(ElementBindingExpressionSyntax node)
{
if (_conditionalAccessExpressionsStack.Count == 0)
{
throw new InvalidOperationException("Expected at least one conditional expression on the stack");
}
var targetExpression = _conditionalAccessExpressionsStack.Pop();
return _nullConditionalRewriteSupport switch {
NullConditionalRewriteSupport.Ignore => SyntaxFactory.ElementAccessExpression(targetExpression, node.ArgumentList),
NullConditionalRewriteSupport.Rewrite => SyntaxFactory.ElementAccessExpression(targetExpression, node.ArgumentList),
_ => Visit(node)
};
}
public override SyntaxNode? VisitMemberAccessExpression(MemberAccessExpressionSyntax node)
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
@@ -31,7 +32,6 @@ namespace EntityFrameworkCore.Projectables.Generator
return null;
}
var projectableAttributeTypeSymbol = context.Compilation.GetTypeByMetadataName("EntityFrameworkCore.Projectables.ProjectableAttribute");
var projectableAttributeClass = memberSymbol.GetAttributes()
@@ -43,7 +43,15 @@ namespace EntityFrameworkCore.Projectables.Generator
return null;
}
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel);
var nullConditionalRewriteSupport = projectableAttributeClass.NamedArguments
.Where(x => x.Key == "NullConditionalRewriteSupport")
.Where(x => x.Value.Kind == TypedConstantKind.Enum)
.Select(x => x.Value.Value)
.Where(x => Enum.IsDefined(typeof(NullConditionalRewriteSupport), x))
.Cast<NullConditionalRewriteSupport>()
.FirstOrDefault();
var expressionSyntaxRewriter = new ExpressionSyntaxRewriter(memberSymbol.ContainingType, semanticModel, nullConditionalRewriteSupport);
var parameterSyntaxRewriter = new ParameterSyntaxRewriter(semanticModel);
var returnTypeSyntaxRewriter = new ReturnTypeSyntaxRewriter(semanticModel);
@@ -128,8 +136,6 @@ namespace EntityFrameworkCore.Projectables.Generator
return null;
}
descriptor.UsingDirectives =
memberDeclarationSyntax.SyntaxTree
.GetRoot()
@@ -137,7 +143,6 @@ namespace EntityFrameworkCore.Projectables.Generator
.OfType<UsingDirectiveSyntax>()
.Select(x => x.ToString());
return descriptor;
}
}