Skip to content

Commit a0be809

Browse files
authored
Fix linting issues (#792)
* Use static readonly fields instead constant arrays (CA1861) * Use generic overload (CA2263) * Use GeneratedRegexAttribute (SYSLIB1045) * Remove useless ToList calls (S2971) * Seal not inherited private classes (S3260) * Use caller information (S3236) * Make private methods static (S2325)
1 parent b1364f6 commit a0be809

12 files changed

Lines changed: 61 additions & 49 deletions

File tree

src/Cake.Issues.InspectCode/InspectCodeIssuesProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ private static IssuePriority GetPriority(string severity) =>
241241
/// <summary>
242242
/// Description of an issue type.
243243
/// </summary>
244-
private class IssueType
244+
private sealed class IssueType
245245
{
246246
/// <summary>
247247
/// Gets the description of the issue.

src/Cake.Issues.Markdownlint/LogFileFormat/MarkdownlintCliJsonLogFileFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ private static string GetFilePath(
102102
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'
103103

104104
[DataContract]
105-
private class LogFileEntry
105+
private sealed class LogFileEntry
106106
{
107107
[DataMember]
108108
public string fileName;

src/Cake.Issues.Markdownlint/LogFileFormat/MarkdownlintCliLogFileFormat.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
/// Logfile format as written by markdownlint-cli.
1111
/// </summary>
1212
/// <param name="log">The Cake log instance.</param>
13-
internal class MarkdownlintCliLogFileFormat(ICakeLog log)
13+
internal partial class MarkdownlintCliLogFileFormat(ICakeLog log)
1414
: BaseMarkdownlintLogFileFormat(log)
1515
{
1616
private static readonly string[] Separator = ["\r\n", "\r", "\n"];
@@ -25,9 +25,9 @@ public override IEnumerable<IIssue> ReadIssues(
2525
repositorySettings.NotNull();
2626
markdownlintIssuesSettings.NotNull();
2727

28-
var regex = new Regex(@"(?<filePath>.*[^:\d+]): ?(?<lineNumber>\d+):?(?<columnNumber>\d+)? (?<ruleId>MD\d+)/(?<ruleName>(?:\w*-*/*)*) (?<message>.*)");
28+
var regex = LineParsingRegEx();
2929

30-
foreach (var line in markdownlintIssuesSettings.LogFileContent.ToStringUsingEncoding().Split(Separator, StringSplitOptions.None).ToList().Where(s => !string.IsNullOrEmpty(s)))
30+
foreach (var line in markdownlintIssuesSettings.LogFileContent.ToStringUsingEncoding().Split(Separator, StringSplitOptions.None).Where(s => !string.IsNullOrEmpty(s)))
3131
{
3232
var groups = regex.Match(line).Groups;
3333

@@ -57,6 +57,9 @@ public override IEnumerable<IIssue> ReadIssues(
5757
}
5858
}
5959

60+
[GeneratedRegex(@"(?<filePath>.*[^:\d+]): ?(?<lineNumber>\d+):?(?<columnNumber>\d+)? (?<ruleId>MD\d+)/(?<ruleName>(?:\w*-*/*)*) (?<message>.*)")]
61+
private static partial Regex LineParsingRegEx();
62+
6063
/// <summary>
6164
/// Reads the affected file path from a parsed entry.
6265
/// </summary>

src/Cake.Issues.Markdownlint/MarkdownlintRuleUrlResolver.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/// <summary>
77
/// Class for retrieving a URL linking to a site describing a rule.
88
/// </summary>
9-
internal class MarkdownlintRuleUrlResolver : BaseRuleUrlResolver<MarkdownlintRuleDescription>
9+
internal partial class MarkdownlintRuleUrlResolver : BaseRuleUrlResolver<MarkdownlintRuleDescription>
1010
{
1111
private static readonly Lazy<MarkdownlintRuleUrlResolver> InstanceValue =
1212
new(() => new MarkdownlintRuleUrlResolver());
@@ -28,7 +28,7 @@ internal MarkdownlintRuleUrlResolver()
2828
/// <inheritdoc/>
2929
protected override bool TryGetRuleDescription(string rule, MarkdownlintRuleDescription ruleDescription)
3030
{
31-
var regex = new Regex(@"^MD(\d*)$");
31+
var regex = RuleDescriptionRegEx();
3232
var match = regex.Match(rule);
3333

3434
if (!match.Success)
@@ -52,4 +52,7 @@ protected override bool TryGetRuleDescription(string rule, MarkdownlintRuleDescr
5252

5353
return true;
5454
}
55+
56+
[GeneratedRegex(@"^MD(\d*)$")]
57+
private static partial Regex RuleDescriptionRegEx();
5558
}

src/Cake.Issues.Reporting.Console.Tests/ConsoleIssueReportGeneratorTests.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ public void Should_Throw_If_Settings_Are_Null()
3434
public sealed class TheInternalCreateReportMethod
3535
{
3636
public static IEnumerable<object[]> ReportFormatSettingsCombinations =>
37-
from b1 in new[] { false, true }
38-
from b2 in new[] { false, true }
39-
from b3 in new[] { false, true }
40-
from b4 in new[] { false, true }
41-
from b5 in new[] { false, true }
37+
from b1 in boolArray
38+
from b2 in boolArray
39+
from b3 in boolArray
40+
from b4 in boolArray
41+
from b5 in boolArray
4242
select new object[] { b1, b2, b3, b4, b5 };
4343

44+
private static readonly bool[] boolArray = [false, true];
45+
4446
[Theory]
4547
[MemberData(nameof(ReportFormatSettingsCombinations))]
4648
public void Should_Generate_Report(

src/Cake.Issues.Reporting.Console/IssueDiagnostic.cs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,30 @@ public IssueDiagnostic(IEnumerable<IIssue> issues)
5353
this.CreateLabels();
5454
}
5555

56+
/// <summary>
57+
/// Returns the diagnostic location of an issue.
58+
/// </summary>
59+
/// <param name="issue">Issue for which the location should be returned.</param>
60+
/// <returns>Location for the diagnostic.</returns>
61+
private static (Location Location, int Lenght) GetLocation(IIssue issue)
62+
{
63+
// Errata currently doesn't support file or line level diagnostics.
64+
if (!issue.Line.HasValue || !issue.Column.HasValue)
65+
{
66+
return default;
67+
}
68+
69+
var location = new Location(issue.Line.Value, issue.Column.Value);
70+
71+
var length = 0;
72+
if (issue.EndColumn.HasValue)
73+
{
74+
length = issue.EndColumn.Value - issue.Column.Value;
75+
}
76+
77+
return (location, length);
78+
}
79+
5680
/// <summary>
5781
/// Creates labels for the issue.
5882
/// </summary>
@@ -68,7 +92,7 @@ private void CreateLabels()
6892

6993
foreach (var issue in this.issues)
7094
{
71-
var (location, length) = this.GetLocation(issue);
95+
var (location, length) = GetLocation(issue);
7296
var label =
7397
new Label(
7498
issue.AffectedFileRelativePath.FullPath,
@@ -84,28 +108,4 @@ private void CreateLabels()
84108
this.Labels.Add(label);
85109
}
86110
}
87-
88-
/// <summary>
89-
/// Returns the diagnostic location of an issue.
90-
/// </summary>
91-
/// <param name="issue">Issue for which the location should be returned.</param>
92-
/// <returns>Location for the diagnostic.</returns>
93-
private (Location Location, int Lenght) GetLocation(IIssue issue)
94-
{
95-
// Errata currently doesn't support file or line level diagnostics.
96-
if (!issue.Line.HasValue || !issue.Column.HasValue)
97-
{
98-
return default;
99-
}
100-
101-
var location = new Location(issue.Line.Value, issue.Column.Value);
102-
103-
var length = 0;
104-
if (issue.EndColumn.HasValue)
105-
{
106-
length = issue.EndColumn.Value - issue.Column.Value;
107-
}
108-
109-
return (location, length);
110-
}
111111
}

src/Cake.Issues.Reporting.Generic.Tests/DevExtremeThemeExtensionsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public sealed class TheGetCssFileNameMethod
66
{
77
public static IEnumerable<object[]> DevExtremeThemes()
88
{
9-
foreach (var number in Enum.GetValues(typeof(DevExtremeTheme)))
9+
foreach (var number in Enum.GetValues<DevExtremeTheme>())
1010
{
1111
yield return [number];
1212
}

src/Cake.Issues.Reporting.Generic.Tests/HtmlDxDataGridTemplateTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public void Should_Set_Heading()
5050
public sealed class TheThemeOption
5151
{
5252
public static IEnumerable<object[]> DevExtremeThemes() =>
53-
from object number in Enum.GetValues(typeof(DevExtremeTheme))
53+
from object number in Enum.GetValues<DevExtremeTheme>()
5454
select new[] { number };
5555

5656
[Theory]

src/Cake.Issues.Reporting.Generic/StringExtensions.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/// <summary>
66
/// Extensions for <see cref="string"/>.
77
/// </summary>
8-
public static class StringExtensions
8+
public static partial class StringExtensions
99
{
1010
/// <summary>
1111
/// Sanitizes a string to be a valid HTML ID.
@@ -23,7 +23,7 @@ public static string SanitizeHtmlIdAttribute(this string input)
2323
var firstLegalCharacter = GetIndexOfFirstLetter(input);
2424
input = input[firstLegalCharacter..];
2525

26-
return Regex.Replace(input, @"/^[^a-z]+|[^\w:-]+", "-");
26+
return SanitizeHtmlIdRegEx().Replace(input, "-");
2727
}
2828

2929
/// <summary>
@@ -46,4 +46,7 @@ private static int GetIndexOfFirstLetter(string input)
4646

4747
return input.Length;
4848
}
49+
50+
[GeneratedRegex(@"/^[^a-z]+|[^\w:-]+")]
51+
private static partial Regex SanitizeHtmlIdRegEx();
4952
}

src/Cake.Issues.Reporting.Sarif/SarifIssueReportGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private Result GetResult(SarifIssue sarifIssue)
236236
/// <summary>
237237
/// Contains the <see cref="BaselineState"/> and <see cref="IIssue"/>.
238238
/// </summary>
239-
private class SarifIssue
239+
private sealed class SarifIssue
240240
{
241241
/// <summary>
242242
/// Gets the BaselineState for the <see cref="SarifIssue"/>.

0 commit comments

Comments
 (0)