Skip to content

Commit 3284c3d

Browse files
Merge pull request #1069 from icsharpcode/issue/1062
Issue/1062
2 parents ee8e18f + 970410d commit 3284c3d

6 files changed

Lines changed: 208 additions & 103 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1616
* Constant chars are converted to constant strings where needed
1717
* Select case for a mixture of strings and characters converts correctly [#1062](https://github.com/icsharpcode/CodeConverter/issues/1062)
1818

19-
2019
### C# -> VB
2120

2221

CodeConverter/CSharp/DeclarationNodeVisitor.cs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ private void AddRemainingInterfaceDeclarations(MethodDeclarationSyntax method, S
818818
GetMethodId(interfaceImplement.Name) :
819819
SetMethodId(interfaceImplement.Name));
820820
var interfaceMethodDeclParams = new MethodDeclarationParameters(attributes, filteredModifiers,
821-
method.ReturnType, method.TypeParameterList, MakeOptionalParametersRequired(method.ParameterList), method.ConstraintClauses, clause, identifier);
821+
method.ReturnType, method.TypeParameterList, method.ParameterList, method.ConstraintClauses, clause, identifier);
822822

823823
AddInterfaceMemberDeclarations(interfaceImplement, additionalDeclarations, interfaceMethodDeclParams);
824824
});
@@ -1173,12 +1173,10 @@ public override async Task<CSharpSyntaxNode> VisitMethodStatement(VBSyntax.Metho
11731173
directlyConvertedCsIdentifier = hasExplicitInterfaceImplementation ? directlyConvertedCsIdentifier : CommonConversions.ConvertIdentifier(node.Identifier);
11741174

11751175
if (hasExplicitInterfaceImplementation) {
1176-
1177-
var requiredParameterList = MakeOptionalParametersRequired(parameterList);
1178-
var delegatingClause = GetDelegatingClause(directlyConvertedCsIdentifier, requiredParameterList, false);
1176+
var delegatingClause = GetDelegatingClause(directlyConvertedCsIdentifier, parameterList, false);
11791177
var explicitInterfaceModifiers = convertedModifiers.RemoveWhere(m => m.IsCsMemberVisibility() || m.IsKind(CSSyntaxKind.VirtualKeyword, CSSyntaxKind.AbstractKeyword) || m.IsKind(CSSyntaxKind.OverrideKeyword, CSSyntaxKind.NewKeyword));
11801178

1181-
var interfaceDeclParams = new MethodDeclarationParameters(attributes, explicitInterfaceModifiers, returnType, typeParameters, requiredParameterList, constraints, delegatingClause);
1179+
var interfaceDeclParams = new MethodDeclarationParameters(attributes, explicitInterfaceModifiers, returnType, typeParameters, parameterList, constraints, delegatingClause);
11821180
AddInterfaceMemberDeclarations(declaredSymbol.ExplicitInterfaceImplementations, additionalDeclarations, interfaceDeclParams);
11831181
}
11841182

@@ -1273,41 +1271,6 @@ private void AddMemberDeclaration(ICollection<MemberDeclarationSyntax> additiona
12731271
additionalDeclarations.Add(declaration);
12741272
}
12751273

1276-
1277-
private static ParameterListSyntax MakeOptionalParametersRequired(ParameterListSyntax parameterList)
1278-
{
1279-
if (parameterList == null) return null;
1280-
1281-
var nonOptionalParameters = parameterList.Parameters.Select(ConvertOptionalParameter);
1282-
1283-
var separatedSyntaxList = SyntaxFactory.SeparatedList(nonOptionalParameters);
1284-
var newParameterList = parameterList.WithParameters(separatedSyntaxList);
1285-
return newParameterList;
1286-
}
1287-
1288-
private static ParameterSyntax ConvertOptionalParameter(ParameterSyntax parameter)
1289-
{
1290-
var optionalAttributes = new List<string> { nameof(OptionalAttribute).GetAttributeIdentifier(),
1291-
nameof(DefaultParameterValueAttribute).GetAttributeIdentifier() };
1292-
1293-
var attrListsToRemove = parameter.AttributeLists.SingleOrDefault(aList => aList.Attributes
1294-
.All(a =>
1295-
{
1296-
string attrIdentifier = string.Empty;
1297-
1298-
if (a.Name is IdentifierNameSyntax identifierNameSyntax) {
1299-
attrIdentifier = identifierNameSyntax.Identifier.Text;
1300-
} else if (a.Name is AliasQualifiedNameSyntax aliasQualifiedNameSyntax) {
1301-
attrIdentifier = aliasQualifiedNameSyntax.Alias.Identifier.Text;
1302-
}
1303-
1304-
return optionalAttributes.Contains(attrIdentifier);
1305-
}));
1306-
1307-
var newAttrLists = parameter.AttributeLists.Remove(attrListsToRemove);
1308-
return parameter.WithDefault(null).WithAttributeLists(newAttrLists);
1309-
}
1310-
13111274
private static ArrowExpressionClauseSyntax GetDelegatingClause(SyntaxToken csIdentifier,
13121275
ParameterListSyntax parameterList, bool isSetAccessor)
13131276
{

CodeConverter/CSharp/OperationExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public static bool IsAssignableExpression(this IOperation operation)
5656
case OperationKind.DynamicMemberReference:
5757
return true;
5858

59-
//Just documenting since it's the only one mentioning reference that can't be assigned to AFAIK
59+
//Just documenting since it's the only one mentioning reference that can't necessarily be assigned to AFAIK
6060
case OperationKind.PropertyReference:
6161
return false;
6262
}

Tests/CSharp/ExpressionTests/ByRefTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ private int ExplicitFunc([Optional, DefaultParameterValue("""")] ref string str)
474474
return 5;
475475
}
476476
477-
int IFoo.ExplicitFunc(ref string str) => ExplicitFunc(ref str);
477+
int IFoo.ExplicitFunc([Optional, DefaultParameterValue("""")] ref string str) => ExplicitFunc(ref str);
478478
}");
479479
}
480480

Tests/CSharp/MemberTests/MemberTests.cs

Lines changed: 199 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2211,6 +2211,205 @@ public partial class FooBar : IFoo, IBar
22112211
}");
22122212
}
22132213

2214+
[Fact]
2215+
public async Task ExplicitInterfaceImplementationRequiredMethodParameters_749_Async()
2216+
{
2217+
await TestConversionVisualBasicToCSharpAsync(
2218+
@"
2219+
Public Interface IFoo
2220+
Function DoFooBar(ByRef str As String, i As Integer) As Integer
2221+
End Interface
2222+
2223+
Public Interface IBar
2224+
Function DoFooBar(ByRef str As String, i As Integer) As Integer
2225+
End Interface
2226+
2227+
Public Class FooBar
2228+
Implements IFoo, IBar
2229+
2230+
Function Foo(ByRef str As String, i As Integer) As Integer Implements IFoo.DoFooBar
2231+
Return 4
2232+
End Function
2233+
2234+
Function Bar(ByRef str As String, i As Integer) As Integer Implements IBar.DoFooBar
2235+
Return 2
2236+
End Function
2237+
2238+
End Class", @"
2239+
public partial interface IFoo
2240+
{
2241+
int DoFooBar(ref string str, int i);
2242+
}
2243+
2244+
public partial interface IBar
2245+
{
2246+
int DoFooBar(ref string str, int i);
2247+
}
2248+
2249+
public partial class FooBar : IFoo, IBar
2250+
{
2251+
2252+
public int Foo(ref string str, int i)
2253+
{
2254+
return 4;
2255+
}
2256+
2257+
int IFoo.DoFooBar(ref string str, int i) => Foo(ref str, i);
2258+
2259+
public int Bar(ref string str, int i)
2260+
{
2261+
return 2;
2262+
}
2263+
2264+
int IBar.DoFooBar(ref string str, int i) => Bar(ref str, i);
2265+
2266+
}
2267+
");
2268+
}
2269+
2270+
[Fact]
2271+
public async Task ExplicitInterfaceImplementationOptionalParameters_1062_Async()
2272+
{
2273+
await TestConversionVisualBasicToCSharpAsync(
2274+
@"
2275+
Public Interface InterfaceWithOptionalParameters
2276+
Sub S(Optional i As Integer = 0)
2277+
End Interface
2278+
2279+
Public Class ImplInterfaceWithOptionalParameters : Implements InterfaceWithOptionalParameters
2280+
Public Sub InterfaceWithOptionalParameters_S(Optional i As Integer = 0) Implements InterfaceWithOptionalParameters.S
2281+
End Sub
2282+
End Class", @"
2283+
public partial interface InterfaceWithOptionalParameters
2284+
{
2285+
void S(int i = 0);
2286+
}
2287+
2288+
public partial class ImplInterfaceWithOptionalParameters : InterfaceWithOptionalParameters
2289+
{
2290+
public void InterfaceWithOptionalParameters_S(int i = 0)
2291+
{
2292+
}
2293+
2294+
void InterfaceWithOptionalParameters.S(int i = 0) => InterfaceWithOptionalParameters_S(i);
2295+
}
2296+
");
2297+
}
2298+
2299+
2300+
[Fact]
2301+
public async Task ExplicitInterfaceImplementationOptionalParametersAsync()
2302+
{
2303+
await TestConversionVisualBasicToCSharpAsync(
2304+
@"Public Interface IFoo
2305+
Property ExplicitProp(Optional str As String = """") As Integer
2306+
Function ExplicitFunc(Optional str2 As String = """", Optional i2 As Integer = 1) As Integer
2307+
End Interface
2308+
2309+
Public Class Foo
2310+
Implements IFoo
2311+
2312+
Private Function ExplicitFunc(Optional str As String = """", Optional i2 As Integer = 1) As Integer Implements IFoo.ExplicitFunc
2313+
Return 5
2314+
End Function
2315+
2316+
Private Property ExplicitProp(Optional str As String = """") As Integer Implements IFoo.ExplicitProp
2317+
Get
2318+
Return 5
2319+
End Get
2320+
Set(value As Integer)
2321+
End Set
2322+
End Property
2323+
End Class", @"
2324+
public partial interface IFoo
2325+
{
2326+
int get_ExplicitProp(string str = """");
2327+
void set_ExplicitProp(string str = """", int value = default);
2328+
int ExplicitFunc(string str2 = """", int i2 = 1);
2329+
}
2330+
2331+
public partial class Foo : IFoo
2332+
{
2333+
2334+
private int ExplicitFunc(string str = """", int i2 = 1)
2335+
{
2336+
return 5;
2337+
}
2338+
2339+
int IFoo.ExplicitFunc(string str = """", int i2 = 1) => ExplicitFunc(str, i2);
2340+
2341+
private int get_ExplicitProp(string str = """")
2342+
{
2343+
return 5;
2344+
}
2345+
2346+
private void set_ExplicitProp(string str = """", int value = default)
2347+
{
2348+
}
2349+
2350+
int IFoo.get_ExplicitProp(string str = """") => get_ExplicitProp(str);
2351+
void IFoo.set_ExplicitProp(string str = """", int value = default) => set_ExplicitProp(str, value);
2352+
}
2353+
");
2354+
}
2355+
2356+
2357+
[Fact]
2358+
public async Task ExplicitInterfaceImplementationOptionalMethodParameters_749_Async()
2359+
{
2360+
await TestConversionVisualBasicToCSharpAsync(
2361+
@"
2362+
Public Interface IFoo
2363+
Function DoFooBar(ByRef str As String, Optional i As Integer = 4) As Integer
2364+
End Interface
2365+
2366+
Public Interface IBar
2367+
Function DoFooBar(ByRef str As String, Optional i As Integer = 8) As Integer
2368+
End Interface
2369+
2370+
Public Class FooBar
2371+
Implements IFoo, IBar
2372+
2373+
Function Foo(ByRef str As String, Optional i As Integer = 4) As Integer Implements IFoo.DoFooBar
2374+
Return 4
2375+
End Function
2376+
2377+
Function Bar(ByRef str As String, Optional i As Integer = 8) As Integer Implements IBar.DoFooBar
2378+
Return 2
2379+
End Function
2380+
2381+
End Class", @"
2382+
public partial interface IFoo
2383+
{
2384+
int DoFooBar(ref string str, int i = 4);
2385+
}
2386+
2387+
public partial interface IBar
2388+
{
2389+
int DoFooBar(ref string str, int i = 8);
2390+
}
2391+
2392+
public partial class FooBar : IFoo, IBar
2393+
{
2394+
2395+
public int Foo(ref string str, int i = 4)
2396+
{
2397+
return 4;
2398+
}
2399+
2400+
int IFoo.DoFooBar(ref string str, int i = 4) => Foo(ref str, i);
2401+
2402+
public int Bar(ref string str, int i = 8)
2403+
{
2404+
return 2;
2405+
}
2406+
2407+
int IBar.DoFooBar(ref string str, int i = 8) => Bar(ref str, i);
2408+
2409+
}
2410+
");
2411+
}
2412+
22142413
[Fact]
22152414
public async Task RenamedInterfaceMethodFullyQualifiedAsync()
22162415
{
@@ -3506,62 +3705,6 @@ private void set_ExplicitProp(string str, int value)
35063705
}");
35073706
}
35083707

3509-
[Fact]
3510-
public async Task ExplicitInterfaceImplementationOptionalParametersAsync()
3511-
{
3512-
await TestConversionVisualBasicToCSharpAsync(
3513-
@"Public Interface IFoo
3514-
Property ExplicitProp(Optional str As String = """") As Integer
3515-
Function ExplicitFunc(Optional str2 As String = """", Optional i2 As Integer = 1) As Integer
3516-
End Interface
3517-
3518-
Public Class Foo
3519-
Implements IFoo
3520-
3521-
Private Function ExplicitFunc(Optional str As String = """", Optional i2 As Integer = 1) As Integer Implements IFoo.ExplicitFunc
3522-
Return 5
3523-
End Function
3524-
3525-
Private Property ExplicitProp(Optional str As String = """") As Integer Implements IFoo.ExplicitProp
3526-
Get
3527-
Return 5
3528-
End Get
3529-
Set(value As Integer)
3530-
End Set
3531-
End Property
3532-
End Class", @"
3533-
public partial interface IFoo
3534-
{
3535-
int get_ExplicitProp(string str = """");
3536-
void set_ExplicitProp(string str = """", int value = default);
3537-
int ExplicitFunc(string str2 = """", int i2 = 1);
3538-
}
3539-
3540-
public partial class Foo : IFoo
3541-
{
3542-
3543-
private int ExplicitFunc(string str = """", int i2 = 1)
3544-
{
3545-
return 5;
3546-
}
3547-
3548-
int IFoo.ExplicitFunc(string str, int i2) => ExplicitFunc(str, i2);
3549-
3550-
private int get_ExplicitProp(string str = """")
3551-
{
3552-
return 5;
3553-
}
3554-
3555-
private void set_ExplicitProp(string str = """", int value = default)
3556-
{
3557-
}
3558-
3559-
int IFoo.get_ExplicitProp(string str) => get_ExplicitProp(str);
3560-
void IFoo.set_ExplicitProp(string str, int value) => set_ExplicitProp(str, value);
3561-
}
3562-
");
3563-
}
3564-
35653708
/// <summary>
35663709
///
35673710
/// </summary>

Tests/CSharp/MemberTests/PropertyMemberTests.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,8 +525,8 @@ internal void set_Prop2(int x = 1, int y = 2, int value = default)
525525
{
526526
}
527527
528-
int IFoo.get_Prop(int x, int y) => get_Prop2(x, y);
529-
void IFoo.set_Prop(int x, int y, int value) => set_Prop2(x, y, value);
528+
int IFoo.get_Prop(int x = 1, int y = 2) => get_Prop2(x, y);
529+
void IFoo.set_Prop(int x = 1, int y = 2, int value = default) => set_Prop2(x, y, value);
530530
531531
public void TestGet()
532532
{
@@ -615,8 +615,8 @@ internal void set_Prop2(int x = 1, int y = 2, int z = 3, int value = default)
615615
{
616616
}
617617
618-
int IFoo.get_Prop(int x, int y, int z) => get_Prop2(x, y, z);
619-
void IFoo.set_Prop(int x, int y, int z, int value) => set_Prop2(x, y, z, value);
618+
int IFoo.get_Prop(int x = 1, int y = 2, int z = 3) => get_Prop2(x, y, z);
619+
void IFoo.set_Prop(int x = 1, int y = 2, int z = 3, int value = default) => set_Prop2(x, y, z, value);
620620
621621
public void TestGet()
622622
{

0 commit comments

Comments
 (0)