Skip to content

Commit 8a05f9e

Browse files
Keep optional parameters for parameterized properties - fixes #642
1 parent 24eff41 commit 8a05f9e

4 files changed

Lines changed: 15 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
1111
### VB -> C#
1212
* All handlers from multi-line handles syntax now converted [#701](https://github.com/icsharpcode/CodeConverter/issues/701)
1313
* Implicilty typed inherited events no longer create extra delegates [#700](https://github.com/icsharpcode/CodeConverter/issues/700)
14+
* Keep optional parameters for parameterized properties [#642](https://github.com/icsharpcode/CodeConverter/issues/642)
1415

1516
### C# -> VB
1617

CodeConverter/CSharp/DeclarationNodeVisitor.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -726,8 +726,7 @@ await a.AcceptAsync<AccessorDeclarationSyntax>(TriviaConvertingDeclarationVisito
726726
}
727727
if (propSymbol.SetMethod != null) {
728728
var setMethod = await CreateMethodDeclarationSyntaxAsync(node.ParameterList, SetMethodId(node), true);
729-
var valueParam = SyntaxFactory.Parameter(CommonConversions.CsEscapedIdentifier("value")).WithType(rawType);
730-
setMethod = setMethod.AddParameterListParameters(valueParam);
729+
setMethod = AddValueSetParameter(propSymbol, setMethod, rawType);
731730
methodDeclarationSyntaxs.Add(setMethod);
732731
}
733732
_additionalDeclarations.Add(node, methodDeclarationSyntaxs.Skip(1).ToArray());
@@ -905,7 +904,7 @@ await asClause.Type.AcceptAsync<TypeSyntax>(_triviaConvertingExpressionVisitor,
905904

906905
if (await ShouldConvertAsParameterizedPropertyAsync(containingPropertyStmt)) {
907906
var setMethod = await CreateMethodDeclarationSyntax(containingPropertyStmt?.ParameterList, true);
908-
return setMethod.AddParameterListParameters(SyntaxFactory.Parameter(CommonConversions.CsEscapedIdentifier("value")).WithType(returnType));
907+
return AddValueSetParameter(declaredPropSymbol, setMethod, returnType);
909908
}
910909
break;
911910
case VBasic.SyntaxKind.AddHandlerAccessorBlock:
@@ -939,6 +938,13 @@ async Task<MethodDeclarationSyntax> CreateMethodDeclarationSyntax(VBSyntax.Param
939938
}
940939
}
941940

941+
private static MethodDeclarationSyntax AddValueSetParameter(IPropertySymbol declaredPropSymbol, MethodDeclarationSyntax setMethod, TypeSyntax returnType)
942+
{
943+
var valueParam = SyntaxFactory.Parameter(CommonConversions.CsEscapedIdentifier("value")).WithType(returnType);
944+
if (declaredPropSymbol?.Parameters.Any(p => p.IsOptional) == true) valueParam = valueParam.WithDefault(SyntaxFactory.EqualsValueClause(ValidSyntaxFactory.DefaultExpression));
945+
return setMethod.AddParameterListParameters(valueParam);
946+
}
947+
942948
private static string SetMethodId(VBSyntax.PropertyStatementSyntax containingPropertyStmt)
943949
{
944950
return $"set_{(containingPropertyStmt.Identifier.Text)}";

CodeConverter/CSharp/ExpressionNodeVisitor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,8 +1069,8 @@ public override async Task<CSharpSyntaxNode> VisitParameter(VBSyntax.ParameterSy
10691069
}
10701070

10711071
EqualsValueClauseSyntax @default = null;
1072-
// Parameterized properties get compiled/converted to a methd with non-optional parameters
1073-
if (node.Default != null && node.Parent?.Parent?.IsKind(VBasic.SyntaxKind.PropertyStatement) != true) {
1072+
// Parameterized properties get compiled/converted to a method with non-optional parameters
1073+
if (node.Default != null) {
10741074
var defaultValue = node.Default.Value.SkipIntoParens();
10751075
if (_semanticModel.GetTypeInfo(defaultValue).Type?.SpecialType == SpecialType.System_DateTime) {
10761076
var constant = _semanticModel.GetConstantValue(defaultValue);

Tests/CSharp/MemberTests/PropertyMemberTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ public void Foo()
165165
}", hasLineCommentConversionIssue: true);//TODO: Improve comment mapping for parameterized property
166166
}
167167

168-
[Fact]
168+
[Fact] //https://github.com/icsharpcode/CodeConverter/issues/642
169169
public async Task TestOptionalParameterizedPropertyAsync()
170170
{
171171
await TestConversionVisualBasicToCSharpAsync(
@@ -195,12 +195,12 @@ internal partial class TestClass
195195
public string FirstName { get; set; }
196196
public string LastName { get; set; }
197197
198-
public string get_FullName(bool isFirst)
198+
public string get_FullName(bool isFirst = false)
199199
{
200200
return FirstName + "" "" + LastName;
201201
}
202202
203-
internal void set_FullName(bool isFirst, string value)
203+
internal void set_FullName(bool isFirst = false, string value = default)
204204
{
205205
if (isFirst)
206206
FirstName = value;

0 commit comments

Comments
 (0)