Skip to content

Commit

Permalink
Merge pull request #126 from jnm2/nullable_reference_types
Browse files Browse the repository at this point in the history
Add nullability annotations
  • Loading branch information
danielmarbach committed Nov 8, 2019
2 parents 73aba3e + 97da867 commit 2af0948
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 37 deletions.
3 changes: 2 additions & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<PackageTags>semanticversioning versioning api</PackageTags>
<PackageOutputPath>..\..\nugets</PackageOutputPath>
<LangVersion>latest</LangVersion>
<Nullable>enable</Nullable>
</PropertyGroup>

<PropertyGroup>
Expand All @@ -23,4 +24,4 @@
<PackageReference Include="Microsoft.DotNet.Analyzers.Compatibility" Version="0.2.12-alpha" PrivateAssets="All" />
</ItemGroup>

</Project>
</Project>
12 changes: 6 additions & 6 deletions src/PublicApiGenerator.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class Program
/// <param name="verbose"></param>
/// <param name="leaveArtifacts"></param>
/// <returns></returns>
static int Main(string targetFrameworks, string assembly = null, string projectPath = null, string package = null, string packageVersion = null, string generatorVersion = null, string workingDirectory = null, string outputDirectory = null, bool verbose = false, bool leaveArtifacts = false)
static int Main(string targetFrameworks, string? assembly = null, string? projectPath = null, string? package = null, string? packageVersion = null, string? generatorVersion = null, string? workingDirectory = null, string? outputDirectory = null, bool verbose = false, bool leaveArtifacts = false)
{
if (string.IsNullOrEmpty(outputDirectory))
{
Expand All @@ -47,7 +47,7 @@ static int Main(string targetFrameworks, string assembly = null, string projectP
{
AssertInputParameters(targetFrameworks, projectPath, package, packageVersion, workingArea, assembly);

var template = CreateProjectTemplate(targetFrameworks, projectPath, package, packageVersion, generatorVersion);
var template = CreateProjectTemplate(targetFrameworks, projectPath, package, packageVersion, generatorVersion!);

SaveProjectTemplate(workingArea, template, verbose);

Expand Down Expand Up @@ -77,7 +77,7 @@ static int Main(string targetFrameworks, string assembly = null, string projectP
}
}

private static void GeneratePublicApi(string assembly, string package, string workingArea, string framework, string outputDirectory, bool verbose)
private static void GeneratePublicApi(string? assembly, string? package, string workingArea, string framework, string? outputDirectory, bool verbose)
{
var relativePath = Path.Combine(workingArea, "bin", "Release", framework);
var name = !string.IsNullOrEmpty(assembly) ? $"{assembly}" : $"{package}.dll";
Expand Down Expand Up @@ -168,7 +168,7 @@ private static void SaveProjectTemplate(string workingArea, string template, boo
}
}

private static string CreateProjectTemplate(string targetFrameworks, string project, string package, string packageVersion, string generatorVersion)
private static string CreateProjectTemplate(string targetFrameworks, string? project, string? package, string? packageVersion, string generatorVersion)
{
return ProjectTemplate
.Replace("{TargetFrameworks}", targetFrameworks)
Expand All @@ -182,8 +182,8 @@ private static string CreateProjectTemplate(string targetFrameworks, string proj
);
}

private static void AssertInputParameters(string targetFrameworks, string project, string package,
string packageVersion, string workingArea, string assembly)
private static void AssertInputParameters(string targetFrameworks, string? project, string? package,
string? packageVersion, string workingArea, string? assembly)
{
if (string.IsNullOrEmpty(targetFrameworks))
{
Expand Down
4 changes: 3 additions & 1 deletion src/PublicApiGenerator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ public static class ApiGenerator
{
static readonly string[] defaultWhitelistedNamespacePrefixes = new string[0];

public static string GeneratePublicApi(Assembly assembly, Type[] includeTypes = null, bool shouldIncludeAssemblyAttributes = true, string[] whitelistedNamespacePrefixes = null, string[] excludeAttributes = null)
public static string GeneratePublicApi(Assembly assembly, Type[]? includeTypes = null, bool shouldIncludeAssemblyAttributes = true, string[]? whitelistedNamespacePrefixes = null, string[]? excludeAttributes = null)
{
if (assembly is null) throw new ArgumentNullException(nameof(assembly));

var attributeFilter = new AttributeFilter(excludeAttributes);

using (var assemblyResolver = new DefaultAssemblyResolver())
Expand Down
2 changes: 1 addition & 1 deletion src/PublicApiGenerator/AttributeFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ internal sealed partial class AttributeFilter
{
private readonly HashSet<string> _excludedAttributes;

public AttributeFilter(IEnumerable<string> excludedAttributes)
public AttributeFilter(IEnumerable<string>? excludedAttributes)
{
_excludedAttributes = excludedAttributes is null
? AttributesNotRelevantForThePublicApi
Expand Down
10 changes: 5 additions & 5 deletions src/PublicApiGenerator/CodeTypeReferenceBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal static class CodeTypeReferenceBuilder
{
private const int MAX_COUNT = 100;

internal static CodeTypeReference CreateCodeTypeReference(this TypeReference type, ICustomAttributeProvider attributeProvider = null, NullableMode mode = NullableMode.Default)
internal static CodeTypeReference CreateCodeTypeReference(this TypeReference type, ICustomAttributeProvider? attributeProvider = null, NullableMode mode = NullableMode.Default)
{
return CreateCodeTypeReferenceWithNullabilityMap(type, attributeProvider.GetNullabilityMap().GetEnumerator(), mode, false);
}
Expand All @@ -30,7 +30,7 @@ static CodeTypeReference CreateCodeTypeReferenceWithNullabilityMap(TypeReference
}
}

static CodeTypeReference[] CreateGenericArguments(TypeReference type, IEnumerator<bool?> nullabilityMap)
static CodeTypeReference[]? CreateGenericArguments(TypeReference type, IEnumerator<bool?> nullabilityMap)
{
// ReSharper disable once RedundantEnumerableCastCall
var genericArgs = type is IGenericInstance instance ? instance.GenericArguments : type.HasGenericParameters ? type.GenericParameters.Cast<TypeReference>() : null;
Expand All @@ -44,7 +44,7 @@ static CodeTypeReference[] CreateGenericArguments(TypeReference type, IEnumerato
return genericArguments.ToArray();
}

internal static IEnumerable<bool?> GetNullabilityMap(this ICustomAttributeProvider attributeProvider)
internal static IEnumerable<bool?> GetNullabilityMap(this ICustomAttributeProvider? attributeProvider)
{
var nullableAttr = attributeProvider?.CustomAttributes.SingleOrDefault(d => d.AttributeType.FullName == "System.Runtime.CompilerServices.NullableAttribute");

Expand Down Expand Up @@ -105,7 +105,7 @@ private static bool HasAnyReferenceType(TypeReference type)
return false;
}

static string GetTypeName(TypeReference type, IEnumerator<bool?> nullabilityMap, NullableMode mode, bool disableNested)
static string GetTypeName(TypeReference type, IEnumerator<bool?>? nullabilityMap, NullableMode mode, bool disableNested)
{
bool nullable = mode != NullableMode.Disable && (mode == NullableMode.Force || HasAnyReferenceType(type) && IsNullable());

Expand All @@ -129,7 +129,7 @@ bool IsNullable()
}
}

static string GetTypeNameCore(TypeReference type, IEnumerator<bool?> nullabilityMap, bool nullable, bool disableNested)
static string GetTypeNameCore(TypeReference type, IEnumerator<bool?>? nullabilityMap, bool nullable, bool disableNested)
{
if (type.IsGenericParameter)
{
Expand Down
4 changes: 2 additions & 2 deletions src/PublicApiGenerator/NullableContext.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Mono.Cecil;
using Mono.Cecil;
using System;
using System.Collections.Generic;

Expand All @@ -8,7 +8,7 @@ namespace PublicApiGenerator
internal class NullableContext
{
[ThreadStatic]
private static Stack<ICustomAttributeProvider> _nullableContextProviders;
private static Stack<ICustomAttributeProvider>? _nullableContextProviders;

private static Stack<ICustomAttributeProvider> NullableContextProviders
{
Expand Down
12 changes: 6 additions & 6 deletions src/PublicApiGeneratorTests/ApiGeneratorTestsBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Reflection;
using System.Text.RegularExpressions;
using Xunit;
Expand All @@ -9,27 +9,27 @@ public abstract class ApiGeneratorTestsBase
{
private static readonly Regex StripEmptyLines = new Regex(@"^\s+$[\r\n]*", RegexOptions.Multiline | RegexOptions.Compiled);

protected void AssertPublicApi<T>(string expectedOutput, bool includeAssemblyAttributes = false, string[] excludeAttributes = null)
protected void AssertPublicApi<T>(string expectedOutput, bool includeAssemblyAttributes = false, string[]? excludeAttributes = null)
{
AssertPublicApi(typeof(T), expectedOutput, includeAssemblyAttributes, excludeAttributes);
}

protected void AssertPublicApi(Type type, string expectedOutput, bool includeAssemblyAttributes = false, string[] excludeAttributes = null)
protected void AssertPublicApi(Type type, string expectedOutput, bool includeAssemblyAttributes = false, string[]? excludeAttributes = null)
{
AssertPublicApi(new[] { type }, expectedOutput, includeAssemblyAttributes, excludeAttributes: excludeAttributes);
}

protected void AssertPublicApi(Type[] types, string expectedOutput, bool includeAssemblyAttributes = false, string[] whitelistedNamespacePrefixes = default, string[] excludeAttributes = null)
protected void AssertPublicApi(Type[] types, string expectedOutput, bool includeAssemblyAttributes = false, string[]? whitelistedNamespacePrefixes = null, string[]? excludeAttributes = null)
{
AssertPublicApi(types[0].Assembly, types, expectedOutput, includeAssemblyAttributes, whitelistedNamespacePrefixes, excludeAttributes);
}

private static void AssertPublicApi(Assembly assembly, Type[] types, string expectedOutput, bool includeAssemblyAttributes, string[] whitelistedNamespacePrefixes, string[] excludeAttributes)
private static void AssertPublicApi(Assembly assembly, Type[] types, string expectedOutput, bool includeAssemblyAttributes, string[]? whitelistedNamespacePrefixes, string[]? excludeAttributes)
{
var actualOutput = PublicApiGenerator.ApiGenerator.GeneratePublicApi(assembly, types, includeAssemblyAttributes, whitelistedNamespacePrefixes, excludeAttributes);
actualOutput = StripEmptyLines.Replace(actualOutput, string.Empty);
Assert.Equal(expectedOutput, actualOutput, ignoreCase: false, ignoreLineEndingDifferences: true,
ignoreWhiteSpaceDifferences: true);
}
}
}
}
2 changes: 1 addition & 1 deletion src/PublicApiGeneratorTests/Class_constructors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ protected ClassWithMultipleConstructors(int value)

public class ClassWithConstructorWithDefaultValues
{
public ClassWithConstructorWithDefaultValues(int intValue = 42, string stringValue = "hello world", Type typeValue = null)
public ClassWithConstructorWithDefaultValues(int intValue = 42, string stringValue = "hello world", Type typeValue = null!)
{
}
}
Expand Down
2 changes: 0 additions & 2 deletions src/PublicApiGeneratorTests/Class_nested.cs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,6 @@ public struct NestedStruct
public void Method() { }
}

#nullable enable

// nullable example
public class Foo
{
Expand Down
1 change: 0 additions & 1 deletion src/PublicApiGeneratorTests/Dynamics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ public class ClassWithDynamic2 : System.Collections.Generic.List<dynamic?> { }
// ReSharper disable UnusedMember.Global
namespace Examples
{
#nullable enable
public class ClassWithDynamic : List<dynamic>
{
public class ClassWithDynamic2 : List<dynamic?> { }
Expand Down
6 changes: 3 additions & 3 deletions src/PublicApiGeneratorTests/Interface_methods.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using PublicApiGeneratorTests.Examples;
using Xunit;

Expand Down Expand Up @@ -93,7 +93,7 @@ public interface IMethodsWithParameters

public interface IMethodsWithDefaultParameters
{
void Method1(int intValue = 42, string stringValue = "hello world", Type typeValue = null);
void Method1(int intValue = 42, string stringValue = "hello world", Type typeValue = null!);
}

public interface IMultipleMethods
Expand All @@ -109,4 +109,4 @@ public interface IMethodHiding : IVoidMethod
}
}
// ReSharper restore UnusedMember.Global
}
}
12 changes: 6 additions & 6 deletions src/PublicApiGeneratorTests/Method_parameters.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using PublicApiGeneratorTests.Examples;
using PublicApiGeneratorTests.Examples;
using System.Threading;
using Xunit;

Expand Down Expand Up @@ -368,7 +368,7 @@ public void Method(int value1, string value2, ComplexType value3)

public class MethodWithDefaultValues
{
public void Method(int value1 = 42, string value2 = "hello world", CancellationToken token = default, int value3 = default, string value4 = default)
public void Method(int value1 = 42, string value2 = "hello world", CancellationToken token = default, int value3 = default, string value4 = default!)
{
}
}
Expand Down Expand Up @@ -398,31 +398,31 @@ public class MethodWithOutParameter
{
public void Method(out string value)
{
value = null;
value = null!;
}
}

public class MethodWithOutGenericTypeParameter
{
public void Method(out GenericType<int> value)
{
value = null;
value = null!;
}
}

public class MethodWithOutGenericTypeParameterEnumerable
{
public void Method(out System.Collections.Generic.IEnumerable<int> value)
{
value = null;
value = null!;
}
}

public class MethodWithOutGenericTypeOfComplexTypeParameter
{
public void Method(out GenericType<ComplexType> value)
{
value = null;
value = null!;
}
}

Expand Down
2 changes: 0 additions & 2 deletions src/PublicApiGeneratorTests/NullableTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,6 @@ public interface IDoStuff6<TIn, TOut>
}
}

#nullable enable

// ReSharper disable ClassNeverInstantiated.Global
namespace Examples
{
Expand Down

0 comments on commit 2af0948

Please sign in to comment.