Skip to content
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

Fix linting issues #792

Merged
merged 7 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Cake.Issues.InspectCode/InspectCodeIssuesProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private static IssuePriority GetPriority(string severity) =>
/// <summary>
/// Description of an issue type.
/// </summary>
private class IssueType
private sealed class IssueType
{
/// <summary>
/// Gets the description of the issue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private static string GetFilePath(
#pragma warning disable CS0649 // Field 'field' is never assigned to, and will always have its default value 'value'

[DataContract]
private class LogFileEntry
private sealed class LogFileEntry
{
[DataMember]
public string fileName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
/// Logfile format as written by markdownlint-cli.
/// </summary>
/// <param name="log">The Cake log instance.</param>
internal class MarkdownlintCliLogFileFormat(ICakeLog log)
internal partial class MarkdownlintCliLogFileFormat(ICakeLog log)
: BaseMarkdownlintLogFileFormat(log)
{
private static readonly string[] Separator = ["\r\n", "\r", "\n"];
Expand All @@ -25,9 +25,9 @@ public override IEnumerable<IIssue> ReadIssues(
repositorySettings.NotNull();
markdownlintIssuesSettings.NotNull();

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

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

Expand Down Expand Up @@ -57,6 +57,9 @@ public override IEnumerable<IIssue> ReadIssues(
}
}

[GeneratedRegex(@"(?<filePath>.*[^:\d+]): ?(?<lineNumber>\d+):?(?<columnNumber>\d+)? (?<ruleId>MD\d+)/(?<ruleName>(?:\w*-*/*)*) (?<message>.*)")]
private static partial Regex LineParsingRegEx();

/// <summary>
/// Reads the affected file path from a parsed entry.
/// </summary>
Expand Down
7 changes: 5 additions & 2 deletions src/Cake.Issues.Markdownlint/MarkdownlintRuleUrlResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/// <summary>
/// Class for retrieving a URL linking to a site describing a rule.
/// </summary>
internal class MarkdownlintRuleUrlResolver : BaseRuleUrlResolver<MarkdownlintRuleDescription>
internal partial class MarkdownlintRuleUrlResolver : BaseRuleUrlResolver<MarkdownlintRuleDescription>
{
private static readonly Lazy<MarkdownlintRuleUrlResolver> InstanceValue =
new(() => new MarkdownlintRuleUrlResolver());
Expand All @@ -28,7 +28,7 @@ internal MarkdownlintRuleUrlResolver()
/// <inheritdoc/>
protected override bool TryGetRuleDescription(string rule, MarkdownlintRuleDescription ruleDescription)
{
var regex = new Regex(@"^MD(\d*)$");
var regex = RuleDescriptionRegEx();
var match = regex.Match(rule);

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

return true;
}

[GeneratedRegex(@"^MD(\d*)$")]
private static partial Regex RuleDescriptionRegEx();
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ public void Should_Throw_If_Settings_Are_Null()
public sealed class TheInternalCreateReportMethod
{
public static IEnumerable<object[]> ReportFormatSettingsCombinations =>
from b1 in new[] { false, true }
from b2 in new[] { false, true }
from b3 in new[] { false, true }
from b4 in new[] { false, true }
from b5 in new[] { false, true }
from b1 in boolArray
from b2 in boolArray
from b3 in boolArray
from b4 in boolArray
from b5 in boolArray
select new object[] { b1, b2, b3, b4, b5 };

private static readonly bool[] boolArray = [false, true];

[Theory]
[MemberData(nameof(ReportFormatSettingsCombinations))]
public void Should_Generate_Report(
Expand Down
50 changes: 25 additions & 25 deletions src/Cake.Issues.Reporting.Console/IssueDiagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ public IssueDiagnostic(IEnumerable<IIssue> issues)
this.CreateLabels();
}

/// <summary>
/// Returns the diagnostic location of an issue.
/// </summary>
/// <param name="issue">Issue for which the location should be returned.</param>
/// <returns>Location for the diagnostic.</returns>
private static (Location Location, int Lenght) GetLocation(IIssue issue)
{
// Errata currently doesn't support file or line level diagnostics.
if (!issue.Line.HasValue || !issue.Column.HasValue)
{
return default;
}

var location = new Location(issue.Line.Value, issue.Column.Value);

var length = 0;
if (issue.EndColumn.HasValue)
{
length = issue.EndColumn.Value - issue.Column.Value;
}

return (location, length);
}

/// <summary>
/// Creates labels for the issue.
/// </summary>
Expand All @@ -68,7 +92,7 @@ private void CreateLabels()

foreach (var issue in this.issues)
{
var (location, length) = this.GetLocation(issue);
var (location, length) = GetLocation(issue);
var label =
new Label(
issue.AffectedFileRelativePath.FullPath,
Expand All @@ -84,28 +108,4 @@ private void CreateLabels()
this.Labels.Add(label);
}
}

/// <summary>
/// Returns the diagnostic location of an issue.
/// </summary>
/// <param name="issue">Issue for which the location should be returned.</param>
/// <returns>Location for the diagnostic.</returns>
private (Location Location, int Lenght) GetLocation(IIssue issue)
{
// Errata currently doesn't support file or line level diagnostics.
if (!issue.Line.HasValue || !issue.Column.HasValue)
{
return default;
}

var location = new Location(issue.Line.Value, issue.Column.Value);

var length = 0;
if (issue.EndColumn.HasValue)
{
length = issue.EndColumn.Value - issue.Column.Value;
}

return (location, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public sealed class TheGetCssFileNameMethod
{
public static IEnumerable<object[]> DevExtremeThemes()
{
foreach (var number in Enum.GetValues(typeof(DevExtremeTheme)))
foreach (var number in Enum.GetValues<DevExtremeTheme>())
{
yield return [number];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void Should_Set_Heading()
public sealed class TheThemeOption
{
public static IEnumerable<object[]> DevExtremeThemes() =>
from object number in Enum.GetValues(typeof(DevExtremeTheme))
from object number in Enum.GetValues<DevExtremeTheme>()
select new[] { number };

[Theory]
Expand Down
7 changes: 5 additions & 2 deletions src/Cake.Issues.Reporting.Generic/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <summary>
/// Extensions for <see cref="string"/>.
/// </summary>
public static class StringExtensions
public static partial class StringExtensions
{
/// <summary>
/// Sanitizes a string to be a valid HTML ID.
Expand All @@ -23,7 +23,7 @@ public static string SanitizeHtmlIdAttribute(this string input)
var firstLegalCharacter = GetIndexOfFirstLetter(input);
input = input[firstLegalCharacter..];

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

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

return input.Length;
}

[GeneratedRegex(@"/^[^a-z]+|[^\w:-]+")]
private static partial Regex SanitizeHtmlIdRegEx();
}
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ private Result GetResult(SarifIssue sarifIssue)
/// <summary>
/// Contains the <see cref="BaselineState"/> and <see cref="IIssue"/>.
/// </summary>
private class SarifIssue
private sealed class SarifIssue
{
/// <summary>
/// Gets the BaselineState for the <see cref="SarifIssue"/>.
Expand Down
13 changes: 7 additions & 6 deletions src/Cake.Issues.Tests/IssuesArgumentChecksTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void Should_Not_Throw_If_Value_Is_Valid(string value)
var values = new List<string> { value };

// When
values.NotNullOrEmpty("foo");
values.NotNullOrEmpty();

// Then
}
Expand Down Expand Up @@ -330,7 +330,7 @@ public void Should_Not_Throw_If_Value_Is_Empty()
var value = new List<int>();

// When
value.NotNullOrEmptyElement("foo");
value.NotNullOrEmptyElement();

// Then
}
Expand All @@ -340,12 +340,13 @@ public void Should_Throw_With_Correct_ParameterName_If_ParameterName_Is_Passed_A
{
// Given
var value = new List<string> { null };
const string parameterName = "foo";

// When
var result = Record.Exception(() => value.NotNullOrEmptyElement("foo"));
var result = Record.Exception(() => value.NotNullOrEmptyElement(parameterName));

// Then
result.IsArgumentOutOfRangeException("foo");
result.IsArgumentOutOfRangeException(parameterName);
}

[Theory]
Expand All @@ -357,7 +358,7 @@ public void Should_Not_Throw_If_Value_Is_Valid(string value)
var values = new List<string> { value };

// When
values.NotNullOrEmptyElement("foo");
values.NotNullOrEmptyElement();

// Then
}
Expand Down Expand Up @@ -455,7 +456,7 @@ public void Should_Not_Throw_If_Value_Is_Valid(string value)
var values = new List<string> { value };

// When
values.NotNullOrEmptyOrEmptyElement("foo");
values.NotNullOrEmptyOrEmptyElement();

// Then
}
Expand Down
2 changes: 1 addition & 1 deletion src/Cake.Issues/IIssueComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ y is not null &&
/// <inheritdoc/>
public int GetHashCode(IIssue obj)
{
obj.NotNull(nameof(obj));
obj.NotNull();

var valuesToHash = new List<object>();

Expand Down
Loading