Fix support for Nullable (language version 8) (#25)

* Only emit #nullable when nullable is supported (>= 8.0)

* x

* is not
This commit is contained in:
Stef Heyenrath
2021-08-06 17:09:01 +02:00
committed by GitHub
parent 8d43c73816
commit 9dd2221f23
10 changed files with 56 additions and 34 deletions
@@ -8,10 +8,12 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
internal abstract class BaseGenerator
{
protected readonly Context _context;
protected readonly bool _supportsNullable;
public BaseGenerator(Context context)
public BaseGenerator(Context context, bool supportsNullable)
{
_context = context;
_supportsNullable = supportsNullable;
}
protected string GetPropertyType(IPropertySymbol property, out bool isReplaced)
@@ -11,8 +11,8 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal class PartialInterfacesGenerator : BaseGenerator, IFilesGenerator
{
public PartialInterfacesGenerator(Context context) :
base(context)
public PartialInterfacesGenerator(Context context, bool supportsNullable) :
base(context, supportsNullable)
{
}
@@ -30,7 +30,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
var interfaceName = targetClassSymbol.ResolveInterfaceNameWithOptionalTypeConstraints(pd.InterfaceName);
var file = new FileData(
$"{pd.FileName}.g.cs",
$"{targetClassSymbol.GetFileName()}.g.cs",
CreatePartialInterfaceCode(pd.Namespace, targetClassSymbol, interfaceName, pd.ProxyAll)
);
@@ -48,7 +48,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
{(_supportsNullable ? "#nullable enable" : string.Empty)}
using System;
namespace {ns}
@@ -62,7 +62,7 @@ namespace {ns}
{GenerateEvents(targetClassSymbol)}
}}
}}
#nullable disable";
{(_supportsNullable ? "#nullable disable" : string.Empty)}";
private string GenerateProperties(INamedTypeSymbol targetClassSymbol, bool proxyAll)
{
@@ -12,7 +12,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
{
internal class ProxyClassesGenerator : BaseGenerator, IFilesGenerator
{
public ProxyClassesGenerator(Context context) : base(context)
public ProxyClassesGenerator(Context context, bool supportsNullable) : base(context, supportsNullable)
{
}
@@ -32,7 +32,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
var constructorName = $"{targetClassSymbol.Name}Proxy";
var file = new FileData(
$"{pd.FileName}Proxy.g.cs",
$"{targetClassSymbol.GetFileName()}Proxy.g.cs",
CreateProxyClassCode(pd.Namespace, targetClassSymbol, interfaceName, className, constructorName)
);
@@ -41,7 +41,12 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
return file;
}
private string CreateProxyClassCode(string ns, INamedTypeSymbol targetClassSymbol, string interfaceName, string className, string constructorName) => $@"//----------------------------------------------------------------------------------------
private string CreateProxyClassCode(
string ns,
INamedTypeSymbol targetClassSymbol,
string interfaceName,
string className,
string constructorName) => $@"//----------------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by https://github.com/StefH/ProxyInterfaceSourceGenerator.
//
@@ -50,7 +55,7 @@ namespace ProxyInterfaceSourceGenerator.FileGenerators
// </auto-generated>
//----------------------------------------------------------------------------------------
#nullable enable
{(_supportsNullable ? "#nullable enable" : string.Empty)}
using System;
using AutoMapper;
@@ -76,7 +81,7 @@ namespace {ns}
{GeneratePrivateAutoMapper()}
}}
}}
#nullable disable";
{(_supportsNullable ? "#nullable disable" : string.Empty)}";
private string GeneratePrivateAutoMapper()
{
return _context.ReplacedTypes.Count == 0 ? string.Empty : " private readonly IMapper _mapper;";