-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement S6326 for both C# and VB.NET #7345
base: master
Are you sure you want to change the base?
Conversation
b1d2709
to
f6f7b82
Compare
@pavel-mikula-sonarsource Any ETA on this? |
f6f7b82
to
7c64d8a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some improvements suggested
protected override void Initialize(SonarAnalysisContext context) | ||
{ | ||
context.RegisterNodeAction( | ||
Language.GeneratedCodeRecognizer, | ||
c => Analyze(c, RegexContext.FromCtor(Language, c.SemanticModel, c.Node)), | ||
Language.SyntaxKind.ObjectCreationExpressions); | ||
|
||
context.RegisterNodeAction( | ||
Language.GeneratedCodeRecognizer, | ||
c => Analyze(c, RegexContext.FromMethod(Language, c.SemanticModel, c.Node)), | ||
Language.SyntaxKind.InvocationExpression); | ||
|
||
context.RegisterNodeAction( | ||
Language.GeneratedCodeRecognizer, | ||
c => Analyze(c, RegexContext.FromAttribute(Language, c.SemanticModel, c.Node)), | ||
Language.SyntaxKind.Attribute); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This whole thing will repeat in every single Regex rule and will be hard to change for new APIs.
Please create a new abstract base class RegexBase
or RegexAnalyzerBase
that will override and seal Initialize
and will provide abstract Analyze
method to override by this, and the other rule.
protected override void Initialize(SonarAnalysisContext context) | |
{ | |
context.RegisterNodeAction( | |
Language.GeneratedCodeRecognizer, | |
c => Analyze(c, RegexContext.FromCtor(Language, c.SemanticModel, c.Node)), | |
Language.SyntaxKind.ObjectCreationExpressions); | |
context.RegisterNodeAction( | |
Language.GeneratedCodeRecognizer, | |
c => Analyze(c, RegexContext.FromMethod(Language, c.SemanticModel, c.Node)), | |
Language.SyntaxKind.InvocationExpression); | |
context.RegisterNodeAction( | |
Language.GeneratedCodeRecognizer, | |
c => Analyze(c, RegexContext.FromAttribute(Language, c.SemanticModel, c.Node)), | |
Language.SyntaxKind.Attribute); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fully agree. But I was only repeating myself once. And some people say you should only start to refactor when you repeat yourself twice. ;)
Nevertheless, the setup so far was with a base class in mind, I now added it.
|
||
private void Analyze(SonarSyntaxNodeReportingContext c, RegexContext context) | ||
{ | ||
if (context?.Regex is { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick
if (context?.Regex is { } | |
if (context?.Regex is not null |
@@ -0,0 +1,124 @@ | |||
using System; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add a test case with regex comment (?# comment with spaces)
, mark it as FP and leave it like that
|
||
void Unknown(string unknown) | ||
{ | ||
var regex = new NoRegex(unknown + "multiple white spaces"); // Compliant |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a FN, because no matter what the unknown
part does, there are multiple spaces
...s/SonarAnalyzer.UnitTest/TestCases/RegularExpressions/RegexShouldNotContainMultipleSpaces.cs
Outdated
Show resolved
Hide resolved
|| Regex.IsMatch(input, "ignore pattern white space", RegexOptions.IgnorePatternWhitespace); | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's document other white-spacing characters? Like hardspace, no-break space and others from UnicodeCategory.SpaceSeparator?
See https://learn.microsoft.com/en-us/dotnet/api/system.char.iswhitespace?view=net-7.0#remarks
I guess 0x09, 0xA0, 0xD0 should not be checked.
@@ -44,3 +44,23 @@ public interface ILanguageFacade<TSyntaxKind> : ILanguageFacade | |||
ISyntaxKindFacade<TSyntaxKind> SyntaxKind { get; } | |||
ITrackerFacade<TSyntaxKind> Tracker { get; } | |||
} | |||
|
|||
public static class LanguageFacadeExtensions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The concept of extensions of LangaugeFacade doesn't feel right. It's like dependencies going in both directions.
Why not change just is RegexOptions
to is int
and then cast it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added (and rebased) a set of whitespaces that are fine in regexes.
06c9b7e
to
990ac77
Compare
4b57451
to
8c2714d
Compare
/azp run |
Azure Pipelines successfully started running 1 pipeline(s). |
Quality Gate passedIssues Measures |
|
8c2714d
to
51b6b16
Compare
Failing test has been fixed. |
…sions/RegexShouldNotContainMultipleSpaces.cs Co-authored-by: Pavel Mikula <[email protected]>
51b6b16
to
6600459
Compare
Rebase done. |
What is the status here? |
The rule already exists for Java, and with the work done for S5856 it not that much work.