Fix for projectable instance members returning complex types

This commit is contained in:
Koen Bekkenutte
2021-11-26 19:53:18 +08:00
parent f03aa16680
commit af76e5b0a0
4 changed files with 113 additions and 6 deletions
@@ -126,23 +126,35 @@ namespace EntityFrameworkCore.Projectables.Generator
{ {
if (symbolInfo.Symbol is IMethodSymbol methodSymbol && methodSymbol.IsExtensionMethod) if (symbolInfo.Symbol is IMethodSymbol methodSymbol && methodSymbol.IsExtensionMethod)
{ {
} }
else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field && SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol)) else if (symbolInfo.Symbol.Kind is SymbolKind.Property or SymbolKind.Method or SymbolKind.Field && SymbolEqualityComparer.Default.Equals(symbolInfo.Symbol.ContainingType, _targetTypeSymbol))
{ {
bool rewrite = true;
if (node.Parent is MemberAccessExpressionSyntax parentMemberAccessNode) if (node.Parent is MemberAccessExpressionSyntax parentMemberAccessNode)
{ {
var targetSymbolInfo = _semanticModel.GetSymbolInfo(parentMemberAccessNode.Expression); var targetSymbolInfo = _semanticModel.GetSymbolInfo(parentMemberAccessNode.Expression);
if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter }) if (targetSymbolInfo.Symbol is { Kind: SymbolKind.Parameter })
{ {
return base.VisitIdentifierName(node); rewrite = false;
} }
} }
else if (node.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
{
rewrite = false;
}
else if (node.Parent.IsKind(SyntaxKind.InvocationExpression))
{
rewrite = true;
}
return SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression, if (rewrite)
SyntaxFactory.IdentifierName("@this"), {
node return SyntaxFactory.MemberAccessExpression(SyntaxKind.SimpleMemberAccessExpression,
); SyntaxFactory.IdentifierName("@this"),
node
);
}
} }
else if (symbolInfo.Symbol.Kind is SymbolKind.NamedType && node.Parent?.Kind() is not SyntaxKind.QualifiedName) else if (symbolInfo.Symbol.Kind is SymbolKind.NamedType && node.Parent?.Kind() is not SyntaxKind.QualifiedName)
{ {
@@ -0,0 +1,16 @@
using System;
using EntityFrameworkCore.Projectables;
using Foo;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class Foo_C_Foo
{
public static System.Linq.Expressions.Expression<System.Func<global::Foo.C, int>> Expression()
{
return (global::Foo.C @this) =>
@this.Bar;
}
}
}
@@ -0,0 +1,20 @@
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
using Foo;
namespace EntityFrameworkCore.Projectables.Generated
#nullable disable
{
public static class Foo_EntityExtensions_Entity_Something
{
public static System.Linq.Expressions.Expression<System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression()
{
return (global::Foo.EntityExtensions.Entity entity) =>
new Entity(entity.Id) {
FullName = entity.FullName
};
}
}
}
@@ -76,6 +76,30 @@ namespace Foo {
return Verifier.Verify(result.GeneratedTrees[0].ToString()); return Verifier.Verify(result.GeneratedTrees[0].ToString());
} }
[Fact]
public Task MinimalProjectableComputedProperty()
{
var compilation = CreateCompilation(@"
using System;
using EntityFrameworkCore.Projectables;
namespace Foo {
class C {
public int Bar { get; set; }
[Projectable]
public int Foo => Bar;
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
[Fact] [Fact]
public Task SimpleProjectableComputedProperty() public Task SimpleProjectableComputedProperty()
{ {
@@ -900,6 +924,41 @@ namespace Foo {
return Verifier.Verify(result.GeneratedTrees[0].ToString()); return Verifier.Verify(result.GeneratedTrees[0].ToString());
} }
[Fact]
public Task MixPrimaryConstructorAndProperties()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using System.Collections.Generic;
using EntityFrameworkCore.Projectables;
namespace Foo {
public static class EntityExtensions
{
public record Entity(int Id)
{
public int Id { get; set; }
public string? FullName { get; set; }
[Projectable]
public static Entity Something(Entity entity)
=> new Entity(entity.Id) {
FullName = entity.FullName
};
}
}
}
");
var result = RunGenerator(compilation);
Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);
return Verifier.Verify(result.GeneratedTrees[0].ToString());
}
#region Helpers #region Helpers
Compilation CreateCompilation(string source, bool expectedToCompile = true) Compilation CreateCompilation(string source, bool expectedToCompile = true)