Skip to content

Commit

Permalink
Update NamingSettings and DocumentationSettings to keep one Regex ins…
Browse files Browse the repository at this point in the history
…tance instead of calling Regex.IsMatch

DotNetAnalyzers#3629
  • Loading branch information
bjornhellander committed Apr 15, 2023
1 parent ccaac20 commit 9005888
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ internal class DocumentationSettings
/// </summary>
internal static readonly ImmutableArray<string> DefaultExcludeFromPunctuationCheck = ImmutableArray.Create("seealso");

/// <summary>
/// This is a Regex instance matching valid variables.
/// </summary>
private static readonly Regex ValidVariableNameRegex = new Regex("^[a-zA-Z0-9]+$");

/// <summary>
/// This is the backing field for the <see cref="CompanyName"/> property.
/// </summary>
Expand Down Expand Up @@ -199,7 +204,7 @@ protected internal DocumentationSettings(JsonObject documentationSettingsObject,
{
string name = child.Key;

if (!Regex.IsMatch(name, "^[a-zA-Z0-9]+$"))
if (!ValidVariableNameRegex.IsMatch(name))
{
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ namespace StyleCop.Analyzers.Settings.ObjectModel

internal class NamingSettings
{
private static readonly Regex ValidHungarianPrefixRegex = new Regex("^[a-z]{1,2}$");

/// <summary>
/// Initializes a new instance of the <see cref="NamingSettings"/> class.
/// </summary>
Expand Down Expand Up @@ -55,7 +57,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi
{
var prefix = prefixJsonValue.ToStringValue(kvp.Key);

if (!Regex.IsMatch(prefix, "^[a-z]{1,2}$"))
if (!ValidHungarianPrefixRegex.IsMatch(prefix))
{
continue;
}
Expand Down Expand Up @@ -86,7 +88,7 @@ protected internal NamingSettings(JsonObject namingSettingsObject, AnalyzerConfi

allowCommonHungarianPrefixes ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "stylecop.naming.allowCommonHungarianPrefixes");
allowedHungarianPrefixes ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedHungarianPrefixes")
?.Where(value => Regex.IsMatch(value, "^[a-z]{1,2}$"))
?.Where(value => ValidHungarianPrefixRegex.IsMatch(value))
.ToImmutableArray()
.ToBuilder();
allowedNamespaceComponents ??= AnalyzerConfigHelper.TryGetStringListValue(analyzerConfigOptions, "stylecop.naming.allowedNamespaceComponents")?.ToBuilder();
Expand Down

0 comments on commit 9005888

Please sign in to comment.