From c73104b41086202d90f0e9edfa1e0f9d8e0620d9 Mon Sep 17 00:00:00 2001 From: John Moreno Date: Sat, 26 Jul 2025 21:07:24 -0400 Subject: [PATCH] VB -> C#: Brackets needed around ternary expression - Added unit test to AccessExpressionTest.cs - CommonConversions.cs - use SyntaxFactory.ParenthesizedExpression to ParenthesizedExpression the converted expression if it is a ConditionalExpressionSyntax - Ran all test, no change in existing test, new test passes after initially failing. #1170 --- CodeConverter/CSharp/CommonConversions.cs | 4 ++++ .../ExpressionTests/AccessExpressionTests.cs | 20 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/CodeConverter/CSharp/CommonConversions.cs b/CodeConverter/CSharp/CommonConversions.cs index 28af37137..60b10caa5 100644 --- a/CodeConverter/CSharp/CommonConversions.cs +++ b/CodeConverter/CSharp/CommonConversions.cs @@ -523,6 +523,10 @@ private async Task IncreaseArrayUpperBoundExpressionAsync(VBSy && convertedExpression.SkipIntoParens() is CSSyntax.BinaryExpressionSyntax bExp && bExp.IsKind(CSSyntaxKind.SubtractExpression)) return bExp.Left; + if (convertedExpression is CSSyntax.ConditionalExpressionSyntax ce) { + convertedExpression = SyntaxFactory.ParenthesizedExpression(convertedExpression); + } + return SyntaxFactory.BinaryExpression( CSSyntaxKind.SubtractExpression, convertedExpression, SyntaxFactory.Token(CSSyntaxKind.PlusToken), SyntaxFactory.LiteralExpression(CSSyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal(1))); diff --git a/Tests/CSharp/ExpressionTests/AccessExpressionTests.cs b/Tests/CSharp/ExpressionTests/AccessExpressionTests.cs index 5a6db9f87..8449f61f8 100644 --- a/Tests/CSharp/ExpressionTests/AccessExpressionTests.cs +++ b/Tests/CSharp/ExpressionTests/AccessExpressionTests.cs @@ -209,6 +209,26 @@ public string[][] GetStringsFromAmbiguous(int amb) }"); } + [Fact] + public async Task TernaryArrayIndexerAsync() + { + await TestConversionVisualBasicToCSharpAsync(@"Public Class A + Public Sub Test() + Dim i as integer = 0 + Dim a(If(i = 1, 2, 3)) as string + End Sub +End Class", @" +public partial class A +{ + public void Test() + { + int i = 0; + var a = new string[(i == 1 ? 2 : 3) + 1]; + } +}"); + } + + [Fact] public async Task ElementAtOrDefaultIndexingAsync() {