Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, NullCondition
return base.VisitInvocationExpression(node);
}

public override SyntaxNode? VisitInterpolation(InterpolationSyntax node)
{
// Visit the expression first
var targetExpression = (ExpressionSyntax)Visit(node.Expression);

// Check if the expression already has parentheses
if (targetExpression is ParenthesizedExpressionSyntax)
{
return node.WithExpression(targetExpression);
}

// Create a new expression wrapped in parentheses
var newExpression = SyntaxFactory.ParenthesizedExpression(targetExpression);

return node.WithExpression(newExpression);
}

public override SyntaxNode? VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
{
var targetExpression = (ExpressionSyntax)Visit(node.Expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.Entity<T, TEnum>, string>> Expression()
{
return (global::Foo.Entity<T, TEnum> @this) => $"{@this.FirstName} {@this.LastName} {@this.SomeSubobject.SomeProp}";
return (global::Foo.Entity<T, TEnum> @this) => $"{(@this.FirstName)} {(@this.LastName)} {(@this.SomeSubobject.SomeProp)}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.Entity<T>, string>> Expression()
{
return (global::Foo.Entity<T> @this) => $"{@this.FirstName} {@this.LastName} {@this.SomeSubobject}";
return (global::Foo.Entity<T> @this) => $"{(@this.FirstName)} {(@this.LastName)} {(@this.SomeSubobject)}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <auto-generated/>
#nullable disable
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;
using Foo;

namespace EntityFrameworkCore.Projectables.Generated
{
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
static class Foo_C_Status
{
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.C, string>> Expression()
{
return (global::Foo.C @this) => @this.ValidationDate != null ? $"Validation date : ({(global::Foo.MyExtensions.ToDateString(@this.ValidationDate.Value))})" : "";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// <auto-generated/>
#nullable disable
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;
using Foo;

namespace EntityFrameworkCore.Projectables.Generated
{
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
static class Foo_C_Status
{
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.C, string>> Expression()
{
return (global::Foo.C @this) => @this.ValidationDate != null ? $"Validation date : ({(global::Foo.MyExtensions.ToDateString(@this.ValidationDate.Value))})" : "";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,66 @@ static class C {

return Verifier.Verify(result.GeneratedTrees[0].ToString());
}

[Fact]
public Task StringInterpolationWithStaticCall_IsBeingRewritten()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;

namespace Foo {
static class MyExtensions {
public static string ToDateString(this DateTime date) => date.ToString(""dd/MM/yyyy"");
}

class C {
public DateTime? ValidationDate { get; set; }

[Projectable]
public string Status => ValidationDate != null ? $""Validation date : ({ValidationDate.Value.ToDateString()})"" : """";
}
}
");

var result = RunGenerator(compilation);

Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);

return Verifier.Verify(result.GeneratedTrees[0].ToString());
}

[Fact]
public Task StringInterpolationWithParenthesis_NoParenthesisAdded()
{
var compilation = CreateCompilation(@"
using System;
using System.Linq;
using EntityFrameworkCore.Projectables;

namespace Foo {
static class MyExtensions {
public static string ToDateString(this DateTime date) => date.ToString(""dd/MM/yyyy"");
}

class C {
public DateTime? ValidationDate { get; set; }

[Projectable]
public string Status => ValidationDate != null ? $""Validation date : ({(ValidationDate.Value.ToDateString())})"" : """";
}
}
");

var result = RunGenerator(compilation);

Assert.Empty(result.Diagnostics);
Assert.Single(result.GeneratedTrees);

return Verifier.Verify(result.GeneratedTrees[0].ToString());
}

[Fact]
public Task NullableSimpleElementBinding_WithRewriteSupport_IsBeingRewritten()
Expand Down