diff --git a/CodeConverter/CSharp/DeclarationNodeVisitor.cs b/CodeConverter/CSharp/DeclarationNodeVisitor.cs index 698190667..a719080d4 100644 --- a/CodeConverter/CSharp/DeclarationNodeVisitor.cs +++ b/CodeConverter/CSharp/DeclarationNodeVisitor.cs @@ -305,7 +305,10 @@ private MemberDeclarationSyntax[] GetAdditionalDeclarations(VBSyntax.StatementSy private async Task ConvertMemberAsync(VBSyntax.StatementSyntax member) { try { - return await member.AcceptAsync(TriviaConvertingDeclarationVisitor); + var sourceTriviaMapKind = member is VBSyntax.PropertyBlockSyntax propBlock && ShouldConvertAsParameterizedProperty(propBlock.PropertyStatement) + ? SourceTriviaMapKind.SubNodesOnly + : SourceTriviaMapKind.All; + return await member.AcceptAsync(TriviaConvertingDeclarationVisitor, sourceTriviaMapKind); } catch (Exception e) { return CreateErrorMember(member, e); } @@ -674,7 +677,7 @@ public override async Task VisitPropertyStatement(VBSyntax.Pro throw new NotImplementedException("MyClass indexing not implemented"); } var methodDeclarationSyntaxs = await propertyBlock.Accessors.SelectAsync(async a => - await a.AcceptAsync(TriviaConvertingDeclarationVisitor, a == propertyBlock.Accessors.First() ? SourceTriviaMapKind.All : SourceTriviaMapKind.None)); + await a.AcceptAsync(TriviaConvertingDeclarationVisitor, SourceTriviaMapKind.All)); var accessorMethods = methodDeclarationSyntaxs.Select(WithMergedModifiers).ToArray(); if (hasExplicitInterfaceImplementation) { @@ -938,7 +941,30 @@ private static AccessorListSyntax ConvertSimpleAccessors(bool isWriteOnly, bool public override async Task VisitPropertyBlock(VBSyntax.PropertyBlockSyntax node) { - return await node.PropertyStatement.AcceptAsync(TriviaConvertingDeclarationVisitor, SourceTriviaMapKind.SubNodesOnly); + var converted = await node.PropertyStatement.AcceptAsync(TriviaConvertingDeclarationVisitor, SourceTriviaMapKind.SubNodesOnly); + + if (converted is MethodDeclarationSyntax) { + var first = (MethodDeclarationSyntax)converted; + + var firstCsConvertedToken = first.GetFirstToken(); + var firstVbSourceToken = node.GetFirstToken(); + first = first.ReplaceToken(firstCsConvertedToken, firstCsConvertedToken.WithSourceMappingFrom(firstVbSourceToken)); + + var members = _additionalDeclarations[node]; + var last = members.OfType().LastOrDefault() ?? first; + var lastIx = members.ToList().IndexOf(last); + var lastIsFirst = lastIx < 0; + var lastCsConvertedToken = last.GetLastToken(); + var lastVbSourceToken = node.GetLastToken(); + last = last.ReplaceToken(lastCsConvertedToken, lastCsConvertedToken.WithSourceMappingFrom(lastVbSourceToken)); + + converted = lastIsFirst ? last : first; + if (!lastIsFirst) { + members[lastIx] = last; + } + } + + return converted; } public override async Task VisitAccessorBlock(VBSyntax.AccessorBlockSyntax node) diff --git a/Tests/CSharp/MemberTests/MemberTests.cs b/Tests/CSharp/MemberTests/MemberTests.cs index 319b7aae9..9abcbb254 100644 --- a/Tests/CSharp/MemberTests/MemberTests.cs +++ b/Tests/CSharp/MemberTests/MemberTests.cs @@ -590,11 +590,9 @@ public virtual int get_RenamedPropertyParam(int i) { return 1; } - public virtual void set_RenamedPropertyParam(int i, int value) { } - int IClass.get_ReadOnlyPropParam(int i) => get_RenamedPropertyParam(i); public virtual int RenamedReadOnlyProperty @@ -614,11 +612,9 @@ public virtual int get_RenamedWriteOnlyPropParam(int i) { return 1; } - public virtual void set_RenamedWriteOnlyPropParam(int i, int value) { } - void IClass.set_WriteOnlyPropParam(int i, int value) => set_RenamedWriteOnlyPropParam(i, value); public virtual int RenamedWriteOnlyProperty @@ -669,7 +665,7 @@ public void set_Prop(int i, string value) _Prop_bSet = false; } -}", incompatibleWithAutomatedCommentTesting: true);// Known bug: Additional declarations don't get comments correctly converted +}"); } [Fact] @@ -871,21 +867,18 @@ public string get_ReadOnlyPropRenamed(int i) { throw new NotImplementedException(); } - string IClass.get_ReadOnlyPropToRename(int i) => get_ReadOnlyPropRenamed(i); public virtual void set_WriteOnlyPropRenamed(int i, string value) { throw new NotImplementedException(); } - void IClass.set_WriteOnlyPropToRename(int i, string value) => set_WriteOnlyPropRenamed(i, value); public virtual string get_PropRenamed(int i) { throw new NotImplementedException(); } - public virtual void set_PropRenamed(int i, string value) { throw new NotImplementedException(); @@ -898,21 +891,18 @@ private string get_ReadOnlyPropNonPublic(int i) { throw new NotImplementedException(); } - string IClass.get_ReadOnlyPropNonPublic(int i) => get_ReadOnlyPropNonPublic(i); protected internal virtual void set_WriteOnlyPropNonPublic(int i, string value) { throw new NotImplementedException(); } - void IClass.set_WriteOnlyPropNonPublic(int i, string value) => set_WriteOnlyPropNonPublic(i, value); internal virtual string get_PropNonPublic(int i) { throw new NotImplementedException(); } - internal virtual void set_PropNonPublic(int i, string value) { throw new NotImplementedException(); @@ -925,21 +915,18 @@ protected internal virtual string get_ReadOnlyPropRenamedNonPublic(int i) { throw new NotImplementedException(); } - string IClass.get_ReadOnlyPropToRenameNonPublic(int i) => get_ReadOnlyPropRenamedNonPublic(i); private void set_WriteOnlyPropRenamedNonPublic(int i, string value) { throw new NotImplementedException(); } - void IClass.set_WriteOnlyPropToRenameNonPublic(int i, string value) => set_WriteOnlyPropRenamedNonPublic(i, value); internal virtual string get_PropToRenameNonPublic(int i) { throw new NotImplementedException(); } - internal virtual void set_PropToRenameNonPublic(int i, string value) { throw new NotImplementedException(); @@ -2436,7 +2423,6 @@ private int get_ExplicitProp(string str = """") { return 5; } - private void set_ExplicitProp(string str = """", int value = default) { } @@ -3111,7 +3097,6 @@ private int get_ExplicitProp(string str) { return 5; } - private void set_ExplicitProp(string str, int value) { } @@ -3164,7 +3149,6 @@ public virtual int get_PropParams(string str) { return 5; } - public virtual void set_PropParams(string str, int value) { } @@ -3787,7 +3771,6 @@ private int get_ExplicitProp(string str) { return 5; } - private void set_ExplicitProp(string str, int value) { } diff --git a/Tests/CSharp/MemberTests/PropertyMemberTests.cs b/Tests/CSharp/MemberTests/PropertyMemberTests.cs index 32938a498..e75b9b09b 100644 --- a/Tests/CSharp/MemberTests/PropertyMemberTests.cs +++ b/Tests/CSharp/MemberTests/PropertyMemberTests.cs @@ -84,7 +84,7 @@ Return LastName & "" "" & FirstName Return FirstName & "" "" & LastName End If End Get - ' Bug: Comment moves inside generated method + ' This comment belongs to the set method Friend Set If isFirst Then FirstName = Value End Set @@ -110,9 +110,8 @@ public string get_FullName(bool lastNameFirst, bool isFirst) { return FirstName + "" "" + LastName; } - // Bug: Comment moves inside generated method } - + // This comment belongs to the set method internal void set_FullName(bool lastNameFirst, bool isFirst, string value) { if (isFirst) @@ -151,7 +150,6 @@ public float get_SomeProp(int index) { return 1.5f; } - public void set_SomeProp(int index, float value) { } @@ -176,7 +174,7 @@ Public Property FullName(Optional ByVal isFirst As Boolean = False) As String Get Return FirstName & "" "" & LastName End Get -'Bug: Comment moves inside generated get method +'This comment belongs to the set method Friend Set If isFirst Then FirstName = Value End Set @@ -197,9 +195,8 @@ internal partial class TestClass public string get_FullName(bool isFirst = false) { return FirstName + "" "" + LastName; - // Bug: Comment moves inside generated get method } - + // This comment belongs to the set method internal void set_FullName(bool isFirst = false, string value = default) { if (isFirst) @@ -259,7 +256,6 @@ public string get_MyProp(int blah) { return blah.ToString(); } - public void set_MyProp(int blah, string value) { } @@ -288,6 +284,57 @@ public void ReturnWhatever(MyEnum m) }"); } + [Fact] + public async Task TestParameterizedPropertyWithTriviaAsync() + { + //issue 1095 + await TestConversionVisualBasicToCSharpAsync( + @"Class IndexedPropertyWithTrivia + 'a + Property P(i As Integer) As Integer + 'b + Get + '1 + Dim x = 1 '2 + '3 + End Get + + 'c + Set(value As Integer) + '4 + Dim x = 1 '5 + '6 + x = value + i '7 + '8 + End Set + 'd + End Property +End Class", @" +internal partial class IndexedPropertyWithTrivia +{ + // a + // b + public int get_P(int i) + { + // 1 + int x = 1; // 2 + return default; + // 3 + } + + // c + public void set_P(int i, int value) + { + // 4 + int x = 1; // 5 + // 6 + x = value + i; // 7 + // 8 + // d + } +}"); + } + [Fact] public async Task PropertyWithMissingTypeDeclarationAsync()//TODO Check object is the inferred type { @@ -520,7 +567,6 @@ internal int get_Prop2(int x = 1, int y = 2) { return default; } - internal void set_Prop2(int x = 1, int y = 2, int value = default) { } @@ -610,7 +656,6 @@ internal int get_Prop2(int x = 1, int y = 2, int z = 3) { return default; } - internal void set_Prop2(int x = 1, int y = 2, int z = 3, int value = default) { } diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs index 29ee4992f..1a4f092f7 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertVbLibraryOnly/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs @@ -27,7 +27,6 @@ public static string get_Value(IEnumerable source) return item.Value; return null; } - public static void set_Value(IEnumerable source, string value) { foreach (XElement item in source) @@ -42,7 +41,6 @@ public static string get_AttributeValue(IEnumerable source, XName name return (string)item.Attribute(name); return null; } - public static void set_AttributeValue(IEnumerable source, XName name, string value) { foreach (XElement item in source) @@ -55,7 +53,6 @@ public static string get_AttributeValue(XElement source, XName name) { return (string)source.Attribute(name); } - public static void set_AttributeValue(XElement source, XName name, string value) { source.SetAttributeValue(name, value); diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp1/My Project/MyNamespace.Static.2.Designer.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp1/My Project/MyNamespace.Static.2.Designer.cs index 9a517d23a..05c0af1bc 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp1/My Project/MyNamespace.Static.2.Designer.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp1/My Project/MyNamespace.Static.2.Designer.cs @@ -28,7 +28,6 @@ public static string get_Value(IEnumerable source) return item.Value; return null; } - public static void set_Value(IEnumerable source, string value) { foreach (XElement item in source) @@ -43,7 +42,6 @@ public static string get_AttributeValue(IEnumerable source, XName name return (string)item.Attribute(name); return null; } - public static void set_AttributeValue(IEnumerable source, XName name, string value) { foreach (XElement item in source) @@ -56,7 +54,6 @@ public static string get_AttributeValue(XElement source, XName name) { return (string)source.Attribute(name); } - public static void set_AttributeValue(XElement source, XName name, string value) { source.SetAttributeValue(name, value); diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp4/My Project/MyNamespace.Static.2.Designer.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp4/My Project/MyNamespace.Static.2.Designer.cs index 7599da928..b41027574 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp4/My Project/MyNamespace.Static.2.Designer.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/ConsoleApp4/My Project/MyNamespace.Static.2.Designer.cs @@ -27,7 +27,6 @@ public static string get_Value(IEnumerable source) return item.Value; return null; } - public static void set_Value(IEnumerable source, string value) { foreach (XElement item in source) @@ -42,7 +41,6 @@ public static string get_AttributeValue(IEnumerable source, XName name return (string)item.Attribute(name); return null; } - public static void set_AttributeValue(IEnumerable source, XName name, string value) { foreach (XElement item in source) @@ -55,7 +53,6 @@ public static string get_AttributeValue(XElement source, XName name) { return (string)source.Attribute(name); } - public static void set_AttributeValue(XElement source, XName name, string value) { source.SetAttributeValue(name, value); diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/My Project/MyNamespace.Static.2.Designer.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/My Project/MyNamespace.Static.2.Designer.cs index ad6dbce36..1eafdfc6b 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/My Project/MyNamespace.Static.2.Designer.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/Prefix.VbLibrary/My Project/MyNamespace.Static.2.Designer.cs @@ -28,7 +28,6 @@ public static string get_Value(IEnumerable source) return item.Value; return null; } - public static void set_Value(IEnumerable source, string value) { foreach (XElement item in source) @@ -43,7 +42,6 @@ public static string get_AttributeValue(IEnumerable source, XName name return (string)item.Attribute(name); return null; } - public static void set_AttributeValue(IEnumerable source, XName name, string value) { foreach (XElement item in source) @@ -56,7 +54,6 @@ public static string get_AttributeValue(XElement source, XName name) { return (string)source.Attribute(name); } - public static void set_AttributeValue(XElement source, XName name, string value) { source.SetAttributeValue(name, value); diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs index 29ee4992f..1a4f092f7 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbLibrary/My Project/MyNamespace.Static.3.Designer.cs @@ -27,7 +27,6 @@ public static string get_Value(IEnumerable source) return item.Value; return null; } - public static void set_Value(IEnumerable source, string value) { foreach (XElement item in source) @@ -42,7 +41,6 @@ public static string get_AttributeValue(IEnumerable source, XName name return (string)item.Attribute(name); return null; } - public static void set_AttributeValue(IEnumerable source, XName name, string value) { foreach (XElement item in source) @@ -55,7 +53,6 @@ public static string get_AttributeValue(XElement source, XName name) { return (string)source.Attribute(name); } - public static void set_AttributeValue(XElement source, XName name, string value) { source.SetAttributeValue(name, value); diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbNetStandardLib/My Project/MyNamespace.Static.3.Designer.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbNetStandardLib/My Project/MyNamespace.Static.3.Designer.cs index feb85f04c..f178d034f 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbNetStandardLib/My Project/MyNamespace.Static.3.Designer.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/VbNetStandardLib/My Project/MyNamespace.Static.3.Designer.cs @@ -27,7 +27,6 @@ public static string get_Value(IEnumerable source) return item.Value; return null; } - public static void set_Value(IEnumerable source, string value) { foreach (XElement item in source) @@ -42,7 +41,6 @@ public static string get_AttributeValue(IEnumerable source, XName name return (string)item.Attribute(name); return null; } - public static void set_AttributeValue(IEnumerable source, XName name, string value) { foreach (XElement item in source) @@ -55,7 +53,6 @@ public static string get_AttributeValue(XElement source, XName name) { return (string)source.Attribute(name); } - public static void set_AttributeValue(XElement source, XName name, string value) { source.SetAttributeValue(name, value); diff --git a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/WindowsAppVb/My Project/MyNamespace.Static.2.Designer.cs b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/WindowsAppVb/My Project/MyNamespace.Static.2.Designer.cs index 281d52945..b53428969 100644 --- a/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/WindowsAppVb/My Project/MyNamespace.Static.2.Designer.cs +++ b/Tests/TestData/MultiFileCharacterization/VBToCSResults/ConvertWholeSolution/WindowsAppVb/My Project/MyNamespace.Static.2.Designer.cs @@ -28,7 +28,6 @@ public static string get_Value(IEnumerable source) return item.Value; return null; } - public static void set_Value(IEnumerable source, string value) { foreach (XElement item in source) @@ -43,7 +42,6 @@ public static string get_AttributeValue(IEnumerable source, XName name return (string)item.Attribute(name); return null; } - public static void set_AttributeValue(IEnumerable source, XName name, string value) { foreach (XElement item in source) @@ -56,7 +54,6 @@ public static string get_AttributeValue(XElement source, XName name) { return (string)source.Attribute(name); } - public static void set_AttributeValue(XElement source, XName name, string value) { source.SetAttributeValue(name, value);