Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions CodeConverter/CSharp/Extensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace ICSharpCode.CodeConverter.CSharp;

Expand All @@ -12,7 +13,7 @@ internal static class Extensions
/// </summary>
public static StatementSyntax UnpackNonNestedBlock(this BlockSyntax block)
{
return block.Statements.Count == 1 && !block.ContainsNestedStatements() ? block.Statements[0] : block;
return block.ContainsNestedStatements() ? block : block.UnpackPossiblyNestedBlock();
}

/// <summary>
Expand Down Expand Up @@ -56,7 +57,16 @@ public static bool TryUnpackSingleExpressionFromStatement(this StatementSyntax s
public static StatementSyntax UnpackPossiblyNestedBlock(this BlockSyntax block)
{
SyntaxList<StatementSyntax> statementSyntaxs = block.Statements;
return statementSyntaxs.Count == 1 ? statementSyntaxs[0] : block;
return statementSyntaxs.Count == 1 && statementSyntaxs[0].CanBeEmbedded() ? statementSyntaxs[0] : block;
}

/// <summary>
/// C# forbids declarations and labels as the embedded statement of if/else/while/do/for/foreach/lock/using (CS1023),
/// see BindPossibleEmbeddedStatement in https://github.com/dotnet/roslyn/blob/main/src/Compilers/CSharp/Portable/Binder/Binder_Statements.cs
/// </summary>
public static bool CanBeEmbedded(this StatementSyntax statement)
{
return !statement.IsKind(SyntaxKind.LocalDeclarationStatement, SyntaxKind.LabeledStatement, SyntaxKind.LocalFunctionStatement);
}

private static bool ContainsNestedStatements(this BlockSyntax block)
Expand Down
222 changes: 221 additions & 1 deletion Tests/CSharp/StatementTests/LoopStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -812,4 +812,224 @@ private void TestMethod(bool someCondition)
}
}");
}
}

[Fact]
public async Task WhileWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a As Boolean)
While a
Dim z = F()
End While
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(bool a)
{
while (a)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task DoWhileWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a As Boolean)
Do While a
Dim z = F()
Loop
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(bool a)
{
while (a)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task DoUntilWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a As Boolean)
Do Until a
Dim z = F()
Loop
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(bool a)
{
while (!a)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task DoLoopWhileWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a As Boolean)
Do
Dim z = F()
Loop While a
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(bool a)
{
do
{
int z = F();
}
while (a);
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task ForWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod()
For i = 1 To 2
Dim z = F()
Next
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod()
{
for (int i = 1; i <= 2; i++)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task ForEachWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(xs As Integer())
For Each x In xs
Dim z = F()
Next
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(int[] xs)
{
foreach (var x in xs)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task WhileWithLoneLabelKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a As Boolean)
While a
lbl:
End While
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(bool a)
{
while (a)
{
lbl:
;

}
}

private int F()
{
return 1;
}
}");
}
}
123 changes: 123 additions & 0 deletions Tests/CSharp/StatementTests/MethodStatementTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1861,6 +1861,129 @@ public void AllAssignmentOperators()
set_Item(0, get_Item(0) >> 2);
set_StrItem(0, get_StrItem(0) + "" World"");
}
}");
}

[Fact]
public async Task SingleLineIfWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a As Boolean)
If a Then Dim z = F()
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(bool a)
{
if (a)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task SingleLineIfElseWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(a As Boolean)
If a Then F() Else Dim z = F()
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(bool a)
{
if (a)
F();
else
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task SyncLockWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(o As Object)
SyncLock o
Dim z = F()
End SyncLock
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"
internal partial class TestClass
{
private void TestMethod(object o)
{
lock (o)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}

[Fact]
public async Task UsingExpressionWithLoneLocalDeclarationKeepsBracesAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Class TestClass
Private Sub TestMethod(d As System.IDisposable)
Using d
Dim z = F()
End Using
End Sub

Private Function F() As Integer
Return 1
End Function
End Class", @"using System;

internal partial class TestClass
{
private void TestMethod(IDisposable d)
{
using (d)
{
int z = F();
}
}

private int F()
{
return 1;
}
}");
}
}
Loading
Loading