Fix missing static null analysis attribute errors on unsupported tfms.

This commit is contained in:
Mohammadreza Taikandi 2021-07-29 17:40:55 +01:00
parent 06ec1cd962
commit 84929cd0a6
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}";
}
}
}