From e0a36d1bdf1fdc1c51211617426b9e02b0f1c8e3 Mon Sep 17 00:00:00 2001 From: GrahamTheCoder Date: Sun, 29 Sep 2024 14:38:13 +0100 Subject: [PATCH] Escape character -> string conversions - fixes #1135 --- CHANGELOG.md | 1 + .../CSharp/TypeConversionAnalyzer.cs | 2 +- .../ExpressionTests/StringExpressionTests.cs | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c21352a1f..6424b9b52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/CodeConverter/CSharp/TypeConversionAnalyzer.cs b/CodeConverter/CSharp/TypeConversionAnalyzer.cs index a354e2748..3da81d0eb 100644 --- a/CodeConverter/CSharp/TypeConversionAnalyzer.cs +++ b/CodeConverter/CSharp/TypeConversionAnalyzer.cs @@ -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()); } diff --git a/Tests/CSharp/ExpressionTests/StringExpressionTests.cs b/Tests/CSharp/ExpressionTests/StringExpressionTests.cs index c32b34a42..73e07b5d6 100644 --- a/Tests/CSharp/ExpressionTests/StringExpressionTests.cs +++ b/Tests/CSharp/ExpressionTests/StringExpressionTests.cs @@ -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() {