Keep leading and traling trivias on identifiers to prevent some syntax errors

This commit is contained in:
fabien.menager
2022-10-05 17:41:33 +02:00
parent 6aa1e60309
commit f61a30d013
3 changed files with 53 additions and 2 deletions

View File

@@ -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)

View File

@@ -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;
}
}
}

View File

@@ -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()
{