Skip to content

Commit

Permalink
cleaned up
Browse files Browse the repository at this point in the history
  • Loading branch information
retailcoder committed Apr 23, 2021
1 parent 3b0d094 commit 43a4d41
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
6 changes: 1 addition & 5 deletions Rubberduck.Core/UI/Refactorings/ExtractMethodDialog.cs
Expand Up @@ -195,13 +195,9 @@ public string MethodName

private void ValidateName()
{
var tokenValues = typeof(Tokens).GetFields()
.Where(item => !item.GetCustomAttributes<NotReservedAttribute>().Any())
.Select(item => item.GetValue(null)).Cast<string>().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;
Expand Down
14 changes: 4 additions & 10 deletions Rubberduck.Refactorings/Common/VBAIdentifierValidator.cs
Expand Up @@ -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<string> ReservedIdentifiers =
typeof(Tokens).GetFields()
.Where(item => item.GetType().GetCustomAttributes<ForbiddenAttribute>().Any())
.Select(item => item.GetValue(null).ToString()).ToArray();

/// <summary>
/// Predicate function determining if an identifier string's content will trigger a result for the UseMeaningfulNames inspection.
Expand Down Expand Up @@ -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;
Expand All @@ -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<string>() { "Name" });
var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List<string>() { "Name" });

if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
Expand Down Expand Up @@ -182,7 +176,7 @@ public static IReadOnlyList<string> 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));
}
Expand All @@ -194,7 +188,7 @@ public static IReadOnlyList<string> 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<string>() { "Name" });
var invalidUDTArrayIdentifiers = Tokens.IllegalIdentifierNames.Concat(new List<string>() { "Name" });

if (invalidUDTArrayIdentifiers.Contains(name, StringComparer.InvariantCultureIgnoreCase))
{
Expand Down
21 changes: 14 additions & 7 deletions 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
{
/// <summary>
/// Identifies a static <see cref="Tokens"/> string that isn't a legal identifier name for user code, e.g. keyword or reserved identifier.
/// </summary>
[AttributeUsage(AttributeTargets.Field)]
public class ForbiddenAttribute : Attribute { }

[SuppressMessage("ReSharper", "InconsistentNaming")]
public static class Tokens
{
public static IEnumerable<string> IllegalIdentifierNames =>
typeof(Tokens).GetFields().Where(item => item.GetCustomAttributes<ForbiddenAttribute>().Any())
.Select(item => item.GetValue(null)).Cast<string>().Select(item => item);

[Forbidden]
public static readonly string Abs = "Abs";
[Forbidden]
Expand Down Expand Up @@ -374,11 +388,4 @@ public static class Tokens
public static readonly string XOr = "Xor";
public static readonly string Year = "Year";
}

/// <summary>
/// Identifies a static <see cref="Tokens"/> string that isn't a legal identifier name for user code, e.g. keyword or reserved identifier.
/// </summary>
public class ForbiddenAttribute : Attribute
{
}
}

0 comments on commit 43a4d41

Please sign in to comment.