mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2025-12-06 05:56:10 +00:00
Merge pull request #75 from koenbeuk/issue-73
Don't throw when out of accessible expressions
This commit is contained in:
@@ -72,6 +72,9 @@ namespace EntityFrameworkCore.Projectables.Generator
|
|||||||
{
|
{
|
||||||
var diagnostic = Diagnostic.Create(Diagnostics.NullConditionalRewriteUnsupported, node.GetLocation(), node);
|
var diagnostic = Diagnostic.Create(Diagnostics.NullConditionalRewriteUnsupported, node.GetLocation(), node);
|
||||||
_context.ReportDiagnostic(diagnostic);
|
_context.ReportDiagnostic(diagnostic);
|
||||||
|
|
||||||
|
// Return the original node, do not attempt further rewrites
|
||||||
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Ignore)
|
else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Ignore)
|
||||||
@@ -112,34 +115,34 @@ namespace EntityFrameworkCore.Projectables.Generator
|
|||||||
|
|
||||||
public override SyntaxNode? VisitMemberBindingExpression(MemberBindingExpressionSyntax node)
|
public override SyntaxNode? VisitMemberBindingExpression(MemberBindingExpressionSyntax node)
|
||||||
{
|
{
|
||||||
if (_conditionalAccessExpressionsStack.Count == 0)
|
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
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetExpression = _conditionalAccessExpressionsStack.Pop();
|
return base.VisitMemberBindingExpression(node);
|
||||||
|
|
||||||
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)
|
public override SyntaxNode? VisitElementBindingExpression(ElementBindingExpressionSyntax node)
|
||||||
{
|
{
|
||||||
if (_conditionalAccessExpressionsStack.Count == 0)
|
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)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
var targetExpression = _conditionalAccessExpressionsStack.Pop();
|
return base.VisitElementBindingExpression(node);
|
||||||
|
|
||||||
return _nullConditionalRewriteSupport switch {
|
|
||||||
NullConditionalRewriteSupport.Ignore => SyntaxFactory.ElementAccessExpression(targetExpression, node.ArgumentList),
|
|
||||||
NullConditionalRewriteSupport.Rewrite => SyntaxFactory.ElementAccessExpression(targetExpression, node.ArgumentList),
|
|
||||||
_ => Visit(node)
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override SyntaxNode? VisitThisExpression(ThisExpressionSyntax node)
|
public override SyntaxNode? VisitThisExpression(ThisExpressionSyntax node)
|
||||||
|
|||||||
@@ -644,6 +644,69 @@ namespace Foo {
|
|||||||
Assert.Equal("EFP0002", diagnostic.Id);
|
Assert.Equal("EFP0002", diagnostic.Id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void NullableMemberBinding_UndefinedSupport_IsBeingReported()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
static class C {
|
||||||
|
[Projectable]
|
||||||
|
public static int? GetLength(this string input) => input?.Length;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
var diagnostic = Assert.Single(result.Diagnostics);
|
||||||
|
Assert.Equal("EFP0002", diagnostic.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MultiLevelNullableMemberBinding_UndefinedSupport_IsBeingReported()
|
||||||
|
{
|
||||||
|
var compilation = CreateCompilation(@"
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using EntityFrameworkCore.Projectables;
|
||||||
|
|
||||||
|
namespace Foo {
|
||||||
|
public record Address
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string? Country { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Party
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public Address? Address { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
public record Entity
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
public Party? Left { get; set; }
|
||||||
|
public Party? Right { get; set; }
|
||||||
|
|
||||||
|
[Projectable]
|
||||||
|
public bool IsSameCountry => Left?.Address?.Country == Right?.Address?.Country;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
");
|
||||||
|
var result = RunGenerator(compilation);
|
||||||
|
|
||||||
|
Assert.All(result.Diagnostics, diagnostic => {
|
||||||
|
Assert.Equal("EFP0002", diagnostic.Id);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public Task NullableMemberBinding_WithIgnoreSupport_IsBeingRewritten()
|
public Task NullableMemberBinding_WithIgnoreSupport_IsBeingRewritten()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user