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

Include operators in api generation #49

Merged
merged 2 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/PublicApiGenerator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,8 @@ static void AddCtorToTypeDeclaration(CodeTypeDeclaration typeDeclaration, Method

static void AddMethodToTypeDeclaration(CodeTypeDeclaration typeDeclaration, MethodDefinition member, HashSet<string> excludeAttributes)
{
if (member.IsAssembly || member.IsPrivate || member.IsSpecialName)
var isOperator = member.Name.StartsWith("op_");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we inline this so that it is only evaluated with IsSpecialName?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely.

if (member.IsAssembly || member.IsPrivate || (member.IsSpecialName && !isOperator))
return;

var returnType = CreateCodeTypeReference(member.ReturnType);
Expand Down
48 changes: 48 additions & 0 deletions src/PublicApiGeneratorTests/Operator_order.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using PublicApiGeneratorTests.Examples;
using Xunit;

namespace PublicApiGeneratorTests
{
public class Operator_order : ApiGeneratorTestsBase
{
[Fact]
public void Should_show_custom_operators()
{
AssertPublicApi<ClassWithUnaryOperators>(
@"namespace PublicApiGeneratorTests.Examples
{
public class ClassWithUnaryOperators
{
public ClassWithUnaryOperators() { }
public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_Addition(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first, PublicApiGeneratorTests.Examples.ClassWithUnaryOperators second) { }
public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_Decrement(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first) { }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make sense to have a translation table that translates comnon well known ops into the corresponding operator code? Would be a bit more readable in the output I think

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree. I looked into why the full type name was being generated but, likely due to operators being static, it appears they are seen as "unnested" types which causes the FullName to be used instead of just the specific type's name.

Handling the operators in a more special manner would definitely allow me to more cleanly format them (since the problematic area only knows the Type without any context of its use). Do you have a preference of such output? To riff on something:

public static ClassWithUnaryOperator +(ClassWithUnaryOperator first, ClassWithUnaryOperator second) { }

Or are you looking to further condense the signature?

public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_Division(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first, PublicApiGeneratorTests.Examples.ClassWithUnaryOperators second) { }
public static bool op_False(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first) { }
public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_Increment(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first) { }
public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_LogicalNot(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first) { }
public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_Multiply(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first, PublicApiGeneratorTests.Examples.ClassWithUnaryOperators second) { }
public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_OnesComplement(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first) { }
public static PublicApiGeneratorTests.Examples.ClassWithUnaryOperators op_Subtraction(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first, PublicApiGeneratorTests.Examples.ClassWithUnaryOperators second) { }
public static bool op_True(PublicApiGeneratorTests.Examples.ClassWithUnaryOperators first) { }
}
}");
}
}

namespace Examples
{
public class ClassWithUnaryOperators
{
public static ClassWithUnaryOperators operator +(ClassWithUnaryOperators first, ClassWithUnaryOperators second) => second;
public static ClassWithUnaryOperators operator -(ClassWithUnaryOperators first, ClassWithUnaryOperators second) => second;
public static ClassWithUnaryOperators operator /(ClassWithUnaryOperators first, ClassWithUnaryOperators second) => second;
public static ClassWithUnaryOperators operator *(ClassWithUnaryOperators first, ClassWithUnaryOperators second) => second;
public static ClassWithUnaryOperators operator !(ClassWithUnaryOperators first) => first;
public static ClassWithUnaryOperators operator ~(ClassWithUnaryOperators first) => first;
public static ClassWithUnaryOperators operator ++(ClassWithUnaryOperators first) => first;
public static ClassWithUnaryOperators operator --(ClassWithUnaryOperators first) => first;
public static bool operator true(ClassWithUnaryOperators first) => true;
public static bool operator false(ClassWithUnaryOperators first) => false;
}
}
}
30 changes: 30 additions & 0 deletions src/PublicApiGeneratorTests/Operator_visibility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using PublicApiGeneratorTests.Examples;
using Xunit;

namespace PublicApiGeneratorTests
{
public class Operator_visibility : ApiGeneratorTestsBase
{
[Fact]
public void Should_show_custom_operators()
{
AssertPublicApi<ClassWithOperator>(
@"namespace PublicApiGeneratorTests.Examples
{
public class ClassWithOperator
{
public ClassWithOperator() { }
public static PublicApiGeneratorTests.Examples.ClassWithOperator op_Addition(PublicApiGeneratorTests.Examples.ClassWithOperator first, PublicApiGeneratorTests.Examples.ClassWithOperator second) { }
}
}");
}
}

namespace Examples
{
public class ClassWithOperator
{
public static ClassWithOperator operator +(ClassWithOperator first, ClassWithOperator second) => second;
}
}
}