|
1 | 1 | using System.Globalization;
|
2 | 2 | using Rubberduck.Parsing.Grammar;
|
3 | 3 | using System;
|
| 4 | +using System.Collections.Generic; |
4 | 5 | using System.Linq;
|
5 |
| -using System.Text.RegularExpressions; |
6 | 6 |
|
7 | 7 | namespace Rubberduck.Inspections
|
8 | 8 | {
|
9 |
| - public class VariableNameValidator |
| 9 | + public static class VariableNameValidator |
10 | 10 | {
|
11 |
| - public VariableNameValidator() { } |
12 |
| - public VariableNameValidator(string identifier) { _identifier = identifier; } |
| 11 | + private static readonly string Vowels = "aeiouyàâäéèêëïîöôùûü"; |
| 12 | + private static readonly int MinVariableNameLength = 3; |
13 | 13 |
|
14 |
| - private const string AllVowels = "aeiouyàâäéèêëïîöôùûü"; |
15 |
| - private const int MinVariableNameLength = 3; |
16 |
| - |
17 |
| - /**** Meaningful Name Characteristics ************/ |
18 |
| - |
19 |
| - private bool HasVowels |
| 14 | + private static bool HasVowel(string name) |
20 | 15 | {
|
21 |
| - get |
22 |
| - { |
23 |
| - return _identifier.Any(character => AllVowels.Any(vowel => |
24 |
| - string.Compare(vowel.ToString(CultureInfo.InvariantCulture), |
25 |
| - character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0)); |
26 |
| - } |
| 16 | + return name.Any(character => Vowels.Any(vowel => |
| 17 | + string.Compare(vowel.ToString(CultureInfo.InvariantCulture), |
| 18 | + character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0)); |
27 | 19 | }
|
28 | 20 |
|
29 |
| - private bool HasConsonants |
| 21 | + private static bool HasConsonant(string name) |
30 | 22 | {
|
31 |
| - get |
32 |
| - { |
33 |
| - return !_identifier.All(character => AllVowels.Any(vowel => |
34 |
| - string.Compare(vowel.ToString(CultureInfo.InvariantCulture), |
35 |
| - character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0)); |
36 |
| - } |
| 23 | + return !name.All(character => Vowels.Any(vowel => |
| 24 | + string.Compare(vowel.ToString(CultureInfo.InvariantCulture), |
| 25 | + character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0)); |
37 | 26 | }
|
38 | 27 |
|
39 |
| - private bool IsSingleRepeatedLetter |
| 28 | + private static bool IsRepeatedCharacter(string name) |
40 | 29 | {
|
41 |
| - get |
42 |
| - { |
43 |
| - var firstLetter = _identifier.First().ToString(CultureInfo.InvariantCulture); |
44 |
| - return _identifier.All(a => string.Compare(a.ToString(CultureInfo.InvariantCulture), firstLetter, |
45 |
| - StringComparison.OrdinalIgnoreCase) == 0); |
46 |
| - } |
| 30 | + var firstLetter = name.First().ToString(CultureInfo.InvariantCulture); |
| 31 | + return name.All(a => string.Compare(a.ToString(CultureInfo.InvariantCulture), firstLetter, |
| 32 | + StringComparison.OrdinalIgnoreCase) == 0); |
47 | 33 | }
|
48 | 34 |
|
49 |
| - private bool IsTooShort { get { return _identifier.Length < MinVariableNameLength; } } |
50 |
| - private bool EndsWithNumber { get { return char.IsDigit(_identifier.Last()); } } |
51 |
| - |
52 |
| - /**** Invalid Name Characteristics ************/ |
53 |
| - public bool StartsWithNumber { get { return FirstLetterIsDigit(); } } |
54 |
| - |
55 |
| - public bool IsReservedName |
| 35 | + private static bool IsUnderMinLength(string name) |
56 | 36 | {
|
57 |
| - get |
58 |
| - { |
59 |
| - var tokenValues = typeof(Tokens).GetFields().Select(item => item.GetValue(null)).Cast<string>().Select(item => item); |
60 |
| - return tokenValues.Contains(_identifier, StringComparer.InvariantCultureIgnoreCase); |
61 |
| - } |
| 37 | + return name.Length < MinVariableNameLength; |
62 | 38 | }
|
63 | 39 |
|
64 |
| - public bool ContainsSpecialCharacters { get { return UsesSpecialCharacters(); } } |
65 |
| - |
66 |
| - private string _identifier; |
67 |
| - public string Identifier |
| 40 | + private static bool EndsWithDigit(string name) |
68 | 41 | {
|
69 |
| - get { return _identifier; } |
70 |
| - set { _identifier = value; } |
71 |
| - } |
| 42 | + return char.IsDigit(name.Last()); |
| 43 | + } |
72 | 44 |
|
73 |
| - public bool IsValidName() |
| 45 | + public static bool StartsWithDigit(string name) |
74 | 46 | {
|
75 |
| - return !string.IsNullOrEmpty(_identifier) |
76 |
| - && !StartsWithNumber |
77 |
| - && !IsReservedName |
78 |
| - && !ContainsSpecialCharacters; |
| 47 | + return !char.IsLetter(name.First()); |
79 | 48 | }
|
80 | 49 |
|
81 |
| - public bool IsMeaningfulName() |
| 50 | + private static readonly IEnumerable<string> ReservedNames = |
| 51 | + typeof (Tokens).GetFields().Select(item => item.GetValue(null).ToString()).ToArray(); |
| 52 | + |
| 53 | + public static bool IsReservedIdentifier(string name) |
82 | 54 | {
|
83 |
| - return HasVowels |
84 |
| - && HasConsonants |
85 |
| - && !IsSingleRepeatedLetter |
86 |
| - && !IsTooShort |
87 |
| - && !EndsWithNumber; |
| 55 | + return ReservedNames.Contains(name, StringComparer.InvariantCultureIgnoreCase); |
88 | 56 | }
|
89 | 57 |
|
90 |
| - public bool IsFoundIn(string input) |
| 58 | + public static bool HasSpecialCharacters(string name) |
91 | 59 | {
|
92 |
| - const string noAdjacentLettersNumbersOrUnderscores = "([^0-9a-zA-Z_])"; |
93 |
| - |
94 |
| - Regex rgxSurroundedBySpacesOrEndsTheString = new Regex("(\\s)" + _identifier.ToUpper() + "(\\s|\\z)"); |
95 |
| - |
96 |
| - Regex rgxNoAdjacentLettersNumbersOrUnderscores = new Regex(noAdjacentLettersNumbersOrUnderscores + _identifier.ToUpper() + noAdjacentLettersNumbersOrUnderscores); |
97 |
| - |
98 |
| - Regex rgxStartsTheString = new Regex("^" + _identifier.ToUpper() + noAdjacentLettersNumbersOrUnderscores); |
99 |
| - |
100 |
| - return rgxSurroundedBySpacesOrEndsTheString.IsMatch(input.ToUpper()) |
101 |
| - || rgxNoAdjacentLettersNumbersOrUnderscores.IsMatch(input.ToUpper()) |
102 |
| - || rgxStartsTheString.IsMatch(input.ToUpper()); |
| 60 | + return name.Any(c => !char.IsLetterOrDigit(c) && c != '_'); |
103 | 61 | }
|
104 | 62 |
|
105 |
| - private bool FirstLetterIsDigit() |
| 63 | + public static bool IsValidName(string name) |
106 | 64 | {
|
107 |
| - return !char.IsLetter(_identifier.FirstOrDefault()); |
| 65 | + return !string.IsNullOrEmpty(name) |
| 66 | + && !StartsWithDigit(name) |
| 67 | + && !IsReservedIdentifier(name) |
| 68 | + && !HasSpecialCharacters(name); |
108 | 69 | }
|
109 | 70 |
|
110 |
| - private bool UsesSpecialCharacters() |
| 71 | + public static bool IsMeaningfulName(string name) |
111 | 72 | {
|
112 |
| - return _identifier.Any(c => !char.IsLetterOrDigit(c) && c != '_'); |
| 73 | + return HasVowel(name) |
| 74 | + && HasConsonant(name) |
| 75 | + && !IsRepeatedCharacter(name) |
| 76 | + && !IsUnderMinLength(name) |
| 77 | + && !EndsWithDigit(name); |
113 | 78 | }
|
114 | 79 | }
|
115 | 80 | }
|
0 commit comments