From 8f7528a8b60fc7ffc3a3378e46ba48d88f54ab9b Mon Sep 17 00:00:00 2001 From: Tim M <49349513+TimothyMakkison@users.noreply.github.com> Date: Wed, 20 Sep 2023 07:11:16 +0100 Subject: [PATCH] fix: escape method parameters with reserved keywords (#756) --- src/Riok.Mapperly/Symbols/MethodParameter.cs | 8 +++++++- test/Riok.Mapperly.Tests/Mapping/MapperTest.cs | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Riok.Mapperly/Symbols/MethodParameter.cs b/src/Riok.Mapperly/Symbols/MethodParameter.cs index 6f828d030e..4bc9a60227 100644 --- a/src/Riok.Mapperly/Symbols/MethodParameter.cs +++ b/src/Riok.Mapperly/Symbols/MethodParameter.cs @@ -6,8 +6,14 @@ namespace Riok.Mapperly.Symbols; public readonly record struct MethodParameter(int Ordinal, string Name, ITypeSymbol Type) { + private static readonly SymbolDisplayFormat _parameterNameFormat = + new( + parameterOptions: SymbolDisplayParameterOptions.IncludeName, + miscellaneousOptions: SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers + ); + public MethodParameter(IParameterSymbol symbol) - : this(symbol.Ordinal, symbol.Name, symbol.Type.UpgradeNullable()) { } + : this(symbol.Ordinal, symbol.ToDisplayString(_parameterNameFormat), symbol.Type.UpgradeNullable()) { } public MethodArgument WithArgument(ExpressionSyntax? argument) => new(this, argument ?? throw new ArgumentNullException(nameof(argument))); diff --git a/test/Riok.Mapperly.Tests/Mapping/MapperTest.cs b/test/Riok.Mapperly.Tests/Mapping/MapperTest.cs index 511be80714..73a05b45a1 100644 --- a/test/Riok.Mapperly.Tests/Mapping/MapperTest.cs +++ b/test/Riok.Mapperly.Tests/Mapping/MapperTest.cs @@ -147,4 +147,11 @@ enum E2 { Value1 } return TestHelper.VerifyGenerator(source); } + + [Fact] + public void RestrictedKeywordParametersShouldBeEscaped() + { + var source = TestSourceBuilder.MapperWithBody("public partial string Map(int @object);"); + TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return @object.ToString();"); + } }