Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rudimentary in modifier support #85

Merged
merged 4 commits into from Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/PublicApiGenerator/ApiGenerator.cs
Expand Up @@ -417,6 +417,7 @@ static string ConvertAttributeToCode(Func<CodeTypeReference, CodeTypeReference>
"System.Runtime.CompilerServices.ExtensionAttribute",
"System.Runtime.CompilerServices.RuntimeCompatibilityAttribute",
"System.Runtime.CompilerServices.IteratorStateMachineAttribute",
"System.Runtime.CompilerServices.IsReadOnlyAttribute",
"System.Reflection.DefaultMemberAttribute",
"System.Diagnostics.DebuggableAttribute",
"System.Diagnostics.DebuggerNonUserCodeAttribute",
Expand Down Expand Up @@ -589,9 +590,16 @@ static bool IsExtensionMethod(ICustomAttributeProvider method)
else if (parameter.ParameterType.IsByReference)
direction |= FieldDirection.Ref;

var parameterType = parameter.ParameterType.IsByReference
? ((ByReferenceType)parameter.ParameterType).ElementType
: parameter.ParameterType;
var parameterType = parameter.ParameterType;
if (parameterType is RequiredModifierType requiredModifierType)
{
parameterType = requiredModifierType.ElementType;
}
// order is crucial because a RequiredModifierType can be a ByReferenceType
if (parameterType is ByReferenceType byReferenceType)
{
parameterType = byReferenceType.ElementType;
}

var type = CreateCodeTypeReference(parameterType);

Expand All @@ -601,6 +609,15 @@ static bool IsExtensionMethod(ICustomAttributeProvider method)
isExtension = false;
}

// special case of ref is in
// TODO: Move CustomAttributes.Any(a => a.AttributeType.FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute") to extension method once other PR is merged
if (parameter.CustomAttributes.Any(a =>
a.AttributeType.FullName == "System.Runtime.CompilerServices.IsReadOnlyAttribute"))
{
type = ModifyCodeTypeReference(type, "in");
direction = FieldDirection.In;
}

var name = parameter.HasConstant
? string.Format(CultureInfo.InvariantCulture, "{0} = {1}", parameter.Name, FormatParameterConstant(parameter))
: parameter.Name;
Expand Down
4 changes: 2 additions & 2 deletions src/PublicApiGeneratorTests/Class_constructors.cs
Expand Up @@ -73,7 +73,7 @@ public void Should_output_constructor_parameters()
{
public class ClassWithConstructorWithParameters
{
public ClassWithConstructorWithParameters(int intValue, string stringValue, PublicApiGeneratorTests.Examples.ComplexType complexType, PublicApiGeneratorTests.Examples.GenericType<int> genericType) { }
public ClassWithConstructorWithParameters(int intValue, string stringValue, PublicApiGeneratorTests.Examples.ComplexType complexType, PublicApiGeneratorTests.Examples.GenericType<int> genericType, ref int intValueByRef, in int intValueByIn) { }
}
}");
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public ClassWithPublicConstructor()

public class ClassWithConstructorWithParameters
{
public ClassWithConstructorWithParameters(int intValue, string stringValue, ComplexType complexType, GenericType<int> genericType)
public ClassWithConstructorWithParameters(int intValue, string stringValue, ComplexType complexType, GenericType<int> genericType, ref int intValueByRef, in int intValueByIn)
{
}
}
Expand Down
18 changes: 18 additions & 0 deletions src/PublicApiGeneratorTests/Interface_method_parameters.cs
Expand Up @@ -148,6 +148,19 @@ public interface IMethodWithRefParameter
}");
}

[Fact]
void Should_output_in_parameters()
{
AssertPublicApi<IMethodWithInParameter>(
@"namespace PublicApiGeneratorTests.Examples
{
public interface IMethodWithInParameter
{
void Method(in string value);
}
}");
}

[Fact]
void Should_output_out_parameters()
{
Expand Down Expand Up @@ -230,6 +243,11 @@ public interface IMethodWithRefParameter
void Method(ref string value);
}

public interface IMethodWithInParameter
{
void Method(in string value);
}

public interface IMethodWithOutParameter
{
void Method(out string value);
Expand Down
63 changes: 63 additions & 0 deletions src/PublicApiGeneratorTests/Method_parameters.cs
Expand Up @@ -75,6 +75,34 @@ public class MethodWithGenericTypeParameter
}");
}

[Fact]
public void Should_output_generic_ref_type()
{
AssertPublicApi<MethodWithGenericRefTypeParameter>(
@"namespace PublicApiGeneratorTests.Examples
{
public class MethodWithGenericRefTypeParameter
{
public MethodWithGenericRefTypeParameter() { }
public void Method(ref PublicApiGeneratorTests.Examples.GenericType<int> value) { }
}
}");
}

[Fact]
public void Should_output_generic_in_type()
{
AssertPublicApi<MethodWithGenericInTypeParameter>(
@"namespace PublicApiGeneratorTests.Examples
{
public class MethodWithGenericInTypeParameter
{
public MethodWithGenericInTypeParameter() { }
public void Method(in PublicApiGeneratorTests.Examples.GenericType<int> value) { }
}
}");
}

[Fact]
public void Should_output_fully_qualified_type_name_for_generic_parameter()
{
Expand Down Expand Up @@ -159,6 +187,20 @@ public class MethodWithRefParameter
}");
}

[Fact]
public void Should_output_in_parameters()
{
AssertPublicApi<MethodWithInParameter>(
@"namespace PublicApiGeneratorTests.Examples
{
public class MethodWithInParameter
{
public MethodWithInParameter() { }
public void Method(in string value) { }
}
}");
}

[Fact]
public void Should_output_out_parameters()
{
Expand Down Expand Up @@ -265,6 +307,20 @@ public void Method(GenericType<int> value)
}
}

public class MethodWithGenericRefTypeParameter
{
public void Method(ref GenericType<int> value)
{
}
}

public class MethodWithGenericInTypeParameter
{
public void Method(in GenericType<int> value)
{
}
}

public class MethodWithGenericTypeOfComplexTypeParameter
{
public void Method(GenericType<ComplexType> value)
Expand Down Expand Up @@ -307,6 +363,13 @@ public void Method(ref string value)
}
}

public class MethodWithInParameter
{
public void Method(in string value)
{
}
}

public class MethodWithOutParameter
{
public void Method(out string value)
Expand Down
1 change: 1 addition & 0 deletions src/PublicApiGeneratorTests/PublicApiGeneratorTests.csproj
Expand Up @@ -3,6 +3,7 @@
<PropertyGroup>
<TargetFrameworks>netcoreapp2.1;net452</TargetFrameworks>
<NoWarn>$(NoWarn);CS0067</NoWarn>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down