Skip to content

Commit

Permalink
Rename properties in options (#290)
Browse files Browse the repository at this point in the history
  • Loading branch information
sungam3r committed Mar 6, 2023
1 parent 113bfd3 commit d877e4e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
26 changes: 13 additions & 13 deletions src/PublicApiGenerator/ApiGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public static string GeneratePublicApi(this Assembly assembly, ApiGeneratorOptio
return CreatePublicApiForAssembly(
asm,
typeDefinition => !typeDefinition.IsNested &&
ShouldIncludeType(typeDefinition, options.BlacklistedNamespacePrefixes, options.WhitelistedNamespacePrefixes, options.UseBlacklistedNamespacePrefixesForExtensionMethods) &&
ShouldIncludeType(typeDefinition, options.DenyNamespacePrefixes, options.AllowNamespacePrefixes, options.UseDenyNamespacePrefixesForExtensionMethods) &&
(options.IncludeTypes == null || options.IncludeTypes.Any(type => type.FullName == typeDefinition.FullName && type.Assembly.FullName == typeDefinition.Module.Assembly.FullName)),
options.IncludeAssemblyAttributes,
options.BlacklistedNamespacePrefixes,
options.WhitelistedNamespacePrefixes,
options.UseBlacklistedNamespacePrefixesForExtensionMethods,
options.DenyNamespacePrefixes,
options.AllowNamespacePrefixes,
options.UseDenyNamespacePrefixesForExtensionMethods,
attributeFilter);
}
}
Expand Down Expand Up @@ -150,28 +150,28 @@ private static string CreatePublicApiForAssembly(AssemblyDefinition assembly, Fu
}
}

private static bool ShouldIncludeType(TypeDefinition t, string[] blacklistedNamespacePrefixes, string[] whitelistedNamespacePrefixes, bool useBlacklistedNamespacePrefixesForExtensionMethods)
private static bool ShouldIncludeType(TypeDefinition t, string[] denyNamespacePrefixes, string[] allowNamespacePrefixes, bool useDenyNamespacePrefixesForExtensionMethods)
{
if (t.IsCompilerGenerated())
return false;

if (!t.IsPublic && !t.IsNestedPublic && !t.IsNestedFamily && !t.IsNestedFamilyOrAssembly)
return false;

if (!useBlacklistedNamespacePrefixesForExtensionMethods)
if (!useDenyNamespacePrefixesForExtensionMethods)
{
if (t.GetMembers().Any(m => ShouldIncludeMember(m, blacklistedNamespacePrefixes, whitelistedNamespacePrefixes, useBlacklistedNamespacePrefixesForExtensionMethods)))
if (t.GetMembers().Any(m => ShouldIncludeMember(m, denyNamespacePrefixes, allowNamespacePrefixes, useDenyNamespacePrefixesForExtensionMethods)))
return true;
}

if (blacklistedNamespacePrefixes.Any(t.FullName.StartsWith)
&& !whitelistedNamespacePrefixes.Any(t.FullName.StartsWith))
if (denyNamespacePrefixes.Any(t.FullName.StartsWith)
&& !allowNamespacePrefixes.Any(t.FullName.StartsWith))
return false;

return true;
}

private static bool ShouldIncludeMember(IMemberDefinition m, string[] blacklistedNamespacePrefixes, string[] whitelistedNamespacePrefixes, bool useBlacklistedNamespacePrefixesForExtensionMethods)
private static bool ShouldIncludeMember(IMemberDefinition m, string[] denyNamespacePrefixes, string[] allowNamespacePrefixes, bool useDenyNamespacePrefixesForExtensionMethods)
{
// https://github.com/PublicApiGenerator/PublicApiGenerator/issues/245
bool isRecord = m.DeclaringType.GetMethods().Any(m => m.Name == "<Clone>$");
Expand All @@ -187,11 +187,11 @@ private static bool ShouldIncludeMember(IMemberDefinition m, string[] blackliste
if (m.DeclaringType?.FullName == null)
return false;

if (!useBlacklistedNamespacePrefixesForExtensionMethods && m is MethodDefinition md && md.IsExtensionMethod())
if (!useDenyNamespacePrefixesForExtensionMethods && m is MethodDefinition md && md.IsExtensionMethod())
return true;

if (blacklistedNamespacePrefixes.Any(m.DeclaringType.FullName.StartsWith)
&& !whitelistedNamespacePrefixes.Any(m.DeclaringType.FullName.StartsWith))
if (denyNamespacePrefixes.Any(m.DeclaringType.FullName.StartsWith)
&& !allowNamespacePrefixes.Any(m.DeclaringType.FullName.StartsWith))
return false;

return true;
Expand Down
73 changes: 63 additions & 10 deletions src/PublicApiGenerator/ApiGeneratorOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,42 @@ public class ApiGeneratorOptions
public bool IncludeAssemblyAttributes { get; set; } = true;

/// <summary>
/// Allows to whitelist certain namespace prefixes.
/// Allows to print APIs in certain namespace prefixes.
/// For example by default types found in Microsoft or System namespaces are not treated as part of the public API.
/// This option has priority over <see cref="BlacklistedNamespacePrefixes"/>.
/// This option has priority over <see cref="DenyNamespacePrefixes"/>.
/// </summary>
/// <example>
/// <code>
/// var options = new DefaultApiGeneratorOptions
/// {
/// WhitelistedNamespacePrefixes = new[] { "Microsoft.Whitelisted" }
/// WhitelistedNamespacePrefixes = new[] { "Microsoft" }
/// };
/// </code>
/// </example>
public string[] WhitelistedNamespacePrefixes { get; set; } = _defaultWhitelistedNamespacePrefixes;
[Obsolete("Use AllowNamespacePrefixes instead. Will be removed in the next major.")]
public string[] WhitelistedNamespacePrefixes
{
get => AllowNamespacePrefixes;
set => AllowNamespacePrefixes = value;
}

/// <summary>
/// Allows to blacklist certain namespace prefixes.
/// Allows to print APIs in certain namespace prefixes.
/// For example by default types found in Microsoft or System namespaces are not treated as part of the public API.
/// This option has priority over <see cref="DenyNamespacePrefixes"/>.
/// </summary>
/// <example>
/// <code>
/// var options = new DefaultApiGeneratorOptions
/// {
/// AllowNamespacePrefixes = new[] { "Microsoft" }
/// };
/// </code>
/// </example>
public string[] AllowNamespacePrefixes { get; set; } = _defaultAllowNamespacePrefixes;

/// <summary>
/// Denies to print APIs in certain namespace prefixes.
/// By default types found in Microsoft or System namespaces are not treated as part of the public API.
/// </summary>
/// <example>
Expand All @@ -44,16 +64,49 @@ public class ApiGeneratorOptions
/// };
/// </code>
/// </example>
public string[] BlacklistedNamespacePrefixes { get; set; } = _defaultBlacklistedNamespacePrefixes;
[Obsolete("Use DenyNamespacePrefixes instead. Will be removed in the next major.")]
public string[] BlacklistedNamespacePrefixes
{
get => DenyNamespacePrefixes;
set => DenyNamespacePrefixes = value;
}

/// <summary>
/// Denies to print APIs in certain namespace prefixes.
/// By default types found in Microsoft or System namespaces are not treated as part of the public API.
/// </summary>
/// <example>
/// <code>
/// var options = new DefaultApiGeneratorOptions
/// {
/// DenyNamespacePrefixes = new[] { "System", "Microsoft", "ThirdParty" }
/// };
/// </code>
/// </example>
public string[] DenyNamespacePrefixes { get; set; } = _defaultDenyNamespacePrefixes;

/// <summary>
/// Allows to control whether to include extension methods into generated API even when
/// containing class falls into <see cref="DenyNamespacePrefixes"/> option. This
/// option may be useful, for example, for those who writes extensions for IServiceCollection
/// keeping them into Microsoft.Extensions.DependencyInjection namespace for better discoverability.
/// </summary>
/// <remarks>Defaults to <see langword="true"/>, i.e. extension methods are excluded from output.</remarks>
[Obsolete("Use UseDenyNamespacePrefixesForExtensionMethods instead. Will be removed in the next major.")]
public bool UseBlacklistedNamespacePrefixesForExtensionMethods
{
get => UseDenyNamespacePrefixesForExtensionMethods;
set => UseDenyNamespacePrefixesForExtensionMethods = value;
}

/// <summary>
/// Allows to control whether to include extension methods into generated API even when
/// containing class falls into <see cref="BlacklistedNamespacePrefixes"/> option. This
/// containing class falls into <see cref="DenyNamespacePrefixes"/> option. This
/// option may be useful, for example, for those who writes extensions for IServiceCollection
/// keeping them into Microsoft.Extensions.DependencyInjection namespace for better discoverability.
/// </summary>
/// <remarks>Defaults to <see langword="true"/>, i.e. extension methods are excluded from output.</remarks>
public bool UseBlacklistedNamespacePrefixesForExtensionMethods { get; set; } = true;
public bool UseDenyNamespacePrefixesForExtensionMethods { get; set; } = true;

/// <summary>
/// Allows to exclude attributes by specifying the fullname of the attribute to exclude.
Expand All @@ -68,7 +121,7 @@ public class ApiGeneratorOptions
/// </example>
public string[]? ExcludeAttributes { get; set; }

private static readonly string[] _defaultWhitelistedNamespacePrefixes = Array.Empty<string>();
private static readonly string[] _defaultAllowNamespacePrefixes = Array.Empty<string>();

private static readonly string[] _defaultBlacklistedNamespacePrefixes = new[] { "System", "Microsoft" };
private static readonly string[] _defaultDenyNamespacePrefixes = new[] { "System", "Microsoft" };
}
2 changes: 1 addition & 1 deletion src/PublicApiGeneratorTests/Method_extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static class StringExtensionsInSystemNamespace
{
public static bool CheckLength(this string value, int length) { }
}
}", new PublicApiGenerator.ApiGeneratorOptions { UseBlacklistedNamespacePrefixesForExtensionMethods = false, IncludeAssemblyAttributes = false });
}", new PublicApiGenerator.ApiGeneratorOptions { UseDenyNamespacePrefixesForExtensionMethods = false, IncludeAssemblyAttributes = false });
}

[Fact]
Expand Down
7 changes: 7 additions & 0 deletions src/PublicApiGeneratorTests/PublicApiGenerator.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,18 @@ namespace PublicApiGenerator
public class ApiGeneratorOptions
{
public ApiGeneratorOptions() { }
public string[] AllowNamespacePrefixes { get; set; }
[System.Obsolete("Use DenyNamespacePrefixes instead. Will be removed in the next major.")]
public string[] BlacklistedNamespacePrefixes { get; set; }
public string[] DenyNamespacePrefixes { get; set; }
public string[]? ExcludeAttributes { get; set; }
public bool IncludeAssemblyAttributes { get; set; }
public System.Type[]? IncludeTypes { get; set; }
[System.Obsolete("Use UseDenyNamespacePrefixesForExtensionMethods instead. Will be removed in the n" +
"ext major.")]
public bool UseBlacklistedNamespacePrefixesForExtensionMethods { get; set; }
public bool UseDenyNamespacePrefixesForExtensionMethods { get; set; }
[System.Obsolete("Use AllowNamespacePrefixes instead. Will be removed in the next major.")]
public string[] WhitelistedNamespacePrefixes { get; set; }
}
}

0 comments on commit d877e4e

Please sign in to comment.