-
Notifications
You must be signed in to change notification settings - Fork 295
/
VBAIdentifierValidator.cs
215 lines (183 loc) · 9.65 KB
/
VBAIdentifierValidator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
using Rubberduck.Parsing.Grammar;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Resources;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using Tokens = Rubberduck.Resources.Tokens;
namespace Rubberduck.Refactorings.Common
{
public static class VBAIdentifierValidator
{
/// <summary>
/// Predicate function determining if an identifier string's content will trigger a result for the UseMeaningfulNames inspection.
/// </summary>
public static bool IsMeaningfulIdentifier(string name)
=> !TryMatchMeaninglessIdentifierCriteria(name, out _);
/// <summary>
/// Evaluates if an identifier string's content will trigger a result for the UseMeaningfulNames inspection.
/// </summary>
/// <returns>Message indicating that the string will result in a UseMeaningfulNames inspection result</returns>
public static bool TryMatchMeaninglessIdentifierCriteria(string name, out string criteriaMatchMessage)
{
criteriaMatchMessage = string.Empty;
string Vowels = "aeiouyàâäéèêëïîöôùûü";
int MinimumNameLength = 3;
bool HasVowel()
=> name.Any(character => Vowels.Any(vowel =>
string.Compare(vowel.ToString(CultureInfo.InvariantCulture),
character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0));
bool HasConsonant()
=> !name.All(character => Vowels.Any(vowel =>
string.Compare(vowel.ToString(CultureInfo.InvariantCulture),
character.ToString(CultureInfo.InvariantCulture), StringComparison.OrdinalIgnoreCase) == 0));
bool IsRepeatedCharacter()
{
var firstLetter = name.First().ToString(CultureInfo.InvariantCulture);
return name.All(a => string.Compare(a.ToString(CultureInfo.InvariantCulture), firstLetter,
StringComparison.OrdinalIgnoreCase) == 0);
}
bool EndsWithDigit()
=> char.IsDigit(name.Last());
bool IsTooShort()
=> name.Length < MinimumNameLength;
var isMeaningless = !(HasVowel()
&& HasConsonant()
&& !IsRepeatedCharacter()
&& !IsTooShort()
&& !EndsWithDigit());
if (isMeaningless)
{
criteriaMatchMessage = string.Format(RubberduckUI.MeaninglessNameCriteriaMatchFormat, name);
}
return isMeaningless;
}
/// <summary>
/// Predicate function determining if an identifier string conforms to MS-VBAL naming requirements
/// </summary>
public static bool IsValidIdentifier(string name, DeclarationType declarationType, bool isArrayDeclaration = false)
=> !TryMatchInvalidIdentifierCriteria(name, declarationType, out _, isArrayDeclaration);
/// <summary>
/// Evaluates an identifier string's conformance with MS-VBAL naming requirements.
/// </summary>
/// <returns>Message for first matching invalid identifier criteria. Or, an empty string if the identifier is valid</returns>
public static bool TryMatchInvalidIdentifierCriteria(string name, DeclarationType declarationType, out string criteriaMatchMessage, bool isArrayDeclaration = false)
{
criteriaMatchMessage = string.Empty;
var maxNameLength = declarationType.HasFlag(DeclarationType.Module)
? Declaration.MaxModuleNameLength : Declaration.MaxMemberNameLength;
if (string.IsNullOrEmpty(name))
{
criteriaMatchMessage = RubberduckUI.InvalidNameCriteria_IsNullOrEmpty;
return true;
}
//Does not start with a letter
if (!char.IsLetter(name.First()))
{
criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_DoesNotStartWithLetterFormat, name);
return true;
}
//Has special characters
if (name.Any(c => !char.IsLetterOrDigit(c) && c != '_'))
{
criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_InvalidCharactersFormat, name);
return true;
}
//Is a reserved identifier
if (!declarationType.HasFlag(DeclarationType.UserDefinedTypeMember))
{
if (Tokens.IllegalIdentifierNames.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name);
return true;
}
}
else if (isArrayDeclaration) //is a DeclarationType.UserDefinedTypeMember
{
//DeclarationType.UserDefinedTypeMember can have reserved identifier keywords
//...unless the declaration is an array. Adding the parentheses causes errors.
//Name is not a reserved identifier, but when used as a UDTMember array declaration
//it collides with the 'Name' Statement (Renames a disk file, directory, or folder)
var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List<string>() { "Name" });
if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name);
return true;
}
}
//"VBA" identifier not allowed for projects
if (declarationType.HasFlag(DeclarationType.Project)
&& name.Equals("VBA", StringComparison.InvariantCultureIgnoreCase))
{
criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name);
return true;
}
//Exceeds max length
if (name.Length > maxNameLength)
{
criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_ExceedsMaximumLengthFormat, name);
return true;
}
return false;
}
/// <summary>
/// Evaluates an identifier string's conformance with MS-VBAL naming requirements.
/// </summary>
/// <returns>Messages for all matching invalid identifier criteria</returns>
public static IReadOnlyList<string> SatisfiedInvalidIdentifierCriteria(string name, DeclarationType declarationType, bool isArrayDeclaration = false)
{
var criteriaMatchMessages = new List<string>();
var maxNameLength = declarationType.HasFlag(DeclarationType.Module)
? Declaration.MaxModuleNameLength
: Declaration.MaxMemberNameLength;
if (string.IsNullOrEmpty(name))
{
criteriaMatchMessages.Add(RubberduckUI.InvalidNameCriteria_IsNullOrEmpty);
}
//Does not start with a letter
if (!char.IsLetter(name.First()))
{
criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_DoesNotStartWithLetterFormat, name));
}
//Has special characters
if (name.Any(c => !char.IsLetterOrDigit(c) && c != '_'))
{
criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_InvalidCharactersFormat, name));
}
//Is a reserved identifier
if (!declarationType.HasFlag(DeclarationType.UserDefinedTypeMember))
{
if (Tokens.IllegalIdentifierNames.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name));
}
}
else if (isArrayDeclaration) //is a DeclarationType.UserDefinedTypeMember
{
//DeclarationType.UserDefinedTypeMember can have reserved identifier keywords
//...unless the declaration is an array. Adding the parentheses causes errors.
//Name is not a reserved identifier, but when used as a UDTMember array declaration
//it collides with the 'Name' Statement (Renames a disk file, directory, or folder)
var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List<string>() { "Name" });
if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name));
}
}
//"VBA" identifier not allowed for projects
if (declarationType.HasFlag(DeclarationType.Project)
&& name.Equals("VBA", StringComparison.InvariantCultureIgnoreCase))
{
criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name));
}
//Exceeds max length
if (name.Length > maxNameLength)
{
criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_ExceedsMaximumLengthFormat, name));
}
return criteriaMatchMessages;
}
}
}