Skip to content

Commit

Permalink
Remove hardcoded IDE and CA types
Browse files Browse the repository at this point in the history
  • Loading branch information
FrediKats committed May 17, 2024
1 parent 179529f commit dcdf829
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public class EditorConfigDocumentParsingContext
private EditorConfigCategoryNode? _currentCategory;
private EditorConfigDocumentSectionNode? _currentSection;
private List<string> _currentTrivia;

public EditorConfigDocumentParsingContext()
{
_currentDocument = new EditorConfigDocument(ImmutableList<IEditorConfigNode>.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public EditorConfigFormatter(IDotnetConfigSettingsParser settingsParser)
public EditorConfigDocument Format(EditorConfigDocument value)
{
List<EditorConfigPropertyNode> nodesForRemoving = new List<EditorConfigPropertyNode>();
IReadOnlyCollection<EditorConfigPropertyNode> styleRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleType.StyleRule).OrderBy(r => r.Key.Value).ToList();
IReadOnlyCollection<EditorConfigPropertyNode> qualityRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleType.QualityRule).OrderBy(r => r.Key.Value).ToList();
IReadOnlyCollection<EditorConfigPropertyNode> styleRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.StyleRule).OrderBy(r => r.Key.Value).ToList();
IReadOnlyCollection<EditorConfigPropertyNode> qualityRuleNodesForMoving = SelectIdeNodes(value, RoslynRuleTypes.QualityRule).OrderBy(r => r.Key.Value).ToList();
nodesForRemoving.AddRange(styleRuleNodesForMoving);
nodesForRemoving.AddRange(qualityRuleNodesForMoving);

Expand Down Expand Up @@ -112,7 +112,7 @@ private EditorConfigCategoryNode CreateAutoGeneratedCategory(IReadOnlyCollection
return autoGeneratedSection;
}

private IReadOnlyCollection<EditorConfigPropertyNode> SelectIdeNodes(EditorConfigDocument document, RoslynRuleType roslynRuleType)
private IReadOnlyCollection<EditorConfigPropertyNode> SelectIdeNodes(EditorConfigDocument document, string ruleType)
{
List<EditorConfigPropertyNode> propertyNodes = document
.DescendantNodes()
Expand All @@ -126,7 +126,7 @@ private IReadOnlyCollection<EditorConfigPropertyNode> SelectIdeNodes(EditorConfi
if (editorConfigSetting is not RoslynSeverityEditorConfigSetting severityConfigSetting)
continue;

if (severityConfigSetting.RuleId.Type == roslynRuleType)
if (severityConfigSetting.RuleId.RuleType == ruleType)
styleRuleNodes.Add(editorConfigPropertyNode);
}

Expand Down
34 changes: 9 additions & 25 deletions Sources/Kysect.Configuin.RoslynModels/RoslynRuleId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,31 @@

namespace Kysect.Configuin.RoslynModels;

public readonly record struct RoslynRuleId(RoslynRuleType Type, int Id) : IComparable<RoslynRuleId>
public readonly record struct RoslynRuleId(string RuleType, int Id) : IComparable<RoslynRuleId>
{
public static RoslynRuleId Parse(string value)
{
value.ThrowIfNull();

int ParseInt(string intAsString)
{
if (int.TryParse(intAsString, out int parsedInt))
return parsedInt;
if (value.Length < 5)
throw new ArgumentException($"Invalid Roslyn rule ID {value}");

string code = value.Substring(value.Length - 4, 4);
if (!int.TryParse(code, out int parsedCode))
throw new ConfiguinException($"Value {value} is not valid rule identifier.");
}

// CA1234
string qualityRulePrefix = "CA";
if (value.StartsWith(qualityRulePrefix, StringComparison.InvariantCultureIgnoreCase))
{
string id = value.WithoutPrefix(qualityRulePrefix);
return new RoslynRuleId(RoslynRuleType.QualityRule, ParseInt(id));
}

// IDE1234
string styleRulePrefix = "IDE";
if (value.StartsWith(styleRulePrefix, StringComparison.InvariantCultureIgnoreCase))
{
string id = value.WithoutPrefix(styleRulePrefix);
return new RoslynRuleId(RoslynRuleType.StyleRule, ParseInt(id));
}

throw new ArgumentException($"String {value} is not valid Roslyn rule id");
string type = value.Substring(0, value.Length - 4).ToUpper();
return new RoslynRuleId(type, parsedCode);
}

public override string ToString()
{
return $"{Type.ToAlias()}{Id:D4}";
return $"{RuleType}{Id:D4}";
}

public int CompareTo(RoslynRuleId other)
{
int typeComparison = Type.CompareTo(other.Type);
int typeComparison = string.Compare(RuleType, other.RuleType, StringComparison.Ordinal);
if (typeComparison != 0)
return typeComparison;
return Id.CompareTo(other.Id);
Expand Down
2 changes: 1 addition & 1 deletion Sources/Kysect.Configuin.RoslynModels/RoslynRuleIdRange.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ public static RoslynRuleIdRange Parse(string value)
public IEnumerable<RoslynRuleId> Enumerate()
{
for (int i = Start.Id; i <= End.Id; i++)
yield return new RoslynRuleId(Start.Type, i);
yield return Start with { Id = i };
}
}
22 changes: 0 additions & 22 deletions Sources/Kysect.Configuin.RoslynModels/RoslynRuleType.cs

This file was deleted.

7 changes: 7 additions & 0 deletions Sources/Kysect.Configuin.RoslynModels/RoslynRuleTypes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Kysect.Configuin.RoslynModels;

public static class RoslynRuleTypes
{
public const string StyleRule = "IDE";
public const string QualityRule = "CA";
}
22 changes: 12 additions & 10 deletions Sources/Kysect.Configuin.Tests/RoslynRuleIdTests.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using Kysect.Configuin.RoslynModels;
using Kysect.Configuin.Common;
using Kysect.Configuin.RoslynModels;

namespace Kysect.Configuin.Tests;

public class RoslynRuleIdTests
{
[Theory]
[InlineData("CA1000", RoslynRuleType.QualityRule, 1000)]
[InlineData("ca1001", RoslynRuleType.QualityRule, 1001)]
[InlineData("IDE1002", RoslynRuleType.StyleRule, 1002)]
public void Parse(string input, RoslynRuleType type, int id)
[InlineData("CA1000", RoslynRuleTypes.QualityRule, 1000)]
[InlineData("ca1001", RoslynRuleTypes.QualityRule, 1001)]
[InlineData("IDE1002", RoslynRuleTypes.StyleRule, 1002)]
[InlineData("CS0219", "CS", 0219)]
public void Parse(string input, string type, int id)
{
var expected = new RoslynRuleId(type, id);

Expand All @@ -20,9 +22,9 @@ public void Parse(string input, RoslynRuleType type, int id)
[Fact]
public void Parse_WithIncorrectPrefix_ThrowException()
{
Assert.Throws<ArgumentException>(() =>
Assert.Throws<ConfiguinException>(() =>
{
var roslynRuleId = RoslynRuleId.Parse("QWE1234");
var roslynRuleId = RoslynRuleId.Parse("QWE234");
});
}

Expand All @@ -36,8 +38,8 @@ public void Parse_Range_ReturnAllValueInRange()

roslynRuleIds
.Should().HaveCount(3)
.And.Contain(new RoslynRuleId(RoslynRuleType.QualityRule, 1865))
.And.Contain(new RoslynRuleId(RoslynRuleType.QualityRule, 1866))
.And.Contain(new RoslynRuleId(RoslynRuleType.QualityRule, 1867));
.And.Contain(new RoslynRuleId("CA", 1865))
.And.Contain(new RoslynRuleId("CA", 1866))
.And.Contain(new RoslynRuleId("CA", 1867));
}
}

0 comments on commit dcdf829

Please sign in to comment.