Skip to content

Commit

Permalink
Escape keywords in parameter names - fixes #1092
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamTheCoder committed Apr 21, 2024
1 parent 6aea19a commit a978d1b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### VB -> C#

* Escape parameter names [#1092](https://github.com/icsharpcode/CodeConverter/issues/1092)

### C# -> VB

Expand Down
2 changes: 1 addition & 1 deletion CodeConverter/CSharp/ExpressionNodeVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1747,7 +1747,7 @@ async Task<ArgumentSyntax> ConvertArg(VBSyntax.ArgumentSyntax arg, int argIndex)

var argSyntax = await arg.AcceptAsync<ArgumentSyntax>(TriviaConvertingExpressionVisitor);
if (forceNamedParameters && !arg.IsNamed && parameterSymbol != null) {
return argSyntax.WithNameColon(SyntaxFactory.NameColon(parameterSymbol.Name));
return argSyntax.WithNameColon(SyntaxFactory.NameColon(SyntaxFactory.IdentifierName(CommonConversions.CsEscapedIdentifier(parameterSymbol.Name))));
}

return argSyntax;
Expand Down
27 changes: 27 additions & 0 deletions Tests/CSharp/MemberTests/MemberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2296,6 +2296,33 @@ public void InterfaceWithOptionalParameters_S(int i = 0)
");
}

[Fact]
public async Task OptionalParameterWithReservedName_1092_Async()
{
await TestConversionVisualBasicToCSharpAsync(
@"
Public Class WithOptionalParameters
Sub S1(Optional a As Object = Nothing, Optional [default] As String = """")
End Sub
Sub S()
S1(, ""a"")
End Sub
End Class", @"
public partial class WithOptionalParameters
{
public void S1(object a = null, string @default = """")
{
}
public void S()
{
S1(@default: ""a"");
}
}
");
}


[Fact]
public async Task ExplicitInterfaceImplementationOptionalParametersAsync()
Expand Down

0 comments on commit a978d1b

Please sign in to comment.