diff --git a/src/PublicApiGenerator/CSharpOperatorKeyword.cs b/src/PublicApiGenerator/CSharpOperatorKeyword.cs index 29cfc1d..6660ce8 100644 --- a/src/PublicApiGenerator/CSharpOperatorKeyword.cs +++ b/src/PublicApiGenerator/CSharpOperatorKeyword.cs @@ -2,7 +2,7 @@ namespace PublicApiGenerator; internal static class CSharpOperatorKeyword { - private static readonly IDictionary OperatorNameMap = new Dictionary + private static readonly Dictionary _operatorNameMap = new() { { "op_False", "false" }, { "op_True", "true" }, @@ -32,6 +32,6 @@ internal static class CSharpOperatorKeyword public static string Get(string memberName) { - return OperatorNameMap.TryGetValue(memberName, out string mappedMemberName) ? "operator " + mappedMemberName : memberName; + return _operatorNameMap.TryGetValue(memberName, out string mappedMemberName) ? "operator " + mappedMemberName : memberName; } } diff --git a/src/PublicApiGenerator/CodeTypeDeclarationExtensions.cs b/src/PublicApiGenerator/CodeTypeDeclarationExtensions.cs index 1e500b7..d9fd0a1 100644 --- a/src/PublicApiGenerator/CodeTypeDeclarationExtensions.cs +++ b/src/PublicApiGenerator/CodeTypeDeclarationExtensions.cs @@ -4,6 +4,27 @@ namespace PublicApiGenerator; internal static class CodeTypeDeclarationExtensions { + private static readonly Dictionary _map = new() + { + { "System.Int16", "short" }, + { "System.Int32", "int" }, + { "System.Int64", "long" }, + { "System.String", "string" }, + { "System.Object", "object" }, + { "System.Boolean", "bool" }, + { "System.Char", "char" }, + { "System.Byte", "byte" }, + { "System.UInt16", "ushort" }, + { "System.UInt32", "uint" }, + { "System.UInt64", "ulong" }, + { "System.SByte", "sbyte" }, + { "System.Single", "float" }, + { "System.Double", "double" }, + { "System.Decimal", "decimal" }, + }; + + private static string MapToAlias(string type) => _map.TryGetValue(type, out string alias) ? alias : type; + public static CodeTypeDeclarationEx Sort(this CodeTypeDeclarationEx original) { if (original.IsEnum) @@ -100,7 +121,7 @@ string GetBaseType(CodeTypeReference r) { return r.BaseType == "System.Nullable`1" && r.TypeArguments.Count == 1 ? r.TypeArguments[0].BaseType + "?" - : r.BaseType; + : MapToAlias(r.BaseType); } var baseType = string.CompareOrdinal(GetBaseType(x), GetBaseType(y)); diff --git a/src/PublicApiGeneratorTests/AliasOrdering.cs b/src/PublicApiGeneratorTests/AliasOrdering.cs new file mode 100644 index 0000000..a10dde2 --- /dev/null +++ b/src/PublicApiGeneratorTests/AliasOrdering.cs @@ -0,0 +1,32 @@ +using PublicApiGeneratorTests.Examples; + +namespace PublicApiGeneratorTests +{ + public class AliasOrdering : ApiGeneratorTestsBase + { + [Fact] + public void Should_Order_Class_First() + { + AssertPublicApi(typeof(ClassWithAliasedTypes), +""" +namespace PublicApiGeneratorTests.Examples +{ + public static class ClassWithAliasedTypes + { + public static void ShouldBeNegative(this float actual, string? customMessage = null) { } + public static void ShouldBeNegative(this short actual, string? customMessage = null) { } + } +} +"""); + } + } + + namespace Examples + { + public static class ClassWithAliasedTypes + { + public static void ShouldBeNegative(this short actual, string? customMessage = null) { } + public static void ShouldBeNegative(this float actual, string? customMessage = null) { } + } + } +} diff --git a/src/PublicApiGeneratorTests/Expressions.cs b/src/PublicApiGeneratorTests/Expressions.cs index 573c19d..b1d6f69 100644 --- a/src/PublicApiGeneratorTests/Expressions.cs +++ b/src/PublicApiGeneratorTests/Expressions.cs @@ -13,8 +13,8 @@ namespace PublicApiGeneratorTests.Examples { public class ClassWithExpressions { - public ClassWithExpressions(params System.Linq.Expressions.Expression>[] expr1) { } public ClassWithExpressions(params System.Linq.Expressions.Expression>[] expr2) { } + public ClassWithExpressions(params System.Linq.Expressions.Expression>[] expr1) { } public ClassWithExpressions(System.Linq.Expressions.Expression?> expr3) { } public ClassWithExpressions(System.Linq.Expressions.Expression>? expr4) { } }