From 84929cd0a6930ab04f73046cc27fdd875c73a9a2 Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Thu, 29 Jul 2021 17:40:55 +0100 Subject: [PATCH 1/3] Fix missing static null analysis attribute errors on unsupported tfms. --- src/MapTo/AccessModifier.cs | 9 ---- src/MapTo/Extensions/RoslynExtensions.cs | 11 +++++ src/MapTo/Models.cs | 49 ++++++++++++------- test/TestConsoleApp/TestConsoleApp.csproj | 4 +- .../ViewModels/UserViewModel.cs | 2 +- 5 files changed, 44 insertions(+), 31 deletions(-) delete mode 100644 src/MapTo/AccessModifier.cs diff --git a/src/MapTo/AccessModifier.cs b/src/MapTo/AccessModifier.cs deleted file mode 100644 index 64f557b..0000000 --- a/src/MapTo/AccessModifier.cs +++ /dev/null @@ -1,9 +0,0 @@ -namespace MapTo -{ - internal enum AccessModifier - { - Public, - Internal, - Private - } -} \ No newline at end of file diff --git a/src/MapTo/Extensions/RoslynExtensions.cs b/src/MapTo/Extensions/RoslynExtensions.cs index 234b60d..edae4c8 100644 --- a/src/MapTo/Extensions/RoslynExtensions.cs +++ b/src/MapTo/Extensions/RoslynExtensions.cs @@ -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 GetTypesByMetadataName(this Compilation compilation, string typeMetadataName) + { + return compilation.References + .Select(compilation.GetAssemblyOrModuleSymbol) + .OfType() + .Select(assemblySymbol => assemblySymbol.GetTypeByMetadataName(typeMetadataName)) + .Where(t => t != null)!; + } + + public static bool TypeByMetadataNameExists(this Compilation compilation, string typeMetadataName) => GetTypesByMetadataName(compilation, typeMetadataName).Any(); } } \ No newline at end of file diff --git a/src/MapTo/Models.cs b/src/MapTo/Models.cs index 1ec8e0d..9bda8be 100644 --- a/src/MapTo/Models.cs +++ b/src/MapTo/Models.cs @@ -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) + } ); } diff --git a/test/TestConsoleApp/TestConsoleApp.csproj b/test/TestConsoleApp/TestConsoleApp.csproj index 244c732..989f8e5 100644 --- a/test/TestConsoleApp/TestConsoleApp.csproj +++ b/test/TestConsoleApp/TestConsoleApp.csproj @@ -2,9 +2,9 @@ Exe - net5.0 + net471 latest - disable + enable diff --git a/test/TestConsoleApp/ViewModels/UserViewModel.cs b/test/TestConsoleApp/ViewModels/UserViewModel.cs index 50f3f3e..91b2f77 100644 --- a/test/TestConsoleApp/ViewModels/UserViewModel.cs +++ b/test/TestConsoleApp/ViewModels/UserViewModel.cs @@ -18,7 +18,7 @@ namespace TestConsoleApp.ViewModels private class IdConverter : ITypeConverter { - public string Convert(int source, object[] converterParameters) => $"{source:X}"; + public string Convert(int source, object[]? converterParameters) => $"{source:X}"; } } } \ No newline at end of file From 6b98098b8cff7086ef14bc1875946d2b4b4eacd0 Mon Sep 17 00:00:00 2001 From: Mohammadreza Date: Thu, 29 Jul 2021 17:50:49 +0100 Subject: [PATCH 2/3] Update build_and-test.yml Fixes nbgv config. --- .github/workflows/build_and-test.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/build_and-test.yml b/.github/workflows/build_and-test.yml index de7f096..64b4e64 100644 --- a/.github/workflows/build_and-test.yml +++ b/.github/workflows/build_and-test.yml @@ -11,6 +11,11 @@ jobs: steps: - uses: actions/checkout@v2 + with: + fetch-depth: 0 # avoid shallow clone so nbgv can do its work. + - uses: dotnet/nbgv@v0.4.0 + with: + setAllVars: true - name: Setup .NET uses: actions/setup-dotnet@v1 with: From 4942307274df5eda8ff60fed07220468e202bb4d Mon Sep 17 00:00:00 2001 From: Mohammadreza Date: Thu, 29 Jul 2021 17:47:24 +0100 Subject: [PATCH 3/3] Update and rename build_and-test.yml to build-and-test.yml Fix incorrect nbfv config. --- .github/workflows/{build_and-test.yml => build-and-test.yml} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{build_and-test.yml => build-and-test.yml} (100%) diff --git a/.github/workflows/build_and-test.yml b/.github/workflows/build-and-test.yml similarity index 100% rename from .github/workflows/build_and-test.yml rename to .github/workflows/build-and-test.yml