Skip to content

Commit c4e5f88

Browse files
Parenthesize all binary expression - fixes #565
Simplifier will deal with ones that weren't needed
1 parent c52caca commit c4e5f88

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
99

1010

1111
### VB -> C#
12+
1213
* Improve multi-declaration field conversion for arrrays - [#559](https://github.com/icsharpcode/CodeConverter/issues/559)
14+
* Add parentheses around ternary statement - [#565](https://github.com/icsharpcode/CodeConverter/issues/565)
1315

1416
### C# -> VB
1517

CodeConverter/CSharp/VbSyntaxNodeExtensions.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,13 @@ public static ExpressionSyntax ParenthesizeIfPrecedenceCouldChange(this VisualBa
2020

2121
public static bool PrecedenceCouldChange(this VisualBasicSyntaxNode node)
2222
{
23-
bool parentIsSameBinaryKind = node is BinaryExpressionSyntax && node.Parent is BinaryExpressionSyntax parent && parent.Kind() == node.Kind();
23+
bool parentIsBinaryExpression = node is BinaryExpressionSyntax;
2424
bool parentIsReturn = node.Parent is ReturnStatementSyntax;
2525
bool parentIsLambda = node.Parent is LambdaExpressionSyntax;
2626
bool parentIsNonArgumentExpression = node.Parent is Microsoft.CodeAnalysis.VisualBasic.Syntax.ExpressionSyntax && !(node.Parent is ArgumentSyntax);
2727
bool parentIsParenthesis = node.Parent is ParenthesizedExpressionSyntax;
2828

29-
// Could be a full C# precedence table - this is just a common case
30-
bool parentIsAndOr = node.Parent.IsKind(SyntaxKind.AndAlsoExpression, SyntaxKind.OrElseExpression);
31-
bool nodeIsRelationalOrEqual = node.IsKind(SyntaxKind.EqualsExpression, SyntaxKind.NotEqualsExpression,
32-
SyntaxKind.LessThanExpression, SyntaxKind.LessThanOrEqualExpression,
33-
SyntaxKind.GreaterThanExpression, SyntaxKind.GreaterThanOrEqualExpression);
34-
bool csharpPrecedenceSame = parentIsAndOr && nodeIsRelationalOrEqual;
35-
36-
return parentIsNonArgumentExpression && !parentIsSameBinaryKind && !parentIsReturn && !parentIsLambda && !parentIsParenthesis && !csharpPrecedenceSame;
29+
return parentIsNonArgumentExpression && !parentIsBinaryExpression && !parentIsReturn && !parentIsLambda && !parentIsParenthesis;
3730
}
3831
}
3932
}

Tests/CSharp/ExpressionTests/ExpressionTests.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,26 @@ private void TestMethod(string str)
995995
CS0103: The name 'string' does not exist in the current context");
996996
}
997997

998+
[Fact]
999+
public async Task ConditionalExpressionInStringConcatAsync()
1000+
{
1001+
await TestConversionVisualBasicToCSharpAsync(@"Class ConditionalExpressionInStringConcat
1002+
Private Sub TestMethod(ByVal str As String)
1003+
Dim appleCount as integer = 42
1004+
Console.WriteLine(""I have "" & appleCount & If(appleCount = 1, "" apple"", "" apples""))
1005+
End Sub
1006+
End Class", @"using System;
1007+
1008+
internal partial class ConditionalExpressionInStringConcat
1009+
{
1010+
private void TestMethod(string str)
1011+
{
1012+
int appleCount = 42;
1013+
Console.WriteLine(""I have "" + appleCount + (appleCount == 1 ? "" apple"" : "" apples""));
1014+
}
1015+
}");
1016+
}
1017+
9981018
[Fact]
9991019
public async Task ConditionalExpressionInUnaryExpressionAsync()
10001020
{

0 commit comments

Comments
 (0)