|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Microsoft.CodeAnalysis; |
| 5 | + |
| 6 | +namespace Microsoft.DotNet.ApiSymbolExtensions.Filtering; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A factory class to create symbol filters. |
| 10 | +/// </summary> |
| 11 | +public static class SymbolFilterFactory |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Creates a composite filter to exclude APIs using the DocIDs provided in the specifed file paths. |
| 15 | + /// </summary> |
| 16 | + /// <param name="apiExclusionFilePaths">A collection of paths where the exclusion files should be searched.</param> |
| 17 | + /// <param name="accessibilitySymbolFilter">An optional custom accessibility symbol filter to use.</param> |
| 18 | + /// <param name="respectInternals">Whether to include internal symbols or not.</param> |
| 19 | + /// <param name="includeEffectivelyPrivateSymbols">Whether to include effectively private symbols or not.</param> |
| 20 | + /// <param name="includeExplicitInterfaceImplementationSymbols">Whether to include explicit interface implementation symbols or not.</param> |
| 21 | + /// <param name="includeImplicitSymbolFilter">Whether to include implicit symbols or not.</param> |
| 22 | + /// <returns>An instance of the symbol filter.</returns> |
| 23 | + public static ISymbolFilter GetFilterFromFiles(string[]? apiExclusionFilePaths, |
| 24 | + AccessibilitySymbolFilter? accessibilitySymbolFilter = null, |
| 25 | + bool respectInternals = false, |
| 26 | + bool includeEffectivelyPrivateSymbols = true, |
| 27 | + bool includeExplicitInterfaceImplementationSymbols = true, |
| 28 | + bool includeImplicitSymbolFilter = true) |
| 29 | + { |
| 30 | + DocIdSymbolFilter? docIdSymbolFilter = |
| 31 | + apiExclusionFilePaths?.Length > 0 ? |
| 32 | + DocIdSymbolFilter.CreateFromFiles(apiExclusionFilePaths) : null; |
| 33 | + |
| 34 | + return GetCompositeSymbolFilter(docIdSymbolFilter, accessibilitySymbolFilter, respectInternals, includeEffectivelyPrivateSymbols, includeExplicitInterfaceImplementationSymbols, includeImplicitSymbolFilter); |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Creates a composite filter to exclude APIs using the DocIDs provided in the specifed list. |
| 39 | + /// </summary> |
| 40 | + /// <param name="apiExclusionList">A collection of exclusion list.</param> |
| 41 | + /// <param name="accessibilitySymbolFilter">An optional custom accessibility symbol filter to use.</param> |
| 42 | + /// <param name="respectInternals">Whether to include internal symbols or not.</param> |
| 43 | + /// <param name="includeEffectivelyPrivateSymbols">Whether to include effectively private symbols or not.</param> |
| 44 | + /// <param name="includeExplicitInterfaceImplementationSymbols">Whether to include explicit interface implementation symbols or not.</param> |
| 45 | + /// <param name="includeImplicitSymbolFilter">Whether to include implicit symbols or not.</param> |
| 46 | + /// <returns>An instance of the symbol filter.</returns> |
| 47 | + public static ISymbolFilter GetFilterFromList(string[]? apiExclusionList, |
| 48 | + AccessibilitySymbolFilter? accessibilitySymbolFilter = null, |
| 49 | + bool respectInternals = false, |
| 50 | + bool includeEffectivelyPrivateSymbols = true, |
| 51 | + bool includeExplicitInterfaceImplementationSymbols = true, |
| 52 | + bool includeImplicitSymbolFilter = true) |
| 53 | + { |
| 54 | + DocIdSymbolFilter? docIdSymbolFilter = |
| 55 | + apiExclusionList?.Count() > 0 ? |
| 56 | + DocIdSymbolFilter.CreateFromLists(apiExclusionList) : null; |
| 57 | + |
| 58 | + return GetCompositeSymbolFilter(docIdSymbolFilter, accessibilitySymbolFilter, respectInternals, includeEffectivelyPrivateSymbols, includeExplicitInterfaceImplementationSymbols, includeImplicitSymbolFilter); |
| 59 | + } |
| 60 | + |
| 61 | + private static ISymbolFilter GetCompositeSymbolFilter(DocIdSymbolFilter? customFilter, |
| 62 | + AccessibilitySymbolFilter? accessibilitySymbolFilter, |
| 63 | + bool respectInternals, |
| 64 | + bool includeEffectivelyPrivateSymbols, |
| 65 | + bool includeExplicitInterfaceImplementationSymbols, |
| 66 | + bool includeImplicitSymbolFilter) |
| 67 | + { |
| 68 | + accessibilitySymbolFilter ??= new( |
| 69 | + respectInternals, |
| 70 | + includeEffectivelyPrivateSymbols, |
| 71 | + includeExplicitInterfaceImplementationSymbols); |
| 72 | + |
| 73 | + CompositeSymbolFilter filter = new(); |
| 74 | + |
| 75 | + if (customFilter != null) |
| 76 | + { |
| 77 | + filter.Add(customFilter); |
| 78 | + } |
| 79 | + if (includeImplicitSymbolFilter) |
| 80 | + { |
| 81 | + filter.Add(new ImplicitSymbolFilter()); |
| 82 | + } |
| 83 | + |
| 84 | + filter.Add(accessibilitySymbolFilter); |
| 85 | + |
| 86 | + return filter; |
| 87 | + } |
| 88 | +} |
0 commit comments