Skip to content

Commit 8027c8a

Browse files
Fix lambda block unpacking with comments
When a single statement lambda contained comments, unpacking it to an expression body or an unparenthesized block resulted in the comments being pushed outside the lambda when it's placed inside method invocations like `Parallel.ForEach`. This checks for comments in the lambda node and forces generating a full block syntax to preserve the comments inside. Co-authored-by: GrahamTheCoder <[email protected]>
1 parent 2c2b64e commit 8027c8a

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

CodeConverter/CSharp/LambdaConverter.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public async Task<CSharpSyntaxNode> ConvertAsync(VBSyntax.LambdaExpressionSyntax
7575
BlockSyntax block = null;
7676
ExpressionSyntax expressionBody = null;
7777
ArrowExpressionClauseSyntax arrow = null;
78-
if (!convertedStatements.TryUnpackSingleStatement(out StatementSyntax singleStatement)) {
78+
bool hasComments = vbNode.DescendantTrivia().Any(t => t.IsKind(VBasic.SyntaxKind.CommentTrivia));
79+
if (hasComments || !convertedStatements.TryUnpackSingleStatement(out StatementSyntax singleStatement)) {
7980
convertedStatements = convertedStatements.Select(l => l.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed)).ToList();
8081
block = SyntaxFactory.Block(convertedStatements);
8182
} else if (singleStatement.TryUnpackSingleExpressionFromStatement(out expressionBody)) {

Tests/CSharp/ExpressionTests/ExpressionTests.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2931,4 +2931,50 @@ public object Edit(bool flag2 = false, CrashEnum? crashEnum = default)
29312931
}
29322932
}");
29332933
}
2934-
}
2934+
[Fact]
2935+
public async Task LambdaBodyExpressionWithCommentsAsync() {
2936+
await TestConversionVisualBasicToCSharpAsync(@"
2937+
Imports System.Threading.Tasks
2938+
Imports System.Collections.Generic
2939+
2940+
Public Class ConversionTest1
2941+
Public Sub BugRepro()
2942+
Dim dt As New List(Of Integer)
2943+
2944+
Parallel.ForEach(dt, Sub(row)
2945+
If Not String.IsNullOrWhiteSpace("""") Then
2946+
'comment1
2947+
Dim test1 As Boolean = True
2948+
'comment2
2949+
Dim test2 As Boolean = True
2950+
'comment3
2951+
Dim test3 As Boolean = True
2952+
End If
2953+
End Sub)
2954+
End Sub
2955+
End Class
2956+
", @"using System.Collections.Generic;
2957+
using System.Threading.Tasks;
2958+
2959+
public partial class ConversionTest1
2960+
{
2961+
public void BugRepro()
2962+
{
2963+
var dt = new List<int>();
2964+
2965+
Parallel.ForEach(dt, row =>
2966+
{
2967+
if (!string.IsNullOrWhiteSpace(""""))
2968+
{
2969+
// comment1
2970+
bool test1 = true;
2971+
// comment2
2972+
bool test2 = true;
2973+
// comment3
2974+
bool test3 = true;
2975+
}
2976+
});
2977+
}
2978+
}", incompatibleWithAutomatedCommentTesting: true);
2979+
}
2980+
}

0 commit comments

Comments
 (0)