Skip to content

Commit

Permalink
Applied suggestions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Corniel committed Jul 20, 2023
1 parent 63af105 commit 4b57451
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 31 deletions.
20 changes: 0 additions & 20 deletions analyzers/src/SonarAnalyzer.Common/Facade/ILanguageFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,3 @@ public interface ILanguageFacade<TSyntaxKind> : ILanguageFacade
ISyntaxKindFacade<TSyntaxKind> SyntaxKind { get; }
ITrackerFacade<TSyntaxKind> Tracker { get; }
}

public static class LanguageFacadeExtensions
{
public static TEnum? FindConstantEnum<TEnum>(this ILanguageFacade facade, SemanticModel model, SyntaxNode node) where TEnum : struct
{
var value = facade.FindConstantValue(model, node);
if (value is TEnum @enum)
{
return @enum;
}
else if (value is int || value is long)
{
return (TEnum)value;
}
else
{
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,14 @@ public RegexContext(SyntaxNode patternNode, string pattern, SyntaxNode optionsNo
pattern,
language.FindConstantValue(model, pattern) as string,
options,
language.FindConstantEnum<RegexOptions>(model, options));
FindRegexOptions(language, model, options));
}

private static RegexOptions? FindRegexOptions<TSyntaxKind>(ILanguageFacade<TSyntaxKind> language, SemanticModel model, SyntaxNode options) where TSyntaxKind : struct =>
language.FindConstantValue(model, options) is int constant
? (RegexOptions)constant
: null;

private static SyntaxNode TryGetNonParamsSyntax(IMethodSymbol method, IMethodParameterLookup parameters, string paramName) =>
method.Parameters.SingleOrDefault(x => x.Name == paramName) is { } param
&& parameters.TryGetNonParamsSyntax(param, out var node)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ namespace SonarAnalyzer.Rules;
public abstract class RegexShouldNotContainMultipleSpacesBase<TSyntaxKind> : RegexAnalyzerBase<TSyntaxKind>
where TSyntaxKind : struct
{
private const string DiagnosticId = "S6326";
private const string DiagnosticId = "S101";

protected sealed override string MessageFormat => "Regular expressions should not contain multiple spaces.";

protected RegexShouldNotContainMultipleSpacesBase() : base(DiagnosticId) { }

protected sealed override void Analyze(SonarSyntaxNodeReportingContext context, RegexContext regexContext)
{
if (regexContext?.Regex is { }
if (regexContext?.Regex is not null
&& !IgnoresPatternWhitespace(regexContext)
&& regexContext.Pattern.Contains(" "))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ void Static()
var noRegex = NoRegex.IsMatch("some input", "multiple white spaces"); // Compliant
}

void Unknown(string unknown)
{
var regex = new NoRegex(unknown + "multiple white spaces"); // Compliant
}

bool ConcatanationMultiline(string input)
{
return Regex.IsMatch(input, "single space"
Expand Down Expand Up @@ -61,7 +56,7 @@ class Noncompliant
void Ctor()
{
var patternOnly = new Regex("multiple white spaces"); // Noncompliant {{Regular expressions should not contain multiple spaces.}}
// ^^^^^^^^^^^^^^^^^^^^^^^^^
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

var withConst = new Regex(Prefix + "multiple white spaces"); // Noncompliant
}
Expand Down Expand Up @@ -108,6 +103,19 @@ bool ConcatanationSingleIne(string input)
public string AttributeGloballySpecified { get; set; }
}

class Invalid
{
void FalseNegative(string unknown)
{
var regex = new NoRegex(unknown + "multiple white spaces"); // FN
}

void FalsePositive()
{
var withComment = new Regex("(?# comment with spaces)"); // Noncompliant, FP
}
}

class DoesNotCrash
{
bool UnknownVariable(string input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ End Class

Class Noncompliant
Private Sub Ctor()
Dim patternOnly = New Regex("multiple white spaces") ' Noncompliant {{Regular expressions should not contain multiple spaces.}}
' ^^^^^^^^^^^^^^^^^^^^^^^^^
Dim patternOnly = New Regex("multiple white spaces") ' Noncompliant {{Regular expressions should not contain multiple spaces.}}
' ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
End Sub

Private Sub [Static]()
Expand Down

0 comments on commit 4b57451

Please sign in to comment.