|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Linq; |
| 7 | +using System.Reflection; |
| 8 | + |
| 9 | +namespace Microsoft.Windows.PowerShell.ScriptAnalyzer.Generic |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// Holds metadata for a single configurable rule property. |
| 13 | + /// </summary> |
| 14 | + public class RuleOptionInfo |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// The name of the configurable property. |
| 18 | + /// </summary> |
| 19 | + public string Name { get; internal set; } |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// The CLR type of the property value. |
| 23 | + /// </summary> |
| 24 | + public Type OptionType { get; internal set; } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// The default value declared via the ConfigurableRuleProperty attribute. |
| 28 | + /// </summary> |
| 29 | + public object DefaultValue { get; internal set; } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// The set of valid values for this property, if constrained. |
| 33 | + /// Null when any value of the declared type is acceptable. |
| 34 | + /// </summary> |
| 35 | + public object[] PossibleValues { get; internal set; } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Extracts RuleOptionInfo entries for every ConfigurableRuleProperty on |
| 39 | + /// the given rule. For string properties backed by a private enum, the |
| 40 | + /// possible values are populated from the enum members. |
| 41 | + /// </summary> |
| 42 | + /// <param name="rule">The rule instance to inspect.</param> |
| 43 | + /// <returns> |
| 44 | + /// A list of option metadata, ordered with Enable first then the |
| 45 | + /// remainder sorted alphabetically. |
| 46 | + /// </returns> |
| 47 | + public static List<RuleOptionInfo> GetRuleOptions(IRule rule) |
| 48 | + { |
| 49 | + var options = new List<RuleOptionInfo>(); |
| 50 | + Type ruleType = rule.GetType(); |
| 51 | + |
| 52 | + PropertyInfo[] properties = ruleType.GetProperties(BindingFlags.Instance | BindingFlags.Public); |
| 53 | + |
| 54 | + // Collect all private nested enums declared on the rule type so we |
| 55 | + // can match them against string properties whose default value is an |
| 56 | + // enum member name. |
| 57 | + Type[] nestedEnums = ruleType |
| 58 | + .GetNestedTypes(BindingFlags.NonPublic | BindingFlags.Public) |
| 59 | + .Where(t => t.IsEnum) |
| 60 | + .ToArray(); |
| 61 | + |
| 62 | + foreach (PropertyInfo prop in properties) |
| 63 | + { |
| 64 | + var attr = prop.GetCustomAttribute<ConfigurableRulePropertyAttribute>(inherit: true); |
| 65 | + if (attr == null) |
| 66 | + { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + var info = new RuleOptionInfo |
| 71 | + { |
| 72 | + Name = prop.Name, |
| 73 | + OptionType = prop.PropertyType, |
| 74 | + DefaultValue = attr.DefaultValue, |
| 75 | + PossibleValues = null |
| 76 | + }; |
| 77 | + |
| 78 | + // For string properties, attempt to find a matching private enum |
| 79 | + // whose member names include the default value. This mirrors the |
| 80 | + // pattern used by rules such as UseConsistentIndentation and |
| 81 | + // ProvideCommentHelp where a string property is parsed into a |
| 82 | + // private enum via Enum.TryParse. |
| 83 | + // |
| 84 | + // When multiple enums contain the default value (e.g. both have |
| 85 | + // a "None" member), prefer the enum whose name contains the |
| 86 | + // property name or vice-versa (e.g. property "Kind" matches enum |
| 87 | + // "IndentationKind"). This helps avoid incorrect matches when a rule |
| 88 | + // declares several enums with possible overlapping member names. |
| 89 | + if (prop.PropertyType == typeof(string) && attr.DefaultValue is string defaultStr) |
| 90 | + { |
| 91 | + Type bestMatch = null; |
| 92 | + bool bestHasNameRelation = false; |
| 93 | + |
| 94 | + foreach (Type enumType in nestedEnums) |
| 95 | + { |
| 96 | + if (!Enum.GetNames(enumType).Any(n => |
| 97 | + string.Equals(n, defaultStr, StringComparison.OrdinalIgnoreCase))) |
| 98 | + { |
| 99 | + continue; |
| 100 | + } |
| 101 | + |
| 102 | + bool hasNameRelation = |
| 103 | + enumType.Name.IndexOf(prop.Name, StringComparison.OrdinalIgnoreCase) >= 0 || |
| 104 | + prop.Name.IndexOf(enumType.Name, StringComparison.OrdinalIgnoreCase) >= 0; |
| 105 | + |
| 106 | + // Take this enum if we have no match yet, or if it has a |
| 107 | + // name-based relationship and the previous match did not. |
| 108 | + if (bestMatch == null || (hasNameRelation && !bestHasNameRelation)) |
| 109 | + { |
| 110 | + bestMatch = enumType; |
| 111 | + bestHasNameRelation = hasNameRelation; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + if (bestMatch != null) |
| 116 | + { |
| 117 | + info.PossibleValues = Enum.GetNames(bestMatch); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + options.Add(info); |
| 122 | + } |
| 123 | + |
| 124 | + // Sort with "Enable" first, then alphabetically by name for consistent ordering. |
| 125 | + return options |
| 126 | + .OrderBy(o => string.Equals(o.Name, "Enable", StringComparison.OrdinalIgnoreCase) ? 0 : 1) |
| 127 | + .ThenBy(o => o.Name, StringComparer.OrdinalIgnoreCase) |
| 128 | + .ToList(); |
| 129 | + } |
| 130 | + } |
| 131 | +} |
0 commit comments