Skip to content

Commit

Permalink
Remove square brackets when escaping labels and identifiers - fixes #…
Browse files Browse the repository at this point in the history
…1043, fixes #1044
  • Loading branch information
GrahamTheCoder committed Dec 10, 2023
1 parent f7283cc commit 0647b8d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions CodeConverter/CSharp/CommonConversions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions CodeConverter/CSharp/MethodBodyExecutableStatementVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,13 +727,13 @@ public override async Task<SyntaxList<StatementSyntax>> VisitForEachBlock(VBSynt

public override async Task<SyntaxList<StatementSyntax>> 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<SyntaxList<StatementSyntax>> 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<SyntaxList<StatementSyntax>> VisitSelectBlock(VBSyntax.SelectBlockSyntax node)
Expand Down
42 changes: 42 additions & 0 deletions Tests/CSharp/ExpressionTests/ExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

This comment has been minimized.

Copy link
@TymurGubayev

TymurGubayev Dec 12, 2023

Contributor

_[Step]_i is not a valid C#-identifier

public void Step()
{
}
}");
}

[Fact]
public async Task CintIsConvertedCorrectlyAsync()
{
Expand Down

0 comments on commit 0647b8d

Please sign in to comment.