diff --git a/Rubberduck.Core/UI/Refactorings/ExtractMethodDialog.cs b/Rubberduck.Core/UI/Refactorings/ExtractMethodDialog.cs index cd5e548bde..3921542d5d 100644 --- a/Rubberduck.Core/UI/Refactorings/ExtractMethodDialog.cs +++ b/Rubberduck.Core/UI/Refactorings/ExtractMethodDialog.cs @@ -195,13 +195,9 @@ public string MethodName private void ValidateName() { - var tokenValues = typeof(Tokens).GetFields() - .Where(item => !item.GetCustomAttributes().Any()) - .Select(item => item.GetValue(null)).Cast().Select(item => item); - OkButton.Enabled = MethodName != OldMethodName && char.IsLetter(MethodName.FirstOrDefault()) - && !tokenValues.Contains(MethodName, StringComparer.InvariantCultureIgnoreCase) + && !Tokens.IllegalIdentifierNames.Contains(MethodName, StringComparer.InvariantCultureIgnoreCase) && !MethodName.Any(c => !char.IsLetterOrDigit(c) && c != '_'); InvalidNameValidationIcon.Visible = !OkButton.Enabled; diff --git a/Rubberduck.Refactorings/Common/VBAIdentifierValidator.cs b/Rubberduck.Refactorings/Common/VBAIdentifierValidator.cs index bddc7141c7..06b5889867 100644 --- a/Rubberduck.Refactorings/Common/VBAIdentifierValidator.cs +++ b/Rubberduck.Refactorings/Common/VBAIdentifierValidator.cs @@ -12,12 +12,6 @@ namespace Rubberduck.Refactorings.Common { public static class VBAIdentifierValidator { - // NOTE: ForbiddenAttribute marks the tokens that don't compile as identifier names. Includes "bad but legal" names. - // TODO: Compare with the unfiltered tokens if a client needs to tell "bad but legal" from "bad and illegal" names. - private static readonly IEnumerable ReservedIdentifiers = - typeof(Tokens).GetFields() - .Where(item => item.GetType().GetCustomAttributes().Any()) - .Select(item => item.GetValue(null).ToString()).ToArray(); /// /// 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 //Is a reserved identifier if (!declarationType.HasFlag(DeclarationType.UserDefinedTypeMember)) { - if (ReservedIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase)) + if (Tokens.IllegalIdentifierNames.Contains(name, StringComparer.InvariantCultureIgnoreCase)) { criteriaMatchMessage = string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name); return true; @@ -124,7 +118,7 @@ public static bool TryMatchInvalidIdentifierCriteria(string name, DeclarationTyp //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 = ReservedIdentifiers.Concat(new List() { "Name" }); + var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List() { "Name" }); if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase)) { @@ -182,7 +176,7 @@ public static IReadOnlyList SatisfiedInvalidIdentifierCriteria(string na //Is a reserved identifier if (!declarationType.HasFlag(DeclarationType.UserDefinedTypeMember)) { - if (ReservedIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase)) + if (Tokens.IllegalIdentifierNames.Contains(name, StringComparer.InvariantCultureIgnoreCase)) { criteriaMatchMessages.Add(string.Format(RubberduckUI.InvalidNameCriteria_IsReservedKeywordFormat, name)); } @@ -194,7 +188,7 @@ public static IReadOnlyList SatisfiedInvalidIdentifierCriteria(string na //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 = ReservedIdentifiers.Concat(new List() { "Name" }); + var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List() { "Name" }); if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase)) { diff --git a/Rubberduck.Resources/Tokens.cs b/Rubberduck.Resources/Tokens.cs index 0e369f425a..5320476075 100644 --- a/Rubberduck.Resources/Tokens.cs +++ b/Rubberduck.Resources/Tokens.cs @@ -1,11 +1,25 @@ using System; +using System.Collections; +using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; +using System.Reflection; namespace Rubberduck.Resources { + /// + /// Identifies a static string that isn't a legal identifier name for user code, e.g. keyword or reserved identifier. + /// + [AttributeUsage(AttributeTargets.Field)] + public class ForbiddenAttribute : Attribute { } + [SuppressMessage("ReSharper", "InconsistentNaming")] public static class Tokens { + public static IEnumerable IllegalIdentifierNames => + typeof(Tokens).GetFields().Where(item => item.GetCustomAttributes().Any()) + .Select(item => item.GetValue(null)).Cast().Select(item => item); + [Forbidden] public static readonly string Abs = "Abs"; [Forbidden] @@ -374,11 +388,4 @@ public static class Tokens public static readonly string XOr = "Xor"; public static readonly string Year = "Year"; } - - /// - /// Identifies a static string that isn't a legal identifier name for user code, e.g. keyword or reserved identifier. - /// - public class ForbiddenAttribute : Attribute - { - } }