diff --git a/CodeConverter/CSharp/LambdaConverter.cs b/CodeConverter/CSharp/LambdaConverter.cs index f1306f5b..1be81c65 100644 --- a/CodeConverter/CSharp/LambdaConverter.cs +++ b/CodeConverter/CSharp/LambdaConverter.cs @@ -75,7 +75,8 @@ public async Task ConvertAsync(VBSyntax.LambdaExpressionSyntax BlockSyntax block = null; ExpressionSyntax expressionBody = null; ArrowExpressionClauseSyntax arrow = null; - if (!convertedStatements.TryUnpackSingleStatement(out StatementSyntax singleStatement)) { + bool hasComments = vbNode.DescendantTrivia().Any(t => t.IsKind(VBasic.SyntaxKind.CommentTrivia)); + if (hasComments || !convertedStatements.TryUnpackSingleStatement(out StatementSyntax singleStatement)) { convertedStatements = convertedStatements.Select(l => l.WithTrailingTrivia(SyntaxFactory.ElasticCarriageReturnLineFeed)).ToList(); block = SyntaxFactory.Block(convertedStatements); } else if (singleStatement.TryUnpackSingleExpressionFromStatement(out expressionBody)) { diff --git a/Tests/CSharp/ExpressionTests/ExpressionTests.cs b/Tests/CSharp/ExpressionTests/ExpressionTests.cs index 2f7b7877..64727cbc 100644 --- a/Tests/CSharp/ExpressionTests/ExpressionTests.cs +++ b/Tests/CSharp/ExpressionTests/ExpressionTests.cs @@ -2931,4 +2931,50 @@ public object Edit(bool flag2 = false, CrashEnum? crashEnum = default) } }"); } -} \ No newline at end of file + [Fact] + public async Task LambdaBodyExpressionWithCommentsAsync() { + await TestConversionVisualBasicToCSharpAsync(@" +Imports System.Threading.Tasks +Imports System.Collections.Generic + +Public Class ConversionTest1 + Public Sub BugRepro() + Dim dt As New List(Of Integer) + + Parallel.ForEach(dt, Sub(row) + If Not String.IsNullOrWhiteSpace("""") Then + 'comment1 + Dim test1 As Boolean = True + 'comment2 + Dim test2 As Boolean = True + 'comment3 + Dim test3 As Boolean = True + End If + End Sub) + End Sub +End Class +", @"using System.Collections.Generic; +using System.Threading.Tasks; + +public partial class ConversionTest1 +{ + public void BugRepro() + { + var dt = new List(); + + Parallel.ForEach(dt, row => + { + if (!string.IsNullOrWhiteSpace("""")) + { + // comment1 + bool test1 = true; + // comment2 + bool test2 = true; + // comment3 + bool test3 = true; + } + }); + } +}", incompatibleWithAutomatedCommentTesting: true); + } +}