Skip to content

Commit

Permalink
Fix missing parenthesis around ternary expressions (Fixes #116)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhenX committed Oct 15, 2024
1 parent ac67d12 commit 5b4a3a0
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, NullCondition

else if (_nullConditionalRewriteSupport is NullConditionalRewriteSupport.Rewrite)
{
var whenNotNullSymbol = _semanticModel.GetSymbolInfo(node.WhenNotNull).Symbol as IPropertySymbol;
var typeInfo = _semanticModel.GetTypeInfo(node);

// Do not translate until we can resolve the target type
if (typeInfo.ConvertedType is not null)
{
// Translate null-conditional into a conditional expression
return SyntaxFactory.ConditionalExpression(
// Translate null-conditional into a conditional expression, wrapped inside parenthesis
return SyntaxFactory.ParenthesizedExpression(
SyntaxFactory.ConditionalExpression(
SyntaxFactory.BinaryExpression(
SyntaxKind.NotEqualsExpression,
targetExpression.WithTrailingTrivia(SyntaxFactory.Whitespace(" ")),
Expand All @@ -105,7 +105,7 @@ public ExpressionSyntaxRewriter(INamedTypeSymbol targetTypeSymbol, NullCondition
SyntaxFactory.ParseName(typeInfo.ConvertedType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat)),
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)
).WithLeadingTrivia(SyntaxFactory.Whitespace(" "))
).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia());
).WithLeadingTrivia(node.GetLeadingTrivia()).WithTrailingTrivia(node.GetTrailingTrivia()));
}
}

Expand Down
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_Test
{
static global::System.Linq.Expressions.Expression<global::System.Func<object, bool>> Expression()
{
return (object x) => (x != null ? (x.Equals(4)) : ( bool ? )null) == false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
static global::System.Linq.Expressions.Expression<global::System.Func<global::Foo, int>> Expression()
{
return (global::Foo fancyClass) => fancyClass != null ? (fancyClass.FancyNumber) : ( int ? )null ?? 3;
return (global::Foo fancyClass) => (fancyClass != null ? (fancyClass.FancyNumber) : ( int ? )null) ?? 3;
}
}
}
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.EntityExtensions.Entity, global::Foo.EntityExtensions.Entity>> Expression()
{
return (global::Foo.EntityExtensions.Entity entity) => entity != null ? (entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : (global::Foo.EntityExtensions.Entity)null) : (global::Foo.EntityExtensions.Entity)null;
return (global::Foo.EntityExtensions.Entity entity) => (entity != null ? ((entity.RelatedEntities != null ? (entity.RelatedEntities[0]) : (global::Foo.EntityExtensions.Entity)null)) : (global::Foo.EntityExtensions.Entity)null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
static global::System.Linq.Expressions.Expression<global::System.Func<string, string>> Expression()
{
return (string input) => input != null ? (input[0].ToString()) : ( string )null;
return (string input) => (input != null ? (input[0].ToString()) : ( string )null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
static global::System.Linq.Expressions.Expression<global::System.Func<string, int?>> Expression()
{
return (string input) => input != null ? (input.Length) : ( int ? )null;
return (string input) => (input != null ? (input.Length) : ( int ? )null);
}
}
}
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.EntityExtensions.Entity, string>> Expression()
{
return (global::Foo.EntityExtensions.Entity entity) => entity.FullName != null ? (entity.FullName.Substring(entity.FullName != null ? (entity.FullName.IndexOf(' ')) : ( int ? )null ?? 0)) : ( string )null;
return (global::Foo.EntityExtensions.Entity entity) => (entity.FullName != null ? (entity.FullName.Substring((entity.FullName != null ? (entity.FullName.IndexOf(' ')) : ( int ? )null) ?? 0)) : ( string )null);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace EntityFrameworkCore.Projectables.Generated
{
static global::System.Linq.Expressions.Expression<global::System.Func<string, char?>> Expression()
{
return (string input) => input != null ? (input[0]) : ( char ? )null;
return (string input) => (input != null ? (input[0]) : ( char ? )null);
}
}
}

0 comments on commit 5b4a3a0

Please sign in to comment.