Skip to content

Commit a978d1b

Browse files
Escape keywords in parameter names - fixes #1092
1 parent 6aea19a commit a978d1b

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

+1
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

1212
### VB -> C#
1313

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

1516
### C# -> VB
1617

CodeConverter/CSharp/ExpressionNodeVisitor.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1747,7 +1747,7 @@ async Task<ArgumentSyntax> ConvertArg(VBSyntax.ArgumentSyntax arg, int argIndex)
17471747

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

17531753
return argSyntax;

Tests/CSharp/MemberTests/MemberTests.cs

+27
Original file line numberDiff line numberDiff line change
@@ -2296,6 +2296,33 @@ public void InterfaceWithOptionalParameters_S(int i = 0)
22962296
");
22972297
}
22982298

2299+
[Fact]
2300+
public async Task OptionalParameterWithReservedName_1092_Async()
2301+
{
2302+
await TestConversionVisualBasicToCSharpAsync(
2303+
@"
2304+
Public Class WithOptionalParameters
2305+
Sub S1(Optional a As Object = Nothing, Optional [default] As String = """")
2306+
End Sub
2307+
2308+
Sub S()
2309+
S1(, ""a"")
2310+
End Sub
2311+
End Class", @"
2312+
public partial class WithOptionalParameters
2313+
{
2314+
public void S1(object a = null, string @default = """")
2315+
{
2316+
}
2317+
2318+
public void S()
2319+
{
2320+
S1(@default: ""a"");
2321+
}
2322+
}
2323+
");
2324+
}
2325+
22992326

23002327
[Fact]
23012328
public async Task ExplicitInterfaceImplementationOptionalParametersAsync()

0 commit comments

Comments
 (0)