Skip to content

Commit

Permalink
Escape character -> string conversions - fixes #1135
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Sep 29, 2024
1 parent 625c207 commit e0a36d1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### VB -> C#

* Escape character -> string conversions [#1135](https://github.com/icsharpcode/CodeConverter/issues/1135)
* Convert Not(x IsNot Nothing) to x is null [#1113](https://github.com/icsharpcode/CodeConverter/issues/1113)
* Escape parameter names [#1092](https://github.com/icsharpcode/CodeConverter/issues/1092)

Expand Down
2 changes: 1 addition & 1 deletion CodeConverter/CSharp/TypeConversionAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ private static ExpressionSyntax GetToStringConversionOrNull(ExpressionSyntax csN
}

if (currentType is {SpecialType: SpecialType.System_Char} && csNode is CSSyntax.LiteralExpressionSyntax {Token: {} t} l) {
return SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Token(t.LeadingTrivia, SyntaxKind.StringLiteralToken, "\"" + t.Text.Trim('\'') + "\"", t.ValueText, t.TrailingTrivia))
return LiteralConversions.GetLiteralExpression(t.Value?.ToString(), convertedType: targetType)
.WithLeadingTrivia(csNode.GetLeadingTrivia()).WithTrailingTrivia(csNode.GetTrailingTrivia());
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/CSharp/ExpressionTests/StringExpressionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,25 @@ private void TestMethod()
}");
}

[Fact]
public async Task QuoteCharacterAsync()
{
await TestConversionVisualBasicToCSharpAsync(@"Public Class C
Public Sub s
Dim x As String = Chr(34)
x = Chr(92)
End Sub
End Class", @"
public partial class C
{
public void s()
{
string x = ""\"""";
x = @""\"";
}
}");
}

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

0 comments on commit e0a36d1

Please sign in to comment.