Fix missing static null analysis attribute errors on unsupported tfms.
This commit is contained in:
parent
06ec1cd962
commit
84929cd0a6
|
@ -1,9 +0,0 @@
|
||||||
namespace MapTo
|
|
||||||
{
|
|
||||||
internal enum AccessModifier
|
|
||||||
{
|
|
||||||
Public,
|
|
||||||
Internal,
|
|
||||||
Private
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -116,5 +116,16 @@ namespace MapTo.Extensions
|
||||||
|
|
||||||
public static SyntaxNode? GetSyntaxNode(this ISymbol symbol) =>
|
public static SyntaxNode? GetSyntaxNode(this ISymbol symbol) =>
|
||||||
symbol.Locations.FirstOrDefault() is { } location ? location.SourceTree?.GetRoot().FindNode(location.SourceSpan) : null;
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,10 +1,25 @@
|
||||||
using System.Collections.Immutable;
|
using System;
|
||||||
|
using System.Collections.Immutable;
|
||||||
using MapTo.Extensions;
|
using MapTo.Extensions;
|
||||||
using Microsoft.CodeAnalysis;
|
using Microsoft.CodeAnalysis;
|
||||||
using Microsoft.CodeAnalysis.CSharp;
|
using Microsoft.CodeAnalysis.CSharp;
|
||||||
|
|
||||||
namespace MapTo
|
namespace MapTo
|
||||||
{
|
{
|
||||||
|
internal enum AccessModifier
|
||||||
|
{
|
||||||
|
Public,
|
||||||
|
Internal,
|
||||||
|
Private
|
||||||
|
}
|
||||||
|
|
||||||
|
internal enum NullStaticAnalysisState
|
||||||
|
{
|
||||||
|
Default,
|
||||||
|
Enabled,
|
||||||
|
Disabled
|
||||||
|
}
|
||||||
|
|
||||||
internal record SourceCode(string Text, string HintName);
|
internal record SourceCode(string Text, string HintName);
|
||||||
|
|
||||||
internal record MappedProperty(
|
internal record MappedProperty(
|
||||||
|
@ -42,29 +57,25 @@ namespace MapTo
|
||||||
AccessModifier GeneratedMethodsAccessModifier,
|
AccessModifier GeneratedMethodsAccessModifier,
|
||||||
bool GenerateXmlDocument,
|
bool GenerateXmlDocument,
|
||||||
bool SupportNullableReferenceTypes,
|
bool SupportNullableReferenceTypes,
|
||||||
bool SupportNullableStaticAnalysis,
|
bool SupportNullableStaticAnalysis)
|
||||||
LanguageVersion LanguageVersion)
|
|
||||||
{
|
{
|
||||||
internal static SourceGenerationOptions From(GeneratorExecutionContext context)
|
internal static SourceGenerationOptions From(GeneratorExecutionContext context)
|
||||||
{
|
{
|
||||||
var compilation = context.Compilation as CSharpCompilation;
|
const string allowNullAttributeName = "System.Diagnostics.CodeAnalysis.AllowNullAttribute";
|
||||||
var supportNullableReferenceTypes = false;
|
var supportNullableStaticAnalysis = context.GetBuildGlobalOption(propertyName: nameof(SupportNullableStaticAnalysis), NullStaticAnalysisState.Default);
|
||||||
var supportNullableStaticAnalysis = false;
|
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(
|
return new(
|
||||||
context.GetBuildGlobalOption(nameof(ConstructorAccessModifier), AccessModifier.Public),
|
ConstructorAccessModifier: context.GetBuildGlobalOption(propertyName: nameof(ConstructorAccessModifier), AccessModifier.Public),
|
||||||
context.GetBuildGlobalOption(nameof(GeneratedMethodsAccessModifier), AccessModifier.Public),
|
GeneratedMethodsAccessModifier: context.GetBuildGlobalOption(propertyName: nameof(GeneratedMethodsAccessModifier), AccessModifier.Public),
|
||||||
context.GetBuildGlobalOption(nameof(GenerateXmlDocument), true),
|
GenerateXmlDocument: context.GetBuildGlobalOption(propertyName: nameof(GenerateXmlDocument), true),
|
||||||
supportNullableReferenceTypes,
|
SupportNullableReferenceTypes: supportNullableReferenceTypes,
|
||||||
supportNullableStaticAnalysis,
|
SupportNullableStaticAnalysis: supportNullableStaticAnalysis switch
|
||||||
compilation?.LanguageVersion ?? LanguageVersion.Default
|
{
|
||||||
|
NullStaticAnalysisState.Enabled => true,
|
||||||
|
NullStaticAnalysisState.Disabled => false,
|
||||||
|
_ => context.Compilation is CSharpCompilation { LanguageVersion: >= LanguageVersion.CSharp8 } cs && cs.TypeByMetadataNameExists(allowNullAttributeName)
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,9 +2,9 @@
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net5.0</TargetFramework>
|
<TargetFramework>net471</TargetFramework>
|
||||||
<LangVersion>latest</LangVersion>
|
<LangVersion>latest</LangVersion>
|
||||||
<Nullable>disable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
|
@ -18,7 +18,7 @@ namespace TestConsoleApp.ViewModels
|
||||||
|
|
||||||
private class IdConverter : ITypeConverter<int, string>
|
private class IdConverter : ITypeConverter<int, string>
|
||||||
{
|
{
|
||||||
public string Convert(int source, object[] converterParameters) => $"{source:X}";
|
public string Convert(int source, object[]? converterParameters) => $"{source:X}";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue