Skip to content

Commit

Permalink
Merge pull request #85 from JakeGinnivan/in-modifier
Browse files Browse the repository at this point in the history
Rudimentary in modifier support
  • Loading branch information
danielmarbach committed Aug 22, 2019
2 parents 36a3876 + 1e8f790 commit a77c1d1
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 5 deletions.
22 changes: 19 additions & 3 deletions src/PublicApiGenerator/ApiGenerator.cs
Expand Up @@ -604,9 +604,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 @@ -616,6 +623,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

0 comments on commit a77c1d1

Please sign in to comment.