|
| 1 | +using Rubberduck.Parsing.Grammar; |
| 2 | +using Rubberduck.Parsing.Symbols; |
| 3 | +using Rubberduck.Resources; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Globalization; |
| 7 | +using System.Linq; |
| 8 | + |
| 9 | +namespace Rubberduck.Refactorings.Common |
| 10 | +{ |
| 11 | + public static class VBAIdentifierValidator |
| 12 | + { |
| 13 | + private static IEnumerable<string> ReservedIdentifiers = |
| 14 | + typeof(Tokens).GetFields().Select(item => item.GetValue(null).ToString()).ToArray(); |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Predicate function determining if an identifier string's content will trigger a result for the UseMeaningfulNames inspection. |
| 18 | + /// </summary> |
| 19 | + public static bool IsMeaningfulIdentifier(string name) |
| 20 | + => !TryMatchMeaninglessIdentifierCriteria(name, out _); |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// Evaluates if an identifier string's content will trigger a result for the UseMeaningfulNames inspection. |
| 24 | + /// </summary> |
| 25 | + /// <returns>Message indicating that the string will result in a UseMeaningfulNames inspection result</returns> |
| 26 | + public static bool TryMatchMeaninglessIdentifierCriteria(string name, out string criteriaMatchMessage) |
| 27 | + { |
| 28 | + criteriaMatchMessage = string.Empty; |
| 29 | + string Vowels = "aeiouyàâäéèêëïîöôùûü"; |
| 30 | + int MinimumNameLength = 3; |
| 31 | + |
| 32 | + bool HasVowel() |
| 33 | + => name.Any(character => Vowels.Any(vowel => |
| 34 | + string.Compare(vowel.ToString(CultureInfo.InvariantCulture), |
| 35 | + character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0)); |
| 36 | + |
| 37 | + bool HasConsonant() |
| 38 | + => !name.All(character => Vowels.Any(vowel => |
| 39 | + string.Compare(vowel.ToString(CultureInfo.InvariantCulture), |
| 40 | + character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0)); |
| 41 | + |
| 42 | + bool IsRepeatedCharacter() |
| 43 | + { |
| 44 | + var firstLetter = name.First().ToString(CultureInfo.InvariantCulture); |
| 45 | + return name.All(a => string.Compare(a.ToString(CultureInfo.InvariantCulture), firstLetter, |
| 46 | + StringComparison.OrdinalIgnoreCase) == 0); |
| 47 | + } |
| 48 | + |
| 49 | + bool EndsWithDigit() |
| 50 | + => char.IsDigit(name.Last()); |
| 51 | + |
| 52 | + bool IsTooShort() |
| 53 | + => name.Length < MinimumNameLength; |
| 54 | + |
| 55 | + var isMeaningless = !(HasVowel() |
| 56 | + && HasConsonant() |
| 57 | + && !IsRepeatedCharacter() |
| 58 | + && !IsTooShort() |
| 59 | + && !EndsWithDigit()); |
| 60 | + |
| 61 | + if (isMeaningless) |
| 62 | + { |
| 63 | + criteriaMatchMessage = string.Format(RubberduckUI.MeaninglessNameCriteriaMatchFormat, name); |
| 64 | + } |
| 65 | + return isMeaningless; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Predicate function determining if an identifier string conforms to MS-VBAL naming requirements |
| 70 | + /// </summary> |
| 71 | + public static bool IsValidIdentifier(string name, DeclarationType declarationType) |
| 72 | + => !TryMatchInvalidIdentifierCriteria(name, declarationType, out _); |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// Evaluates an identifier string's conformance with MS-VBAL naming requirements. |
| 76 | + /// </summary> |
| 77 | + /// <returns>Message for first matching invalid identifier criteria. Or, an empty string if the identifier is valid</returns> |
| 78 | + public static bool TryMatchInvalidIdentifierCriteria(string name, DeclarationType declarationType, out string criteriaMatchMessage) |
| 79 | + { |
| 80 | + criteriaMatchMessage = string.Empty; |
| 81 | + |
| 82 | + var maxNameLength = declarationType.HasFlag(DeclarationType.Module) |
| 83 | + ? Declaration.MaxModuleNameLength : Declaration.MaxMemberNameLength; |
| 84 | + |
| 85 | + if (string.IsNullOrEmpty(name)) |
| 86 | + { |
| 87 | + criteriaMatchMessage = RubberduckUI.InvalidNameCriteria_IsNullOrEmpty; |
| 88 | + return true; |
| 89 | + } |
| 90 | + |
| 91 | + //Does not start with a letter |
| 92 | + if (!char.IsLetter(name.First())) |
| 93 | + { |
| 94 | + criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_DoesNotStartWithLetterFormat, name); |
| 95 | + return true; |
| 96 | + } |
| 97 | + |
| 98 | + //Has special characters |
| 99 | + if (name.Any(c => !char.IsLetterOrDigit(c) && c != '_')) |
| 100 | + { |
| 101 | + criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_InvalidCharactersFormat, name); |
| 102 | + return true; |
| 103 | + } |
| 104 | + |
| 105 | + //Is a reserved identifier |
| 106 | + if (ReservedIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase)) |
| 107 | + { |
| 108 | + criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name); |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + //"VBA" identifier not allowed for projects |
| 113 | + if (declarationType.HasFlag(DeclarationType.Project) |
| 114 | + && name.Equals("VBA", StringComparison.InvariantCultureIgnoreCase)) |
| 115 | + { |
| 116 | + criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name); |
| 117 | + return true; |
| 118 | + } |
| 119 | + |
| 120 | + //Exceeds max length |
| 121 | + if (name.Length > maxNameLength) |
| 122 | + { |
| 123 | + criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_ExceedsMaximumLengthFormat, name); |
| 124 | + return true; |
| 125 | + } |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments