mirror of
https://github.com/zoriya/EntityFrameworkCore.Projectables.git
synced 2025-12-06 05:56:10 +00:00
Keep leading and traling trivias on identifiers to prevent some syntax errors
This commit is contained in:
@@ -29,6 +29,14 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
_context = context;
|
||||
}
|
||||
|
||||
private SyntaxNode? VisitThisBaseExpression(CSharpSyntaxNode node)
|
||||
{
|
||||
// Swap out the use of this to @this and base to @base and keep leading and trailing trivias
|
||||
return SyntaxFactory.IdentifierName("@this")
|
||||
.WithLeadingTrivia(node.GetLeadingTrivia())
|
||||
.WithTrailingTrivia(node.GetTrailingTrivia());
|
||||
}
|
||||
|
||||
public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
|
||||
{
|
||||
var targetExpression = (ExpressionSyntax)Visit(node.Expression);
|
||||
@@ -100,13 +108,13 @@ namespace EntityFrameworkCore.Projectables.Generator
|
||||
public override SyntaxNode? VisitThisExpression(ThisExpressionSyntax node)
|
||||
{
|
||||
// Swap out the use of this to @this
|
||||
return SyntaxFactory.IdentifierName("@this");
|
||||
return VisitThisBaseExpression(node);
|
||||
}
|
||||
|
||||
public override SyntaxNode? VisitBaseExpression(BaseExpressionSyntax node)
|
||||
{
|
||||
// Swap out the use of this to @this
|
||||
return SyntaxFactory.IdentifierName("@this");
|
||||
return VisitThisBaseExpression(node);
|
||||
}
|
||||
|
||||
public override SyntaxNode? VisitIdentifierName(IdentifierNameSyntax node)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
using System;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
using Foo;
|
||||
|
||||
// <auto-generated/>
|
||||
namespace EntityFrameworkCore.Projectables.Generated
|
||||
#nullable disable
|
||||
{
|
||||
public static class Foo_A_IsB
|
||||
{
|
||||
public static System.Linq.Expressions.Expression<System.Func<global::Foo.A, bool>> Expression()
|
||||
{
|
||||
return (global::Foo.A @this) =>
|
||||
@this is global::Foo.B;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -343,6 +343,32 @@ namespace Foo {
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task InOperator()
|
||||
{
|
||||
var compilation = CreateCompilation(@"
|
||||
using System;
|
||||
using System.Linq;
|
||||
using EntityFrameworkCore.Projectables;
|
||||
namespace Foo {
|
||||
class A {
|
||||
[Projectable]
|
||||
public bool IsB => this is B;
|
||||
}
|
||||
|
||||
class B : A {
|
||||
}
|
||||
}
|
||||
");
|
||||
|
||||
var result = RunGenerator(compilation);
|
||||
|
||||
Assert.Empty(result.Diagnostics);
|
||||
Assert.Single(result.GeneratedTrees);
|
||||
|
||||
return Verifier.Verify(result.GeneratedTrees[0].ToString());
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public Task ProjectableExtensionMethod()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user