Skip to content

Commit fe695d1

Browse files
authored
Merge pull request #118 from PhenX/bugfix/116-ternary-parenthesis
Fix missing parenthesis around ternary expressions (Fixes #116)
2 parents fc50971 + f77f296 commit fe695d1

9 files changed

+53
-10
lines changed

src/EntityFrameworkCore.Projectables.Generator/ExpressionSyntaxRewriter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, NullCondition
100100

101101
else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Rewrite)
102102
{
103-
var whenNotNullSymbol = _semanticModel.GetSymbolInfo(node.WhenNotNull).Symbol as IPropertySymbol;
104103
var typeInfo = _semanticModel.GetTypeInfo(node);
105104

106105
// Do not translate until we can resolve the target type
107106
if (typeInfo.ConvertedType is not null)
108107
{
109-
// Translate null-conditional into a conditional expression
110-
return SyntaxFactory.ConditionalExpression(
108+
// Translate null-conditional into a conditional expression, wrapped inside parenthesis
109+
return SyntaxFactory.ParenthesizedExpression(
110+
SyntaxFactory.ConditionalExpression(
111111
SyntaxFactory.BinaryExpression(
112112
SyntaxKind.NotEqualsExpression,
113113
targetExpression.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")),
@@ -120,7 +120,7 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, NullCondition
120120
SyntaxFactory.ParseName(typeInfo.ConvertedType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)),
121121
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)
122122
).WithLeadingTrivia(SyntaxFactory.Whitespace(" "))
123-
).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
123+
).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()));
124124
}
125125
}
126126

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// <auto-generated/>
2+
#nullable disable
3+
using System;
4+
using System.Linq;
5+
using EntityFrameworkCore.Projectables;
6+
using Foo;
7+
8+
namespace EntityFrameworkCore.Projectables.Generated
9+
{
10+
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
11+
static class Foo_C_Test
12+
{
13+
static global::System.Linq.Expressions.Expression<global::System.Func<object, bool>> Expression()
14+
{
15+
return (object x) => (x != null ? (x.Equals(4)) : ( bool ? )null) == false;
16+
}
17+
}
18+
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.NullConditionalNullCoalesceTypeConversion.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
99
{
1010
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo, int>> Expression()
1111
{
12-
return (global::Foo fancyClass) => fancyClass != null ? (fancyClass.FancyNumber) : ( int ? )null ?? 3;
12+
return (global::Foo fancyClass) => (fancyClass != null ? (fancyClass.FancyNumber) : ( int ? )null) ?? 3;
1313
}
1414
}
1515
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.NullableElementAndMemberBinding_WithRewriteSupport_IsBeingRewritten.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace EntityFrameworkCore.Projectables.Generated
1313
{
1414
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression()
1515
{
16-
return (global::Foo.EntityExtensions.Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : (global::Foo.EntityExtensions.Entity)null) : (global::Foo.EntityExtensions.Entity)null;
16+
return (global::Foo.EntityExtensions.Entity entity) => (entity != null ? ((entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : (global::Foo.EntityExtensions.Entity)null)) : (global::Foo.EntityExtensions.Entity)null);
1717
}
1818
}
1919
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.NullableElementBinding_WithRewriteSupport_IsBeingRewritten.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace EntityFrameworkCore.Projectables.Generated
1212
{
1313
static global::System.Linq.Expressions.Expression<global::System.Func<string, string>> Expression()
1414
{
15-
return (string input) => input != null ? (input[0].ToString()) : ( string )null;
15+
return (string input) => (input != null ? (input[0].ToString()) : ( string )null);
1616
}
1717
}
1818
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.NullableMemberBinding_WithRewriteSupport_IsBeingRewritten.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace EntityFrameworkCore.Projectables.Generated
1212
{
1313
static global::System.Linq.Expressions.Expression<global::System.Func<string, int?>> Expression()
1414
{
15-
return (string input) => input != null ? (input.Length) : ( int ? )null;
15+
return (string input) => (input != null ? (input.Length) : ( int ? )null);
1616
}
1717
}
1818
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.NullableParameters_WithRewriteSupport_IsBeingRewritten.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace EntityFrameworkCore.Projectables.Generated
1313
{
1414
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo.EntityExtensions.Entity, string>> Expression()
1515
{
16-
return (global::Foo.EntityExtensions.Entity entity) => entity.FullName != null ? (entity.FullName.Substring(entity.FullName != null ? (entity.FullName.IndexOf(' ')) : ( int ? )null ?? 0)) : ( string )null;
16+
return (global::Foo.EntityExtensions.Entity entity) => (entity.FullName != null ? (entity.FullName.Substring((entity.FullName != null ? (entity.FullName.IndexOf(' ')) : ( int ? )null) ?? 0)) : ( string )null);
1717
}
1818
}
1919
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.NullableSimpleElementBinding_WithRewriteSupport_IsBeingRewritten.verified.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace EntityFrameworkCore.Projectables.Generated
1212
{
1313
static global::System.Linq.Expressions.Expression<global::System.Func<string, char?>> Expression()
1414
{
15-
return (string input) => input != null ? (input[0]) : ( char ? )null;
15+
return (string input) => (input != null ? (input[0]) : ( char ? )null);
1616
}
1717
}
1818
}

tests/EntityFrameworkCore.Projectables.Generator.Tests/ProjectionExpressionGeneratorTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,31 @@ static class C {
803803
return Verifier.Verify(result.GeneratedTrees[0].ToString());
804804
}
805805

806+
[Fact]
807+
public Task BooleanSimpleTernary_WithRewriteSupport_IsBeingRewritten()
808+
{
809+
var compilation = CreateCompilation(@"
810+
using System;
811+
using System.Linq;
812+
using EntityFrameworkCore.Projectables;
813+
814+
namespace Foo {
815+
static class C {
816+
[Projectable(NullConditionalRewriteSupport = NullConditionalRewriteSupport.Rewrite)]
817+
public static bool Test(this object? x) => x?.Equals(4) == false;
818+
}
819+
}
820+
");
821+
822+
var result = RunGenerator(compilation);
823+
824+
Assert.Empty(result.Diagnostics);
825+
Assert.Single(result.GeneratedTrees);
826+
827+
return Verifier.Verify(result.GeneratedTrees[0].ToString());
828+
}
829+
830+
806831

807832
[Fact]
808833
public Task NullableElementBinding_WithIgnoreSupport_IsBeingRewritten()

0 commit comments

Comments
 (0)