Skip to content

Commit 43a4d41

Browse files
committed
cleaned up
1 parent 3b0d094 commit 43a4d41

File tree

3 files changed

+19
-22
lines changed

3 files changed

+19
-22
lines changed

Rubberduck.Core/UI/Refactorings/ExtractMethodDialog.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,9 @@ public string MethodName
195195

196196
private void ValidateName()
197197
{
198-
var tokenValues = typeof(Tokens).GetFields()
199-
.Where(item => !item.GetCustomAttributes<NotReservedAttribute>().Any())
200-
.Select(item => item.GetValue(null)).Cast<string>().Select(item => item);
201-
202198
OkButton.Enabled = MethodName != OldMethodName
203199
&& char.IsLetter(MethodName.FirstOrDefault())
204-
&& !tokenValues.Contains(MethodName, StringComparer.InvariantCultureIgnoreCase)
200+
&& !Tokens.IllegalIdentifierNames.Contains(MethodName, StringComparer.InvariantCultureIgnoreCase)
205201
&& !MethodName.Any(c => !char.IsLetterOrDigit(c) && c != '_');
206202

207203
InvalidNameValidationIcon.Visible = !OkButton.Enabled;

Rubberduck.Refactorings/Common/VBAIdentifierValidator.cs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ namespace Rubberduck.Refactorings.Common
1212
{
1313
public static class VBAIdentifierValidator
1414
{
15-
// NOTE: ForbiddenAttribute marks the tokens that don't compile as identifier names. Includes "bad but legal" names.
16-
// TODO: Compare with the unfiltered tokens if a client needs to tell "bad but legal" from "bad and illegal" names.
17-
private static readonly IEnumerable<string> ReservedIdentifiers =
18-
typeof(Tokens).GetFields()
19-
.Where(item => item.GetType().GetCustomAttributes<ForbiddenAttribute>().Any())
20-
.Select(item => item.GetValue(null).ToString()).ToArray();
2115

2216
/// <summary>
2317
/// Predicate function determining if an identifier string's content will trigger a result for the UseMeaningfulNames inspection.
@@ -111,7 +105,7 @@ public static bool TryMatchInvalidIdentifierCriteria(string name, DeclarationTyp
111105
//Is a reserved identifier
112106
if (!declarationType.HasFlag(DeclarationType.UserDefinedTypeMember))
113107
{
114-
if (ReservedIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
108+
if (Tokens.IllegalIdentifierNames.Contains(name, StringComparer.InvariantCultureIgnoreCase))
115109
{
116110
criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name);
117111
return true;
@@ -124,7 +118,7 @@ public static bool TryMatchInvalidIdentifierCriteria(string name, DeclarationTyp
124118

125119
//Name is not a reserved identifier, but when used as a UDTMember array declaration
126120
//it collides with the 'Name' Statement (Renames a disk file, directory, or folder)
127-
var invalidUDTArrayIdentifiers = ReservedIdentifiers.Concat(new List<string>() { "Name" });
121+
var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List<string>() { "Name" });
128122

129123
if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
130124
{
@@ -182,7 +176,7 @@ public static IReadOnlyList<string> SatisfiedInvalidIdentifierCriteria(string na
182176
//Is a reserved identifier
183177
if (!declarationType.HasFlag(DeclarationType.UserDefinedTypeMember))
184178
{
185-
if (ReservedIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
179+
if (Tokens.IllegalIdentifierNames.Contains(name, StringComparer.InvariantCultureIgnoreCase))
186180
{
187181
criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name));
188182
}
@@ -194,7 +188,7 @@ public static IReadOnlyList<string> SatisfiedInvalidIdentifierCriteria(string na
194188

195189
//Name is not a reserved identifier, but when used as a UDTMember array declaration
196190
//it collides with the 'Name' Statement (Renames a disk file, directory, or folder)
197-
var invalidUDTArrayIdentifiers = ReservedIdentifiers.Concat(new List<string>() { "Name" });
191+
var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List<string>() { "Name" });
198192

199193
if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
200194
{

Rubberduck.Resources/Tokens.cs

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
24
using System.Diagnostics.CodeAnalysis;
5+
using System.Linq;
6+
using System.Reflection;
37

48
namespace Rubberduck.Resources
59
{
10+
/// <summary>
11+
/// Identifies a static <see cref="Tokens"/> string that isn't a legal identifier name for user code, e.g. keyword or reserved identifier.
12+
/// </summary>
13+
[AttributeUsage(AttributeTargets.Field)]
14+
public class ForbiddenAttribute : Attribute { }
15+
616
[SuppressMessage("ReSharper", "InconsistentNaming")]
717
public static class Tokens
818
{
19+
public static IEnumerable<string> IllegalIdentifierNames =>
20+
typeof(Tokens).GetFields().Where(item => item.GetCustomAttributes<ForbiddenAttribute>().Any())
21+
.Select(item => item.GetValue(null)).Cast<string>().Select(item => item);
22+
923
[Forbidden]
1024
public static readonly string Abs = "Abs";
1125
[Forbidden]
@@ -374,11 +388,4 @@ public static class Tokens
374388
public static readonly string XOr = "Xor";
375389
public static readonly string Year = "Year";
376390
}
377-
378-
/// <summary>
379-
/// Identifies a static <see cref="Tokens"/> string that isn't a legal identifier name for user code, e.g. keyword or reserved identifier.
380-
/// </summary>
381-
public class ForbiddenAttribute : Attribute
382-
{
383-
}
384391
}

0 commit comments

Comments
 (0)