From aabbc7b84355dcefb6639ecefd51e04c9bba1658 Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Fri, 25 Aug 2023 10:43:35 +0200 Subject: [PATCH 1/6] add test for a non-VB-Chr Method --- Tests/CSharp/SpecialConversionTests.cs | 69 +++++++++++++++++++++++++- 1 file changed, 68 insertions(+), 1 deletion(-) diff --git a/Tests/CSharp/SpecialConversionTests.cs b/Tests/CSharp/SpecialConversionTests.cs index 436aa423f..f6521d81c 100644 --- a/Tests/CSharp/SpecialConversionTests.cs +++ b/Tests/CSharp/SpecialConversionTests.cs @@ -277,6 +277,73 @@ public void Test(byte b) }"); } + [Fact] + public async Task TestNonVisualBasicChrMethodConversionsAsync() + { + await TestConversionVisualBasicToCSharpAsync(@" +Class TestConversions + Sub Test() + Dim a As String + a = Chr(2) + a = Me.Chr(2) + a = Strings.Chr(2) + a = Microsoft.VisualBasic.Strings.Chr(2) + a = Microsoft.VisualBasic.Chr(2) + End Sub + + Sub TestW() + Dim a As String + a = ChrW(2) + a = Me.ChrW(2) + a = Strings.ChrW(2) + a = Microsoft.VisualBasic.Strings.ChrW(2) + a = Microsoft.VisualBasic.ChrW(2) + End Sub + + Function Chr(o As Object) As Char + Return Microsoft.VisualBasic.Chr(o) + End Function + + Function ChrW(o As Object) As Char + Return Microsoft.VisualBasic.ChrW(o) + End Function +End Class", @"using Microsoft.VisualBasic; // Install-Package Microsoft.VisualBasic +using Microsoft.VisualBasic.CompilerServices; // Install-Package Microsoft.VisualBasic + +internal partial class TestConversions +{ + public void Test() + { + string a; + a = Conversions.ToString(Chr(2)); + a = Conversions.ToString(Chr(2)); + a = Conversions.ToString('\u0002'); + a = Conversions.ToString('\u0002'); + a = Conversions.ToString('\u0002'); + } + + public void TestW() + { + string a; + a = Conversions.ToString(ChrW(2)); + a = Conversions.ToString(ChrW(2)); + a = Conversions.ToString('\u0002'); + a = Conversions.ToString('\u0002'); + a = Conversions.ToString('\u0002'); + } + + public char Chr(object o) + { + return Strings.Chr(Conversions.ToInteger(o)); + } + + public char ChrW(object o) + { + return Strings.ChrW(Conversions.ToInteger(o)); + } +}"); + } + [Fact] public async Task UsingBoolInToExpressionAsync() { @@ -314,4 +381,4 @@ End If } }"); } -} \ No newline at end of file +} From 97801aef239ed03ca3b8534b7c27507ff00c1214 Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Fri, 25 Aug 2023 10:46:18 +0200 Subject: [PATCH 2/6] Fix VB.Chr method recognition --- CodeConverter/CSharp/ExpressionNodeVisitor.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/CodeConverter/CSharp/ExpressionNodeVisitor.cs b/CodeConverter/CSharp/ExpressionNodeVisitor.cs index c9026526c..2ae538ef7 100644 --- a/CodeConverter/CSharp/ExpressionNodeVisitor.cs +++ b/CodeConverter/CSharp/ExpressionNodeVisitor.cs @@ -1904,10 +1904,10 @@ private ArgumentListSyntax CreateArgList(ISymbol invocationSymbol) private async Task SubstituteVisualBasicMethodOrNullAsync(VBSyntax.InvocationExpressionSyntax node, ISymbol symbol) { ExpressionSyntax cSharpSyntaxNode = null; - if (symbol?.ContainingNamespace.MetadataName == nameof(Microsoft.VisualBasic) && symbol.Name == "ChrW" || symbol?.Name == "Chr") { + if (IsVisualBasicChrMethod(symbol)) { var vbArg = node.ArgumentList.Arguments.Single().GetExpression(); var constValue = _semanticModel.GetConstantValue(vbArg); - if (IsCultureInvariant(symbol, constValue)) { + if (IsCultureInvariant(constValue)) { var csArg = await vbArg.AcceptAsync(TriviaConvertingExpressionVisitor); cSharpSyntaxNode = CommonConversions.TypeConversionAnalyzer.AddExplicitConversion(node, csArg, true, true, true, forceTargetType: _semanticModel.GetTypeInfo(node).Type); } @@ -1928,11 +1928,16 @@ private static bool RequiresStringCompareMethodToBeAppended(ISymbol symbol) => symbol.ContainingType.ContainingNamespace.ContainingNamespace.Name == nameof(Microsoft) && symbol.Name is "InStr" or "InStrRev" or "Replace" or "Split" or "StrComp"; + private static bool IsVisualBasicChrMethod(ISymbol symbol) => + symbol is not null + && symbol.ContainingNamespace.MetadataName == nameof(Microsoft.VisualBasic) + && (symbol.Name == "ChrW" || symbol.Name == "Chr"); + /// /// https://github.com/icsharpcode/CodeConverter/issues/745 /// - private static bool IsCultureInvariant(ISymbol symbol, Optional constValue) => - (symbol.Name == "ChrW" || symbol.Name == "Chr") && constValue.HasValue && Convert.ToUInt64(constValue.Value, CultureInfo.InvariantCulture) <= 127; + private static bool IsCultureInvariant(Optional constValue) => + constValue.HasValue && Convert.ToUInt64(constValue.Value, CultureInfo.InvariantCulture) <= 127; private CSharpSyntaxNode AddEmptyArgumentListIfImplicit(SyntaxNode node, ExpressionSyntax id) { @@ -2006,4 +2011,4 @@ private TSymbol GetSymbolInfoInDocument(SyntaxNode node) where TSymbol: { return _semanticModel.SyntaxTree == node.SyntaxTree ? _semanticModel.GetSymbolInfo(node).ExtractBestMatch(): null; } -} \ No newline at end of file +} From a05185ea2086d9bb5e9c8045a8291282ba2f32d3 Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Fri, 25 Aug 2023 10:55:24 +0200 Subject: [PATCH 3/6] remove trailing NewLine in SpecialConversionTests.cs From 89759d25cfde7ba7bad49ff6399ae060e35746af Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Fri, 25 Aug 2023 10:57:33 +0200 Subject: [PATCH 4/6] remove EOL From 8265bc6ce189274ea9ec80539e8528ffaa41ff33 Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Fri, 25 Aug 2023 10:58:38 +0200 Subject: [PATCH 5/6] remove EOL From 9ebcb26f11953e8ad2270793409c11bc9df86663 Mon Sep 17 00:00:00 2001 From: Timur Kelman Date: Fri, 25 Aug 2023 11:01:10 +0200 Subject: [PATCH 6/6] really fix the EOL at EOF this time I hope --- CodeConverter/CSharp/ExpressionNodeVisitor.cs | 2 +- Tests/CSharp/SpecialConversionTests.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CodeConverter/CSharp/ExpressionNodeVisitor.cs b/CodeConverter/CSharp/ExpressionNodeVisitor.cs index 2ae538ef7..d6a29023a 100644 --- a/CodeConverter/CSharp/ExpressionNodeVisitor.cs +++ b/CodeConverter/CSharp/ExpressionNodeVisitor.cs @@ -2011,4 +2011,4 @@ private TSymbol GetSymbolInfoInDocument(SyntaxNode node) where TSymbol: { return _semanticModel.SyntaxTree == node.SyntaxTree ? _semanticModel.GetSymbolInfo(node).ExtractBestMatch(): null; } -} +} \ No newline at end of file diff --git a/Tests/CSharp/SpecialConversionTests.cs b/Tests/CSharp/SpecialConversionTests.cs index f6521d81c..d858bbc9d 100644 --- a/Tests/CSharp/SpecialConversionTests.cs +++ b/Tests/CSharp/SpecialConversionTests.cs @@ -381,4 +381,4 @@ End If } }"); } -} +} \ No newline at end of file