Merge pull request #11 from mrtaikandi/null_support_fix

Fix missing static null analysis attribute errors on unsupported tfms.
This commit is contained in:
Mohammadreza 2021-07-29 17:54:08 +01:00 committed by GitHub
commit 5af2be8eef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 44 additions and 31 deletions

View File

@ -1,9 +0,0 @@
namespace MapTo
{
internal enum AccessModifier
{
Public,
Internal,
Private
}
}

View File

@ -116,5 +116,16 @@ namespace MapTo.Extensions
public static SyntaxNode? GetSyntaxNode(this ISymbol symbol) =>
symbol.Locations.FirstOrDefault() is { } location ? location.SourceTree?.GetRoot().FindNode(location.SourceSpan) : null;
public static IEnumerable<INamedTypeSymbol> GetTypesByMetadataName(this Compilation compilation, string typeMetadataName)
{
return compilation.References
.Select(compilation.GetAssemblyOrModuleSymbol)
.OfType<IAssemblySymbol>()
.Select(assemblySymbol => assemblySymbol.GetTypeByMetadataName(typeMetadataName))
.Where(t => t != null)!;
}
public static bool TypeByMetadataNameExists(this Compilation compilation, string typeMetadataName) => GetTypesByMetadataName(compilation, typeMetadataName).Any();
}
}

View File

@ -1,10 +1,25 @@
using System.Collections.Immutable;
using System;
using System.Collections.Immutable;
using MapTo.Extensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
namespace MapTo
{
internal enum AccessModifier
{
Public,
Internal,
Private
}
internal enum NullStaticAnalysisState
{
Default,
Enabled,
Disabled
}
internal record SourceCode(string Text, string HintName);
internal record MappedProperty(
@ -42,29 +57,25 @@ namespace MapTo
AccessModifier GeneratedMethodsAccessModifier,
bool GenerateXmlDocument,
bool SupportNullableReferenceTypes,
bool SupportNullableStaticAnalysis,
LanguageVersion LanguageVersion)
bool SupportNullableStaticAnalysis)
{
internal static SourceGenerationOptions From(GeneratorExecutionContext context)
{
var compilation = context.Compilation as CSharpCompilation;
var supportNullableReferenceTypes = false;
var supportNullableStaticAnalysis = false;
const string allowNullAttributeName = "System.Diagnostics.CodeAnalysis.AllowNullAttribute";
var supportNullableStaticAnalysis = context.GetBuildGlobalOption(propertyName: nameof(SupportNullableStaticAnalysis), NullStaticAnalysisState.Default);
var supportNullableReferenceTypes = context.Compilation.Options.NullableContextOptions is NullableContextOptions.Warnings or NullableContextOptions.Enable;
if (compilation is not null)
{
supportNullableStaticAnalysis = compilation.LanguageVersion >= LanguageVersion.CSharp8;
supportNullableReferenceTypes = compilation.Options.NullableContextOptions == NullableContextOptions.Warnings ||
compilation.Options.NullableContextOptions == NullableContextOptions.Enable;
}
return new(
context.GetBuildGlobalOption(nameof(ConstructorAccessModifier), AccessModifier.Public),
context.GetBuildGlobalOption(nameof(GeneratedMethodsAccessModifier), AccessModifier.Public),
context.GetBuildGlobalOption(nameof(GenerateXmlDocument), true),
supportNullableReferenceTypes,
supportNullableStaticAnalysis,
compilation?.LanguageVersion ?? LanguageVersion.Default
ConstructorAccessModifier: context.GetBuildGlobalOption(propertyName: nameof(ConstructorAccessModifier), AccessModifier.Public),
GeneratedMethodsAccessModifier: context.GetBuildGlobalOption(propertyName: nameof(GeneratedMethodsAccessModifier), AccessModifier.Public),
GenerateXmlDocument: context.GetBuildGlobalOption(propertyName: nameof(GenerateXmlDocument), true),
SupportNullableReferenceTypes: supportNullableReferenceTypes,
SupportNullableStaticAnalysis: supportNullableStaticAnalysis switch
{
NullStaticAnalysisState.Enabled => true,
NullStaticAnalysisState.Disabled => false,
_ => context.Compilation is CSharpCompilation { LanguageVersion: >= LanguageVersion.CSharp8 } cs && cs.TypeByMetadataNameExists(allowNullAttributeName)
}
);
}

View File

@ -2,9 +2,9 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net471</TargetFramework>
<LangVersion>latest</LangVersion>
<Nullable>disable</Nullable>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>

View File

@ -18,7 +18,7 @@ namespace TestConsoleApp.ViewModels
private class IdConverter : ITypeConverter<int, string>
{
public string Convert(int source, object[] converterParameters) => $"{source:X}";
public string Convert(int source, object[]? converterParameters) => $"{source:X}";
}
}
}