Add support for reserved keywords like @object and @string (#18)
This commit is contained in:
@@ -1,10 +1,21 @@
|
||||
using Microsoft.CodeAnalysis;
|
||||
using Microsoft.CodeAnalysis.CSharp;
|
||||
using ProxyInterfaceSourceGenerator.Enums;
|
||||
|
||||
namespace ProxyInterfaceSourceGenerator.Extensions
|
||||
{
|
||||
internal static class SymbolExtensions
|
||||
{
|
||||
public static bool IsKeywordOrReserved(this ISymbol symbol)
|
||||
{
|
||||
return SyntaxFacts.GetKeywordKind(symbol.Name) != SyntaxKind.None || SyntaxFacts.GetContextualKeywordKind(symbol.Name) != SyntaxKind.None;
|
||||
}
|
||||
|
||||
public static string SanitizedName(this ISymbol symbol)
|
||||
{
|
||||
return symbol.IsKeywordOrReserved() ? $"@{symbol.Name}" : symbol.Name;
|
||||
}
|
||||
|
||||
public static string GetRefPrefix(this IParameterSymbol ps)
|
||||
{
|
||||
switch (ps.RefKind)
|
||||
@@ -65,23 +76,23 @@ namespace ProxyInterfaceSourceGenerator.Extensions
|
||||
|
||||
var type = !string.IsNullOrEmpty(overrideType) ? overrideType : $"{property.Type}";
|
||||
|
||||
return $"{type} {property.Name} {{ {get}{set}}}";
|
||||
return $"{type} {property.SanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property)
|
||||
{
|
||||
var get = property.GetMethod != null ? $"get => _Instance.{property.Name}; " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = value; " : string.Empty;
|
||||
var get = property.GetMethod != null ? $"get => _Instance.{property.SanitizedName()}; " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.SanitizedName()} = value; " : string.Empty;
|
||||
|
||||
return $"{property.Type} {property.Name} {{ {get}{set}}}";
|
||||
return $"{property.Type} {property.SanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
|
||||
public static string ToPropertyTextForClass(this IPropertySymbol property, string overrideType)
|
||||
{
|
||||
var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>(_Instance.{property.Name}); " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.Name} = _mapper.Map<{property.Type}>(value); " : string.Empty;
|
||||
var get = property.GetMethod != null ? $"get => _mapper.Map<{overrideType}>(_Instance.{property.SanitizedName()}); " : string.Empty;
|
||||
var set = property.SetMethod != null ? $"set => _Instance.{property.SanitizedName()} = _mapper.Map<{property.Type}>(value); " : string.Empty;
|
||||
|
||||
return $"{overrideType} {property.Name} {{ {get}{set}}}";
|
||||
return $"{overrideType} {property.SanitizedName()} {{ {get}{set}}}";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user