Fix default valeu for reference types and non-reference types (#34)

* .

* ok

* sw

* .

* .
This commit is contained in:
Stef Heyenrath
2022-05-08 18:19:10 +02:00
committed by GitHub
parent b4c490aef9
commit 87b2b6c6c3
5 changed files with 67 additions and 15 deletions
@@ -1,11 +1,12 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using ProxyInterfaceSourceGenerator.Enums;
namespace ProxyInterfaceSourceGenerator.Extensions;
internal static class ParameterSymbolExtensions
{
private const string ParameterValueDefault = "default";
// private const string ParameterValueDefault = "default";
private const string ParameterValueNull = "null";
public static string GetRefPrefix(this IParameterSymbol ps)
@@ -37,20 +38,17 @@ internal static class ParameterSymbolExtensions
}
string defaultValue;
if (ps.ExplicitDefaultValue is null)
if (ps.ExplicitDefaultValue == null)
{
defaultValue = ps.NullableAnnotation == NullableAnnotation.Annotated
? ParameterValueNull
: ParameterValueDefault;
defaultValue = ps.Type.IsReferenceType ? ParameterValueNull : $"default({ps.Type})";
}
else
{
defaultValue = ps.ExplicitDefaultValue.ToString();
defaultValue = SymbolDisplay.FormatPrimitive(ps.ExplicitDefaultValue, true, false);
}
return $" = {defaultValue}";
}
public static TypeEnum GetTypeEnum(this IParameterSymbol p) =>
p.Type.GetTypeEnum();
public static TypeEnum GetTypeEnum(this IParameterSymbol p) => p.Type.GetTypeEnum();
}