Skip to content

Commit

Permalink
Merge pull request #119 from PhenX/bugfix/115-string-interpolation
Browse files Browse the repository at this point in the history
Add parenthesis inside string interpolations (Fixes #115)
  • Loading branch information
koenbeuk authored Oct 22, 2024
2 parents fe695d1 + 020facf commit 2075d4c
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,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

0 comments on commit 2075d4c

Please sign in to comment.