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
@@ -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;";