From 0647b8d2056b1a7d0fc110290caff50b55cdc3f3 Mon Sep 17 00:00:00 2001 From: GrahamTheCoder Date: Sun, 10 Dec 2023 10:32:53 +0000 Subject: [PATCH] Remove square brackets when escaping labels and identifiers - fixes #1043, fixes #1044 --- CHANGELOG.md | 1 + CodeConverter/CSharp/CommonConversions.cs | 1 + .../MethodBodyExecutableStatementVisitor.cs | 4 +- .../CSharp/ExpressionTests/ExpressionTests.cs | 42 +++++++++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e7b548a7f..1175e8516 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### VB -> C# +* Remove square brackets when escaping labels and identifiers [#1043](https://github.com/icsharpcode/CodeConverter/issues/1043) and [#1044](https://github.com/icsharpcode/CodeConverter/issues/1044) * Exit Property now returns value assigned to return variable [#1051](https://github.com/icsharpcode/CodeConverter/issues/1051) * Avoid stack overflow for very deeply nested binary expressions [#1033](https://github.com/icsharpcode/CodeConverter/issues/1033) * Omit special VB conversions within expression trees [#930](https://github.com/icsharpcode/CodeConverter/issues/930) [#316](https://github.com/icsharpcode/CodeConverter/issues/316) diff --git a/CodeConverter/CSharp/CommonConversions.cs b/CodeConverter/CSharp/CommonConversions.cs index f1e7229ff..440c829bf 100644 --- a/CodeConverter/CSharp/CommonConversions.cs +++ b/CodeConverter/CSharp/CommonConversions.cs @@ -341,6 +341,7 @@ private static string WithDeclarationName(SyntaxToken id, ISymbol idSymbol, stri public static SyntaxToken CsEscapedIdentifier(string text) { + text = text.TrimStart('[').TrimEnd(']'); if (SyntaxFacts.GetKeywordKind(text) != CSSyntaxKind.None) text = "@" + text; return SyntaxFactory.Identifier(text); } diff --git a/CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs b/CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs index d1d2f93eb..6dc881f01 100644 --- a/CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs +++ b/CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs @@ -727,13 +727,13 @@ public override async Task> VisitForEachBlock(VBSynt public override async Task> VisitLabelStatement(VBSyntax.LabelStatementSyntax node) { - return SingleStatement(SyntaxFactory.LabeledStatement(node.LabelToken.Text, SyntaxFactory.EmptyStatement())); + return SingleStatement(SyntaxFactory.LabeledStatement(CommonConversions.CsEscapedIdentifier(node.LabelToken.Text), SyntaxFactory.EmptyStatement())); } public override async Task> VisitGoToStatement(VBSyntax.GoToStatementSyntax node) { return SingleStatement(SyntaxFactory.GotoStatement(SyntaxKind.GotoStatement, - SyntaxFactory.IdentifierName(node.Label.LabelToken.Text))); + SyntaxFactory.IdentifierName(CommonConversions.CsEscapedIdentifier(node.Label.LabelToken.Text)))); } public override async Task> VisitSelectBlock(VBSyntax.SelectBlockSyntax node) diff --git a/Tests/CSharp/ExpressionTests/ExpressionTests.cs b/Tests/CSharp/ExpressionTests/ExpressionTests.cs index 7ecd4a9bd..b02319cbb 100644 --- a/Tests/CSharp/ExpressionTests/ExpressionTests.cs +++ b/Tests/CSharp/ExpressionTests/ExpressionTests.cs @@ -2490,6 +2490,48 @@ public void Operators() }"); } + [Fact] + public async Task SquareBracketsInLabelAsync() + { + await TestConversionVisualBasicToCSharpAsync(@" +Sub S() + GoTo [finally] +[finally]: + GoTo [Step] +[Step]: +End Sub", + @" +public void S() +{ + goto @finally; +@finally: + ; + + goto Step; +Step: + ; + +}"); + } + + [Fact] + public async Task SquareBracketsInIdentifierAsync() + { + await TestConversionVisualBasicToCSharpAsync(@" + Sub [Step]() + Static i As Integer + End Sub", + @" +internal partial class SurroundingClass +{ + private int _[Step]_i; + + public void Step() + { + } +}"); + } + [Fact] public async Task CintIsConvertedCorrectlyAsync() {