Skip to content

Commit

Permalink
fix: escape method parameters with reserved keywords (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimothyMakkison authored Sep 20, 2023
1 parent bbac6d0 commit 8f7528a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Riok.Mapperly/Symbols/MethodParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down
7 changes: 7 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/MapperTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();");
}
}

0 comments on commit 8f7528a

Please sign in to comment.