Fixed code gen issue for Projectable extension methods using other projectable extension methods

This commit is contained in:
Koen Bekkenutte
2021-07-02 03:28:40 +08:00
parent 0899408ed1
commit 659f10ac10
7 changed files with 105 additions and 1 deletions

View File

@@ -51,7 +51,17 @@ namespace EntityFrameworkCore.Projectables.Generator
if (symbolInfo.Symbol is not null)
{
if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field && SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
if (symbolInfo.Symbol is IMethodSymbol methodSymbol && methodSymbol.IsExtensionMethod)
{
if (SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
{
//return SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
// methodSymbol.ReducedFrom.Sym
//);
//throw new Exception("foo");
}
}
else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field && SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
{
return SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
SyntaxFactory.IdentifierName("@this"),

View File

@@ -11,6 +11,9 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
[Projectable]
public static int Foo(this Entity entity) => entity.Id + 1;
[Projectable]
public static int Foo2(this Entity entity) => entity.Foo() + 1;
[Projectable]
public static Entity? LeadingEntity(this Entity entity, DbContext dbContext)
=> dbContext.Set<Entity>().Where(y => y.Id > entity.Id).FirstOrDefault();

View File

@@ -38,6 +38,17 @@ namespace EntityFrameworkCore.Projectables.FunctionalTests.ExtensionMethods
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task SelectProjectableExtensionMethod2()
{
using var dbContext = new SampleDbContext<Entity>();
var query = dbContext.Set<Entity>()
.Select(x => x.Foo2());
return Verifier.Verify(query.ToQueryString());
}
[Fact]
public Task ExtensionMethodAcceptingDbContext()
{

View File

@@ -0,0 +1,14 @@
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;
using Foo;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class Foo_C_Foo1
{
public static System.Linq.Expressions.Expression<System.Func<int, int>> Expression =>
(int i) => i;
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;
using Foo;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class Foo_C_Foo1
{
public static System.Linq.Expressions.Expression<System.Func<object, object>> Expression =>
(object i) => i.Foo1();
}
}

View File

@@ -367,6 +367,56 @@ namespace Foo {
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ProjectableExtensionMethod3()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;
namespace Foo {
static class C {
[Projectable]
public static int Foo1(this int i) => i;
[Projectable]
public static int Foo2(this int i) => i.Foo1();
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Equal(2, result.GeneratedTrees.Length);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public Task ProjectableExtensionMethod4()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;
namespace Foo {
static class C {
[Projectable]
public static object Foo1(this object i) => i.Foo1();
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact]
public void BlockBodiedMember_RaisesDiagnostics()
{