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);
|
||||
_context.ReportDiagnostic(diagnostic);
|
||||
|
||||
// Return the original node, do not attempt further rewrites
|
||||
return node;
|
||||
}
|
||||
|
||||
else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Ignore)
|
||||
@@ -112,11 +115,8 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
|
||||
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 {
|
||||
@@ -126,13 +126,13 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
};
|
||||
}
|
||||
|
||||
public override SyntaxNode? VisitElementBindingExpression(ElementBindingExpressionSyntax node)
|
||||
{
|
||||
if (_conditionalAccessExpressionsStack.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("Expected at least one conditional expression on the stack");
|
||||
return base.VisitMemberBindingExpression(node);
|
||||
}
|
||||
|
||||
public override SyntaxNode? VisitElementBindingExpression(ElementBindingExpressionSyntax node)
|
||||
{
|
||||
if (_conditionalAccessExpressionsStack.Count > 0)
|
||||
{
|
||||
var targetExpression = _conditionalAccessExpressionsStack.Pop();
|
||||
|
||||
return _nullConditionalRewriteSupport switch {
|
||||
@@ -142,6 +142,9 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
};
|
||||
}
|
||||
|
||||
return base.VisitElementBindingExpression(node);
|
||||
}
|
||||
|
||||
public override SyntaxNode? VisitThisExpression(ThisExpressionSyntax node)
|
||||
{
|
||||
// Swap out the use of this to @this
|
||||
|
||||
@@ -644,6 +644,69 @@ namespace Foo {
|
||||
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]
|
||||
public Task NullableMemberBinding_WithIgnoreSupport_IsBeingRewritten()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user