-
Notifications
You must be signed in to change notification settings - Fork 94
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
Adding rule SARIF2001 #1929
Adding rule SARIF2001 #1929
Changes from 1 commit
e32333a
4e6c09a
31d2b52
881f1b7
e8d2b45
10819c3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,85 +3,106 @@ | |
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.RegularExpressions; | ||
|
||
using Microsoft.Json.Pointer; | ||
|
||
namespace Microsoft.CodeAnalysis.Sarif.Multitool.Rules | ||
{ | ||
public class AuthorHighQualityMessages : SarifValidationSkimmerBase | ||
{ | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString | ||
{ | ||
Text = RuleResources.SARIF2001_AuthorHighQualityMessages_FullDescription_Text | ||
/// <summary> | ||
/// SARIF2001 | ||
/// </summary> | ||
public override string Id => RuleId.AuthorHighQualityMessages; | ||
|
||
/// <summary> | ||
/// Placeholder (full description). | ||
/// </summary> | ||
public override MultiformatMessageString FullDescription => new MultiformatMessageString { Text = RuleResources.SARIF2001_AuthorHighQualityMessages_FullDescription_Text }; | ||
|
||
protected override IEnumerable<string> MessageResourceNames => new string[] { | ||
nameof(RuleResources.SARIF2001_AuthorHighQualityMessages_Warning_EnquoteDynamicContent_Text), | ||
nameof(RuleResources.SARIF2001_AuthorHighQualityMessages_Warning_IncludeDynamicContent_Text), | ||
nameof(RuleResources.SARIF2001_AuthorHighQualityMessages_Warning_TerminateWithPeriod_Text) | ||
}; | ||
|
||
public override FailureLevel DefaultLevel => FailureLevel.Warning; | ||
|
||
public override string Id => RuleId.AuthorHighQualityMessages; | ||
private static readonly Regex s_dynamicContentRegex = new Regex(@"{[0-9]+}", RegexOptions.Compiled | RegexOptions.CultureInvariant); | ||
private static readonly Regex s_enquoteDynamicContentRegex = new Regex(@"'{[0-9]+}'", RegexOptions.Compiled | RegexOptions.CultureInvariant); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
How about this:
If the start of the line (
|
||
|
||
protected override IEnumerable<string> MessageResourceNames | ||
protected override void Analyze(Tool tool, string toolPointer) | ||
{ | ||
get | ||
if (tool.Driver != null) | ||
{ | ||
return new string[] | ||
{ | ||
nameof(RuleResources.SARIF2001_AuthorHighQualityMessages_Warning_TerminateWithPeriod_Text) | ||
}; | ||
AnalyzeToolDriver(tool.Driver, toolPointer.AtProperty(SarifPropertyName.Driver)); | ||
} | ||
} | ||
|
||
protected override void Analyze(ReportingDescriptor reportingDescriptor, string reportingDescriptorPointer) | ||
private void AnalyzeToolDriver(ToolComponent toolComponent, string toolDriverPointer) | ||
{ | ||
AnalyzeMessageStrings(reportingDescriptor.MessageStrings, reportingDescriptorPointer); | ||
if (toolComponent.Rules != null && toolComponent.Rules.Count > 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You don't need this check because if Rules is empty, then the |
||
{ | ||
foreach (ReportingDescriptor rule in toolComponent.Rules) | ||
{ | ||
AnalyzeReportingDescriptor(rule, toolDriverPointer.AtProperty(SarifPropertyName.Rules)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You actually want to point to the specific rule, say, the rule at index 0. To do that:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The result will be In reply to: 445100702 [](ancestors = 445100702) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok. just changed to a In reply to: 445101027 [](ancestors = 445101027,445100702) |
||
} | ||
} | ||
} | ||
|
||
private void AnalyzeMessageStrings( | ||
IDictionary<string, MultiformatMessageString> messageStrings, | ||
string reportingDescriptorPointer) | ||
private void AnalyzeReportingDescriptor(ReportingDescriptor rule, string reportingDescriptorPointer) | ||
{ | ||
if (messageStrings != null) | ||
if (rule.MessageStrings != null && rule.MessageStrings.Count > 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This check isn't needed. #Closed |
||
{ | ||
string messageStringsPointer = reportingDescriptorPointer.AtProperty(SarifPropertyName.MessageStrings); | ||
|
||
foreach (string key in messageStrings.Keys) | ||
foreach (KeyValuePair<string, MultiformatMessageString> message in rule.MessageStrings) | ||
{ | ||
MultiformatMessageString messageString = messageStrings[key]; | ||
|
||
string messageStringPointer = messageStringsPointer.AtProperty(key); | ||
|
||
AnalyzeMessageString(messageString.Text, messageStringPointer, SarifPropertyName.Text); | ||
AnalyzeMessageString(message.Value.Text, messageStringsPointer.AtProperty(message.Key)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You'll also need to pass the message.Key to |
||
} | ||
} | ||
} | ||
|
||
protected override void Analyze(MultiformatMessageString multiformatMessageString, string multiformatMessageStringPointer) | ||
private void AnalyzeMessageString(string messageString, string messagePointer) | ||
{ | ||
AnalyzeMessageString(multiformatMessageString.Text, multiformatMessageStringPointer, SarifPropertyName.Text); | ||
} | ||
if (string.IsNullOrEmpty(messageString)) | ||
{ | ||
return; | ||
} | ||
|
||
protected override void Analyze(Message message, string messagePointer) | ||
{ | ||
AnalyzeMessageString(message.Text, messagePointer, SarifPropertyName.Text); | ||
} | ||
string textPointer = messagePointer.AtProperty(SarifPropertyName.Text); | ||
|
||
private void AnalyzeMessageString( | ||
string messageString, | ||
string messagePointer, | ||
string propertyName) | ||
{ | ||
if (!string.IsNullOrEmpty(messageString) && DoesNotEndWithPeriod(messageString)) | ||
// IncludeDynamicContent: check if messageString has dynamic content | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The comment is a complete sentence, so end in a period. (Same for comments below.) #Closed |
||
if (!s_dynamicContentRegex.IsMatch(messageString)) | ||
{ | ||
// {0}: Placeholder '{1}' | ||
LogResult( | ||
textPointer, | ||
nameof(RuleResources.SARIF2001_AuthorHighQualityMessages_Warning_IncludeDynamicContent_Text), | ||
messageString | ||
); | ||
} | ||
|
||
// EnquoteDynamicContent: check if messageString has enquoted dynamic content | ||
if (!s_enquoteDynamicContentRegex.IsMatch(messageString)) | ||
{ | ||
string textPointer = messagePointer.AtProperty(propertyName); | ||
// {0}: Placeholder '{1}' | ||
LogResult( | ||
textPointer, | ||
nameof(RuleResources.SARIF2001_AuthorHighQualityMessages_Warning_EnquoteDynamicContent_Text), | ||
messageString | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Put closing paren on same line as the last argument. You did it correctly on Line 104 below. #Closed |
||
} | ||
|
||
// TerminateWithPeriod: check if messageString ends with period | ||
if (!messageString.EndsWith(".", StringComparison.Ordinal)) | ||
{ | ||
// {0}: Placeholder '{1}' | ||
LogResult( | ||
textPointer, | ||
nameof(RuleResources.SARIF2001_AuthorHighQualityMessages_Warning_TerminateWithPeriod_Text), | ||
messageString); | ||
} | ||
} | ||
|
||
private static bool DoesNotEndWithPeriod(string message) | ||
{ | ||
return message != null && !message.EndsWith(".", StringComparison.Ordinal); | ||
} | ||
} | ||
} |
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.
Curly braces
{
and}
are regular expression metacharacters, so you must escape them:@"\{[0-9]+\}"
#Closed