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

fix: filterconfig exclude rule is not works as documented #9666

Merged
merged 4 commits into from
Feb 18, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/Docfx.Dotnet/Filters/ConfigFilterRuleItemUnion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public ConfigFilterRuleItem Rule
{
return Include;
}

// If kind is not specified for exclude. Set `ExtendedSymbolKind.Type` as default kind.
Exclude.Kind ??= ExtendedSymbolKind.Type | ExtendedSymbolKind.Member;

return Exclude;
}
}
Expand Down
35 changes: 21 additions & 14 deletions src/Docfx.Dotnet/Filters/ExtendedSymbolKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,30 @@

namespace Docfx.Dotnet;

[Flags]
internal enum ExtendedSymbolKind
{
Assembly = 0x100,
Namespace = 0x110,
Type = 0x120,
Class,
Struct,
Enum,
Interface,
Delegate,
Member = 0x200,
Event,
Field,
Method,
Property,
#pragma warning disable format
Assembly = 1 << 1,
Namespace = 1 << 2,
// Type
Class = 1 << 3,
Struct = 1 << 4,
Enum = 1 << 5,
Interface = 1 << 6,
Delegate = 1 << 7,
// Member
Event = 1 << 8,
Field = 1 << 9,
Method = 1 << 10,
Property = 1 << 11,
#pragma warning restore format

Type = Class | Struct | Enum | Interface | Delegate,
Member = Event | Field | Method | Property,
}


internal static class ExtendedSymbolKindHelper
{
public static bool Contains(this ExtendedSymbolKind kind, SymbolFilterData symbol)
Expand All @@ -30,6 +37,6 @@ public static bool Contains(this ExtendedSymbolKind kind, SymbolFilterData symbo
{
return false;
}
return (kind & k.Value) == kind;
return (kind & k.Value) > 0;
}
}
38 changes: 38 additions & 0 deletions test/Docfx.Dotnet.Tests/ApiFilterUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,44 @@ public interface IClass1
Assert.Empty(class1.Items);
}

[Fact]
public void TestDocsSampleFilter()
{
var code = @"
namespace Microsoft.DevDiv
{
public class Class1
{
}
}
namespace Microsoft.DevDiv.SpecialCase
{
public class NestedClass : Class1
{
}
}
";
string configFile = "TestData/filterconfig_docs_sample.yml";
MetadataItem output = Verify(code, new() { FilterConfigFile = configFile });

var namespaces = output.Items;
Assert.Single(namespaces);

var @namespace = namespaces[0];
Assert.NotNull(@namespace);
Assert.Equal("Microsoft.DevDiv.SpecialCase", @namespace.Name);
Assert.Single(@namespace.Items);

var nestedClass = @namespace.Items[0];
Assert.Equal("Microsoft.DevDiv.SpecialCase.NestedClass", nestedClass.Name);
}

[Fact]
public void TestExtendedSymbolKindFlags()
{
Assert.True((ExtendedSymbolKind.Type | ExtendedSymbolKind.Member).Contains(new SymbolFilterData { Kind = ExtendedSymbolKind.Interface }));
}

private static MetadataItem Verify(string code, ExtractMetadataConfig config = null, DotnetApiOptions options = null)
{
var compilation = CompilationHelper.CreateCompilationFromCSharpCode(code, "test.dll");
Expand Down
5 changes: 5 additions & 0 deletions test/Docfx.Dotnet.Tests/TestData/filterconfig_docs_sample.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
apiRules:
- include:
uidRegex: ^Microsoft\.DevDiv\.SpecialCase
- exclude:
uidRegex: ^Microsoft\.DevDiv
Loading